diff --git a/core/admin.py b/core/admin.py index ee64fc0f..5a9d70ec 100644 --- a/core/admin.py +++ b/core/admin.py @@ -1,6 +1,7 @@ from django.contrib import admin -from .models import User +from .models import User, Page admin.site.register(User) +admin.site.register(Page) diff --git a/core/forms.py b/core/forms.py index 15032abd..6fa430d2 100644 --- a/core/forms.py +++ b/core/forms.py @@ -3,7 +3,7 @@ from django import forms from django.contrib.auth import logout, login, authenticate import logging -from .models import User +from .models import User, Page class RegisteringForm(UserCreationForm): error_css_class = 'error' @@ -42,4 +42,15 @@ class LoginForm(AuthenticationForm): params={'username': self.username_field.verbose_name}, ) +class PageForm(forms.ModelForm): + class Meta: + model = Page + fields = ['name', 'title', 'content', ] + + def save(self, commit=True): + page = super(PageForm, self).save(commit=False) + if commit: + page.save() + return page + diff --git a/core/migrations/0004_page_children.py b/core/migrations/0004_page_children.py new file mode 100644 index 00000000..4ecd56be --- /dev/null +++ b/core/migrations/0004_page_children.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', '0003_auto_20151119_1635'), + ] + + operations = [ + migrations.AddField( + model_name='page', + name='children', + field=models.ForeignKey(to='core.Page', related_name='parent', null=True), + ), + ] diff --git a/core/migrations/0005_page_title.py b/core/migrations/0005_page_title.py new file mode 100644 index 00000000..66916b15 --- /dev/null +++ b/core/migrations/0005_page_title.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', '0004_page_children'), + ] + + operations = [ + migrations.AddField( + model_name='page', + name='title', + field=models.CharField(blank=True, max_length=255, verbose_name='page title'), + ), + ] diff --git a/core/migrations/0006_auto_20151120_0958.py b/core/migrations/0006_auto_20151120_0958.py new file mode 100644 index 00000000..24c9d79c --- /dev/null +++ b/core/migrations/0006_auto_20151120_0958.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', '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'), + ), + ] diff --git a/core/migrations/0007_auto_20151120_1347.py b/core/migrations/0007_auto_20151120_1347.py new file mode 100644 index 00000000..b8b8b83e --- /dev/null +++ b/core/migrations/0007_auto_20151120_1347.py @@ -0,0 +1,24 @@ +# -*- 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, + ), + ] diff --git a/core/models.py b/core/models.py index b721dd19..1ec6f22b 100644 --- a/core/models.py +++ b/core/models.py @@ -106,12 +106,14 @@ class User(AbstractBaseUser, PermissionsMixin): self.username = user_name return user_name + class Page(models.Model): name = models.CharField(_('page name'), max_length=30, blank=False) - full_name = models.CharField(_("full name"), max_length=255, 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) class Meta: permissions = ( @@ -120,5 +122,10 @@ class Page(models.Model): ) def __str__(self): - return self.full_name + return self.name + def get_display_name(self): + return self.name + + #def get(self, *args): + # return self.__dict__ diff --git a/core/templates/core/base.html b/core/templates/core/base.html index e763dc8d..cbb7b8a2 100644 --- a/core/templates/core/base.html +++ b/core/templates/core/base.html @@ -14,6 +14,7 @@
  • Login
  • Logout
  • Users
  • +
  • Pages
  • {% endblock %} diff --git a/core/templates/core/page.html b/core/templates/core/page.html new file mode 100644 index 00000000..f34392e9 --- /dev/null +++ b/core/templates/core/page.html @@ -0,0 +1,45 @@ +{% extends "core/base.html" %} + +{% block title %} + {% if page %} + {{ page.get_display_name }} + {% elif page_list %} + Page list + {% else %} + {{ title }} + {% endif %} +{% endblock %} + +{% block content %} + {% if page %} +

    Page

    +

    Back to list

    + {% if user.is_superuser %} +

    Edit

    + {% endif %} +

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

    +

    {{ page.title }}

    +

    {{ page.content }}

    + {% elif page_list %} +

    Page list

    + + {% elif new_page %} +

    {{ title }}

    +

    Create it?

    + {% elif page_form %} +

    Edit page

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

    +
    + {% else %} + There is no page in this website. + {% endif %} +{% endblock %} + + diff --git a/core/urls.py b/core/urls.py index ae5a2b7a..b40c30c4 100644 --- a/core/urls.py +++ b/core/urls.py @@ -10,5 +10,8 @@ urlpatterns = [ url(r'^user/$', views.user, name='user_list'), 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'), ] diff --git a/core/views.py b/core/views.py index 26a14947..c4204fc6 100644 --- a/core/views.py +++ b/core/views.py @@ -1,9 +1,10 @@ from django.shortcuts import render, redirect, get_object_or_404 from django.http import HttpResponse from django.contrib.auth import logout as auth_logout +from django.db import models -from .models import User -from .forms import RegisteringForm, LoginForm +from .models import User, Page +from .forms import RegisteringForm, LoginForm, PageForm import logging @@ -86,3 +87,32 @@ def user_edit(request, user_id=None): return render(request, "core/edit_user.html", context) return user(request, user_id) +def page(request, page_name=None): + context = {'title': 'View a Page'} + if 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['title'] = context['page'].title + context['test'] = "PAGE_FOUND" + except Page.DoesNotExist: + context['title'] = "This page does not exist" + context['new_page'] = page_name + context['test'] = "PAGE_NOT_FOUND" + return render(request, "core/page.html", context) + +def page_edit(request, page_name=None): + context = {'title': 'Edit a page', + 'page_name': page_name} + p = Page.objects.filter(name=page_name).first() + if p == None: + p = Page(name=page_name) + if request.method == 'POST': + f = PageForm(request.POST, instance=p) + if f.is_valid(): + f.save() + context['test'] = "PAGE_SAVED" + context['page_form'] = PageForm(instance=p).as_p() + return render(request, 'core/page.html', context) + diff --git a/users.json b/users.json new file mode 100644 index 00000000..5215f6f1 --- /dev/null +++ b/users.json @@ -0,0 +1 @@ +[{"model": "core.user", "pk": 1, "fields": {"is_staff": true, "password": "pbkdf2_sha256$20000$MDukCN5X8Bof$rYdhppKiusj+W/1Rxpy0yuYsEyWocESEjtRsopkOc5c=", "first_name": "Ro", "is_active": true, "date_of_birth": "1969-12-31T23:00:00Z", "date_joined": "2015-11-19T16:05:51.764Z", "nick_name": "", "is_superuser": true, "username": "root", "email": "bibou@git.an", "last_login": "2015-11-19T16:16:59.539Z", "last_name": "Ot", "user_permissions": [], "groups": []}}, {"model": "core.user", "pk": 2, "fields": {"is_staff": false, "password": "pbkdf2_sha256$20000$UK9a29p5bBEh$Jzv7xs0W9njJZiXfIdYXDydim/3YHs6awKwDmN7gSAc=", "first_name": "Skia", "is_active": true, "date_of_birth": "1969-12-31T23:00:00Z", "date_joined": "2015-11-19T16:06:29.556Z", "nick_name": "", "is_superuser": false, "username": "skia", "email": "plop@libskia.so", "last_login": "2015-11-19T16:16:49.606Z", "last_name": "Kia", "user_permissions": [], "groups": []}}] \ No newline at end of file