Almost working wiki before refactoring again

This commit is contained in:
Skia 2015-11-23 17:23:00 +01:00
parent ace66bba0a
commit d2005ff997
17 changed files with 97 additions and 300 deletions

View File

@ -43,9 +43,13 @@ class LoginForm(AuthenticationForm):
) )
class PageForm(forms.ModelForm): class PageForm(forms.ModelForm):
#parent = forms.ModelChoiceField(queryset=Page.objects.all()) error_css_class = 'error'
#def __init__(self, *args, **kwargs): required_css_class = 'required'
# super(PageForm, self).__init__(*args, **kwargs) parent = forms.ModelChoiceField(queryset=Page.objects.all())
def __init__(self, *args, **kwargs):
super(PageForm, self).__init__(*args, **kwargs)
self.fields['parent'].required = False
class Meta: class Meta:
model = Page model = Page
@ -53,11 +57,6 @@ class PageForm(forms.ModelForm):
def save(self, commit=True): def save(self, commit=True):
page = super(PageForm, self).save(commit=False) page = super(PageForm, self).save(commit=False)
if self.cleaned_data['parent'] is not None:
page.full_name = '/'.join([self.cleaned_data['parent'].full_name, self.cleaned_data['name']])
else:
page.full_name = self.cleaned_data['name']
page.name = self.cleaned_data['name'].split('/')[-1]
if commit: if commit:
page.save() page.save()
return page return page

View File

