diff --git a/core/forms.py b/core/forms.py index dc58b064..e7a709db 100644 --- a/core/forms.py +++ b/core/forms.py @@ -43,12 +43,21 @@ class LoginForm(AuthenticationForm): ) class PageForm(forms.ModelForm): + #parent = forms.ModelChoiceField(queryset=Page.objects.all()) + #def __init__(self, *args, **kwargs): + # super(PageForm, self).__init__(*args, **kwargs) + class Meta: model = Page - fields = ['name', 'title', 'content', ] + fields = ['parent', 'name', 'title', 'content', ] def save(self, commit=True): 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: page.save() return page diff --git a/core/migrations/0009_auto_20151123_0902.py b/core/migrations/0009_auto_20151123_0902.py new file mode 100644 index 00000000..651bf266 --- /dev/null +++ b/core/migrations/0009_auto_20151123_0902.py @@ -0,0 +1,23 @@ +# -*- 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), + ), + ] diff --git a/core/migrations/0010_auto_20151123_0948.py b/core/migrations/0010_auto_20151123_0948.py new file mode 100644 index 00000000..d6ec0bb9 --- /dev/null +++ b/core/migrations/0010_auto_20151123_0948.py @@ -0,0 +1,19 @@ +# -*- 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', + ), + ] diff --git a/core/migrations/0011_page_name.py b/core/migrations/0011_page_name.py new file mode 100644 index 00000000..ad0d0762 --- /dev/null +++ b/core/migrations/0011_page_name.py @@ -0,0 +1,20 @@ +# -*- 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, + ), + ] diff --git a/core/migrations/0012_auto_20151123_1002.py b/core/migrations/0012_auto_20151123_1002.py new file mode 100644 index 00000000..6953c967 --- /dev/null +++ b/core/migrations/0012_auto_20151123_1002.py @@ -0,0 +1,23 @@ +# -*- 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'), + ), + ] diff --git a/core/migrations/0013_auto_20151123_1033.py b/core/migrations/0013_auto_20151123_1033.py new file mode 100644 index 00000000..fbf31b70 --- /dev/null +++ b/core/migrations/0013_auto_20151123_1033.py @@ -0,0 +1,25 @@ +# -*- 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), + ), + ] diff --git a/core/models.py b/core/models.py index e9cdec46..c4f6a0b2 100644 --- a/core/models.py +++ b/core/models.py @@ -108,12 +108,13 @@ class User(AbstractBaseUser, PermissionsMixin): class Page(models.Model): + full_name = models.CharField(_('page full name'), max_length=30, blank=False, primary_key=True) name = models.CharField(_('page name'), max_length=30, blank=False) title = models.CharField(_("page title"), max_length=255, blank=True) content = models.TextField(_("page content"), blank=True) revision = models.PositiveIntegerField(_("current revision"), default=1) is_locked = models.BooleanField(_("page mutex"), default=False) - children = models.ForeignKey('self', related_name="parent", null=True) + parent = models.ForeignKey('self', related_name="children", null=True, blank=True, on_delete=models.SET_NULL) class Meta: permissions = ( @@ -122,10 +123,8 @@ class Page(models.Model): ) def __str__(self): - return self.name + return self.full_name def get_display_name(self): - return self.name + return self.full_name - #def get(self, *args): - # return self.__dict__ diff --git a/core/templates/core/page.html b/core/templates/core/page.html index f34392e9..19d17dd4 100644 --- a/core/templates/core/page.html +++ b/core/templates/core/page.html @@ -11,11 +11,11 @@ {% endblock %} {% block content %} - {% if page %} + {% if view_page %}

Page

Back to list

{% if user.is_superuser %} -

Edit

+

Edit

{% endif %}

You're seeing the page {{ page.get_display_name }}

{{ page.title }}

@@ -24,7 +24,7 @@

Page list

{% elif new_page %} @@ -32,7 +32,7 @@

Create it?

{% elif page_form %}

Edit page

-
+ {% csrf_token %} {{ page_form }}

diff --git a/core/urls.py b/core/urls.py index b40c30c4..7fc5f0f2 100644 --- a/core/urls.py +++ b/core/urls.py @@ -11,7 +11,7 @@ urlpatterns = [ url(r'^user/(?P[0-9]+)/$', views.user, name='user_profile'), url(r'^user/(?P[0-9]+)/edit$', views.user_edit, name='user_edit'), url(r'^page/$', views.page, name='page_list'), - url(r'^page/(?P[a-z0-9]*)/$', views.page, name='page'), - url(r'^page/(?P[a-z0-9]*)/edit$', views.page_edit, name='page_edit'), + url(r'^page/(?P[a-z0-9/]*)/$', views.page, name='page'), + url(r'^page/(?P[a-z0-9/]*)/edit$', views.page_edit, name='page_edit'), ] diff --git a/core/views.py b/core/views.py index c4204fc6..4bda1300 100644 --- a/core/views.py +++ b/core/views.py @@ -93,7 +93,8 @@ def page(request, page_name=None): context['page_list'] = Page.objects.all return render(request, "core/page.html", context) try: - context['page'] = Page.objects.filter(name=page_name).get() + context['view_page'] = True + context['page'] = Page.objects.filter(full_name=page_name).get() context['title'] = context['page'].title context['test'] = "PAGE_FOUND" except Page.DoesNotExist: @@ -111,8 +112,14 @@ def page_edit(request, page_name=None): if request.method == 'POST': f = PageForm(request.POST, instance=p) if f.is_valid(): + print("TROLL") f.save() context['test'] = "PAGE_SAVED" + else: + context['test'] = "PAGE_NOT_SAVED" + else: + context['test'] = "POST_NOT_RECEIVED" + context['page'] = p context['page_form'] = PageForm(instance=p).as_p() return render(request, 'core/page.html', context)