@ -2,9 +2,10 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from django.db import migrations, models from django.db import migrations, models
import django.contrib.auth.models
import django.db.models.deletion
import django.utils.timezone import django.utils.timezone
import django.core.validators import django.core.validators
import django.contrib.auth.models
class Migration(migrations.Migration): class Migration(migrations.Migration):
@ -17,28 +18,47 @@ class Migration(migrations.Migration):
migrations.CreateModel( migrations.CreateModel(
name='User', name='User',
fields=[ fields=[
('id', models.AutoField(primary_key=True, auto_created=True, verbose_name='ID', serialize=False)), ('id', models.AutoField(serialize=False, verbose_name='ID', auto_created=True, primary_key=True)),
('password', models.CharField(verbose_name='password', max_length=128)), ('password', models.CharField(verbose_name='password', max_length=128)),
('last_login', models.DateTimeField(null=True, blank=True, verbose_name='last login')), ('last_login', models.DateTimeField(blank=True, verbose_name='last login', null=True)),
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')), ('is_superuser', models.BooleanField(verbose_name='superuser status', help_text='Designates that this user has all permissions without explicitly assigning them.', default=False)),
('username', models.CharField(validators=[django.core.validators.RegexValidator('^[\\w.@+-]+$', 'Enter a valid username. This value may contain only letters, numbers and @/./+/-/_ characters.')], help_text='Required. 254 characters or fewer. Letters, digits and @/./+/-/_ only.', error_messages={'unique': 'A user with that username already exists.'}, verbose_name='username', unique=True, max_length=254)), ('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, max_length=254, verbose_name='username', validators=[django.core.validators.RegexValidator('^[\\w.@+-]+$', 'Enter a valid username. This value may contain only letters, numbers and @/./+/-/_ characters.')], unique=True, help_text='Required. 254 characters or fewer. Letters, digits and @/./+/-/_ only.')),
('first_name', models.CharField(verbose_name='first name', max_length=30)), ('first_name', models.CharField(verbose_name='first name', max_length=30)),
('last_name', models.CharField(verbose_name='last name', max_length=30)), ('last_name', models.CharField(verbose_name='last name', max_length=30)),
('email', models.EmailField(verbose_name='email address', unique=True, max_length=254)), ('email', models.EmailField(verbose_name='email address', unique=True, max_length=254)),
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
('date_of_birth', models.DateTimeField(verbose_name='date of birth')), ('date_of_birth', models.DateTimeField(verbose_name='date of birth')),
('nick_name', models.CharField(blank=True, max_length=30)), ('nick_name', models.CharField(blank=True, max_length=30)),
('groups', models.ManyToManyField(related_query_name='user', help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', blank=True, to='auth.Group', verbose_name='groups')), ('is_staff', models.BooleanField(verbose_name='staff status', help_text='Designates whether the user can log into this admin site.', default=False)),
('user_permissions', models.ManyToManyField(related_query_name='user', help_text='Specific permissions for this user.', related_name='user_set', blank=True, to='auth.Permission', verbose_name='user permissions')), ('is_active', models.BooleanField(verbose_name='active', help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', default=True)),
('date_joined', models.DateTimeField(verbose_name='date joined', default=django.utils.timezone.now)),
('groups', models.ManyToManyField(related_name='user_set', to='auth.Group', blank=True, verbose_name='groups', related_query_name='user', help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.')),
('user_permissions', models.ManyToManyField(related_name='user_set', to='auth.Permission', blank=True, verbose_name='user permissions', related_query_name='user', help_text='Specific permissions for this user.')),
], ],
options={ options={
'verbose_name_plural': 'users',
'verbose_name': 'user', 'verbose_name': 'user',
'verbose_name_plural': 'users',
}, },
managers=[ managers=[
('objects', django.contrib.auth.models.UserManager()), ('objects', django.contrib.auth.models.UserManager()),
], ],
), ),
migrations.CreateModel(
name='Page',
fields=[
('full_name', models.CharField(serialize=False, verbose_name='page full name', primary_key=True, max_length=30)),
('name', models.CharField(verbose_name='page name', max_length=30)),
('title', models.CharField(blank=True, verbose_name='page title', max_length=255)),
('content', models.TextField(blank=True, verbose_name='page content')),
('revision', models.PositiveIntegerField(verbose_name='current revision', default=1)),
('is_locked', models.BooleanField(verbose_name='page mutex', default=False)),
('parent', models.ForeignKey(related_name='children', blank=True, null=True, to='core.Page', on_delete=django.db.models.deletion.SET_NULL)),
],
options={
'permissions': (('can_edit', 'Can edit the page'), ('can_view', 'Can view the page')),
},
),
migrations.AlterUniqueTogether(
name='page',
unique_together=set([('name', 'parent')]),
),
] ]

View File

@ -1,33 +0,0 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('core', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='Page',
fields=[
('id', models.AutoField(serialize=False, primary_key=True, auto_created=True, verbose_name='ID')),
('name', models.CharField(max_length=30, verbose_name='page name')),
('full_name', models.CharField(max_length=255, verbose_name='full name')),
('content', models.TextField(blank=True, verbose_name='page content')),
('revision', models.PositiveIntegerField(default=1, verbose_name='current revision')),
('is_locked', models.BooleanField(default=False, verbose_name='page mutex')),
],
options={
'permissions': (('can_edit', 'Can edit the page'), ('can_view', 'Can view the page')),
},
),
migrations.AlterField(
model_name='user',
name='date_of_birth',
field=models.DateTimeField(default='1970-01-01T00:00:00+01:00', verbose_name='date of birth'),
),
]

View File

@ -1,21 +0,0 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
import datetime
from django.utils.timezone import utc
class Migration(migrations.Migration):
dependencies = [
('core', '0002_auto_20151119_1533'),
]
operations = [
migrations.AlterField(
model_name='user',
name='date_of_birth',
field=models.DateTimeField(default=datetime.datetime(1942, 6, 12, 0, 0, tzinfo=utc), verbose_name='date of birth'),
),
]

View File

@ -1,19 +0,0 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('core', '0003_auto_20151119_1635'),
]
operations = [
migrations.AddField(
model_name='page',
name='children',
field=models.ForeignKey(to='core.Page', related_name='parent', null=True),
),
]

View File

@ -1,19 +0,0 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('core', '0004_page_children'),
]
operations = [
migrations.AddField(
model_name='page',
name='title',
field=models.CharField(blank=True, max_length=255, verbose_name='page title'),
),
]

View File

@ -1,23 +0,0 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('core', '0005_page_title'),
]
operations = [
migrations.RemoveField(
model_name='page',
name='id',
),
migrations.AlterField(
model_name='page',
name='full_name',
field=models.CharField(primary_key=True, max_length=255, serialize=False, verbose_name='full name'),
),
]

View File

@ -1,24 +0,0 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('core', '0006_auto_20151120_0958'),
]
operations = [
migrations.RemoveField(
model_name='page',
name='full_name',
),
migrations.AddField(
model_name='page',
name='id',
field=models.AutoField(default=0, auto_created=True, verbose_name='ID', primary_key=True, serialize=False),
preserve_default=False,
),
]

View File

@ -1,19 +0,0 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('core', '0007_auto_20151120_1347'),
]
operations = [
migrations.AlterField(
model_name='user',
name='date_of_birth',
field=models.DateTimeField(verbose_name='date of birth'),
),
]

View File

@ -1,23 +0,0 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('core', '0008_auto_20151122_1717'),
]
operations = [
migrations.RemoveField(
model_name='page',
name='id',
),
migrations.AlterField(
model_name='page',
name='name',
field=models.CharField(verbose_name='page name', max_length=30, serialize=False, primary_key=True),
),
]

View File

@ -1,19 +0,0 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('core', '0009_auto_20151123_0902'),
]
operations = [
migrations.RenameField(
model_name='page',
old_name='name',
new_name='full_name',
),
]

View File

@ -1,20 +0,0 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('core', '0010_auto_20151123_0948'),
]
operations = [
migrations.AddField(
model_name='page',
name='name',
field=models.CharField(verbose_name='page name', default='guy', max_length=30),
preserve_default=False,
),
]

View File

@ -1,23 +0,0 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('core', '0011_page_name'),
]
operations = [
migrations.RemoveField(
model_name='page',
name='children',
),
migrations.AddField(
model_name='page',
name='parent',
field=models.ForeignKey(null=True, related_name='children', to='core.Page'),
),
]

View File

@ -1,25 +0,0 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('core', '0012_auto_20151123_1002'),
]
operations = [
migrations.AlterField(
model_name='page',
name='full_name',
field=models.CharField(verbose_name='page full name', primary_key=True, serialize=False, max_length=30),
),
migrations.AlterField(
model_name='page',
name='parent',
field=models.ForeignKey(on_delete=django.db.models.deletion.SET_NULL, blank=True, to='core.Page', related_name='children', null=True),
),
]

View File

@ -1,8 +1,9 @@
from django.db import models from django.db import models
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, UserManager from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, UserManager
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.core import validators
from django.utils import timezone from django.utils import timezone
from django.core import validators
from django.core.exceptions import ValidationError
from datetime import datetime from datetime import datetime
class User(AbstractBaseUser, PermissionsMixin): class User(AbstractBaseUser, PermissionsMixin):
@ -117,12 +118,57 @@ class Page(models.Model):
parent = models.ForeignKey('self', related_name="children", null=True, blank=True, on_delete=models.SET_NULL) parent = models.ForeignKey('self', related_name="children", null=True, blank=True, on_delete=models.SET_NULL)
class Meta: class Meta:
unique_together = ('name', 'parent')
permissions = ( permissions = (
("can_edit", "Can edit the page"), ("can_edit", "Can edit the page"),
("can_view", "Can view the page"), ("can_view", "Can view the page"),
) )
@staticmethod
def get_page_by_full_name(name):
parent_name = '/'.join(name.split('/')[:-1])
name = name.split('/')[-1]
if parent_name == "":
qs = Page.objects.filter(name=name, parent=None)
else:
qs = Page.objects.filter(name=name, parent__name=parent_name)
return qs.first()
def __init__(self, *args, **kwargs):
super(Page, self).__init__(*args, **kwargs)
def clean(self):
"""
This function maintains coherence between full_name, name, and parent.full_name
Be careful modifying it, it could break the entire page table!
This function is mandatory since Django does not support compound primary key,
otherwise, Page class would have had PRIMARY_KEY(name, parent)
"""
if '/' in self.name:
self.name = self.name.split('/')[-1]
if self.full_name is None or self.full_name == "":
if self.parent is None:
self.full_name = self.name
else:
self.full_name = self.parent.get_full_name()+'/'+self.name
self.full_name.strip('/')
if self.full_name.split('/')[-1] != self.name:
self.full_name = '/'.join(['/'.join(self.full_name.split('/')[:-1]), self.name])
#if Page.objects.filter(name=self.name, parent=self.parent).exists():
# raise ValidationError("Duplicate Page")
super(Page, self).clean()
print("fullname: "+self.full_name)
print("name: "+self.name)
def save(self, *args, **kwargs):
self.full_clean()
super(Page, self).save(*args, **kwargs)
def __str__(self): def __str__(self):
return self.get_full_name()
def get_full_name(self):
return self.full_name return self.full_name
def get_display_name(self): def get_display_name(self):

View File

@ -15,7 +15,7 @@
<h3>Page</h3> <h3>Page</h3>
<p><a href="{% url 'core:page_list' %}">Back to list</a></p> <p><a href="{% url 'core:page_list' %}">Back to list</a></p>
{% if user.is_superuser %} {% if user.is_superuser %}
<p><a href="{% url 'core:page_edit' page.full_name %}">Edit</a></p> <p><a href="{% url 'core:page_edit' page.get_full_name %}">Edit</a></p>
{% endif %} {% endif %}
<p>You're seeing the page <strong>{{ page.get_display_name }}</strong></p> <p>You're seeing the page <strong>{{ page.get_display_name }}</strong></p>
<h3>{{ page.title }}</h3> <h3>{{ page.title }}</h3>
@ -24,7 +24,7 @@
<h3>Page list</h3> <h3>Page list</h3>
<ul> <ul>
{% for p in page_list %} {% for p in page_list %}
<li><a href="{% url 'core:page' p.full_name %}">{{ p.get_display_name }}</a></li> <li><a href="{% url 'core:page' p.get_full_name %}">{{ p.get_display_name }}</a></li>
{% endfor %} {% endfor %}
</ul> </ul>
{% elif new_page %} {% elif new_page %}
@ -32,7 +32,7 @@
<p><a href="{% url 'core:page_edit' new_page %}">Create it?</a></p> <p><a href="{% url 'core:page_edit' new_page %}">Create it?</a></p>
{% elif page_form %} {% elif page_form %}
<h2>Edit page</h2> <h2>Edit page</h2>
<form action="{% url 'core:page_edit' page_name=page.full_name %}" method="post"> <form action="{% url 'core:page_edit' page_name=page.get_full_name %}" method="post">
{% csrf_token %} {% csrf_token %}
{{ page_form }} {{ page_form }}
<p><input type="submit" value="Save!" /></p> <p><input type="submit" value="Save!" /></p>

View File

@ -92,12 +92,12 @@ def page(request, page_name=None):
if page_name == None: if page_name == None:
context['page_list'] = Page.objects.all context['page_list'] = Page.objects.all
return render(request, "core/page.html", context) return render(request, "core/page.html", context)
try: context['page'] = Page.get_page_by_full_name(page_name)
if context['page'] is not None:
context['view_page'] = True context['view_page'] = True
context['page'] = Page.objects.filter(full_name=page_name).get()
context['title'] = context['page'].title context['title'] = context['page'].title
context['test'] = "PAGE_FOUND" context['test'] = "PAGE_FOUND"
except Page.DoesNotExist: else:
context['title'] = "This page does not exist" context['title'] = "This page does not exist"
context['new_page'] = page_name context['new_page'] = page_name
context['test'] = "PAGE_NOT_FOUND" context['test'] = "PAGE_NOT_FOUND"
@ -106,20 +106,20 @@ def page(request, page_name=None):
def page_edit(request, page_name=None): def page_edit(request, page_name=None):
context = {'title': 'Edit a page', context = {'title': 'Edit a page',
'page_name': page_name} 'page_name': page_name}
p = Page.objects.filter(name=page_name).first() p = Page.get_page_by_full_name(page_name)
if p == None: if p == None:
p = Page(name=page_name) p = Page(name=page_name)
if request.method == 'POST': if request.method == 'POST':
f = PageForm(request.POST, instance=p) f = PageForm(request.POST, instance=p)
if f.is_valid(): if f.is_valid():
print("TROLL")
f.save() f.save()
context['test'] = "PAGE_SAVED" context['tests'] = "PAGE_SAVED"
else: else:
context['test'] = "PAGE_NOT_SAVED" context['tests'] = "PAGE_NOT_SAVED"
else: else:
context['test'] = "POST_NOT_RECEIVED" context['tests'] = "POST_NOT_RECEIVED"
f = PageForm(instance=p)
context['page'] = p context['page'] = p
context['page_form'] = PageForm(instance=p).as_p() context['page_form'] = f.as_p()
return render(request, 'core/page.html', context) return render(request, 'core/page.html', context)