From 0838e0cab2f5b9992df3f7e4dc5d3aa8470cee26 Mon Sep 17 00:00:00 2001 From: Zambom Date: Mon, 16 Jan 2017 20:06:18 -0200 Subject: [PATCH] Adding topic creation --- amadeus/urls.py | 1 + subjects/templates/subjects/view.html | 13 ++++++++++++- topics/forms.py | 36 ++++++++++++++++++++++++++++++++++++ topics/migrations/0002_auto_20170116_1841.py | 26 ++++++++++++++++++++++++++ topics/models.py | 4 ++-- topics/templates/topics/_form.html | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ topics/templates/topics/create.html | 20 ++++++++++++++++++++ topics/urls.py | 8 ++++++++ topics/views.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 9 files changed, 229 insertions(+), 5 deletions(-) create mode 100644 topics/forms.py create mode 100644 topics/migrations/0002_auto_20170116_1841.py create mode 100644 topics/templates/topics/_form.html create mode 100644 topics/templates/topics/create.html create mode 100644 topics/urls.py diff --git a/amadeus/urls.py b/amadeus/urls.py index 6ef90b7..157e24c 100644 --- a/amadeus/urls.py +++ b/amadeus/urls.py @@ -27,6 +27,7 @@ urlpatterns = [ url(r'^$', index, name = 'home'), url(r'^categories/', include('categories.urls', namespace = 'categories')), url(r'^subjects/', include('subjects.urls', namespace = 'subjects')), + url(r'^topics/', include('topics.urls', namespace = 'topics')), url(r'^mailsender/', include('mailsender.urls', namespace = 'mailsender')), url(r'^security/', include('security.urls', namespace = 'security')), url(r'^themes/', include('themes.urls', namespace = 'themes')), diff --git a/subjects/templates/subjects/view.html b/subjects/templates/subjects/view.html index 17359bc..774f15a 100644 --- a/subjects/templates/subjects/view.html +++ b/subjects/templates/subjects/view.html @@ -14,6 +14,17 @@ {% endblock %} {% block content %} + {% if messages %} + {% for message in messages %} + + {% endfor %} + {% endif %} + {% if subject.visible %}
@@ -68,7 +79,7 @@

{{subject.description|safe}}

{% if request.user in subject.professor.all or request.user in subject.category.coordinators.all or request.user.is_staff %} - + {% endif %}
diff --git a/topics/forms.py b/topics/forms.py new file mode 100644 index 0000000..553db72 --- /dev/null +++ b/topics/forms.py @@ -0,0 +1,36 @@ +# coding=utf-8 +from django import forms +from django.utils.translation import ugettext_lazy as _ + +from subjects.models import Subject + +from .models import Topic + +class TopicForm(forms.ModelForm): + subject = None + + def __init__(self, *args, **kwargs): + super(TopicForm, self).__init__(*args, **kwargs) + + self.subject = kwargs['initial'].get('subject', None) + + def clean_name(self): + name = self.cleaned_data.get('name', '') + repo = self.cleaned_data.get('repository', False) + + if len(self.subject.topic_subject.filter(name = name)) > 0: + if repo: + self._errors['name'] = [_('This subject already has a repository')] + else: + self._errors['name'] = [_('This subject already has a topic with this name')] + + return ValueError + + return name + + class Meta: + model = Topic + fields = ['repository', 'name', 'description', 'visible' ] + widgets = { + 'description': forms.Textarea, + } \ No newline at end of file diff --git a/topics/migrations/0002_auto_20170116_1841.py b/topics/migrations/0002_auto_20170116_1841.py new file mode 100644 index 0000000..fff96f8 --- /dev/null +++ b/topics/migrations/0002_auto_20170116_1841.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10 on 2017-01-16 21:41 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('topics', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='topic', + name='order', + field=models.PositiveSmallIntegerField(null=True, verbose_name='Order'), + ), + migrations.AlterField( + model_name='topic', + name='subject', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='topic_subject', to='subjects.Subject', verbose_name='Subject'), + ), + ] diff --git a/topics/models.py b/topics/models.py index 7187742..e13d1f6 100644 --- a/topics/models.py +++ b/topics/models.py @@ -10,8 +10,8 @@ class Topic(models.Model): description = models.TextField(_('Description'), blank = True) repository = models.BooleanField(_('Repository'), default = False) visible = models.BooleanField(_('Visible'), default = True) - subject = models.ForeignKey(Subject, verbose_name = _('Subject'), related_name = 'topic_subject') - order = models.PositiveSmallIntegerField(_('Order')) + subject = models.ForeignKey(Subject, verbose_name = _('Subject'), related_name = 'topic_subject', null = True) + order = models.PositiveSmallIntegerField(_('Order'), null = True) create_date = models.DateTimeField(_('Create Date'), auto_now_add = True) last_update = models.DateTimeField(_('Last Update'), auto_now = True) diff --git a/topics/templates/topics/_form.html b/topics/templates/topics/_form.html new file mode 100644 index 0000000..22a37fc --- /dev/null +++ b/topics/templates/topics/_form.html @@ -0,0 +1,68 @@ +{% load static i18n %} +{% load widget_tweaks %} + +
+ {% csrf_token %} + {% for field in form %} +
+ {% if field.auto_id == 'id_description' %} + {% if field.field.required %} + + {% else %} + + {% endif %} + {% render_field field class='form-control text_wysiwyg' %} + {% elif field.auto_id == 'id_repository' or field.auto_id == 'id_visible' %} +
+ + {% if field.auto_id == 'id_repository' %} +

{% trans 'Only one topic per subject can be a repository' %}

+ {% endif %} +
+ {% else %} + {% if field.field.required %} + + {% else %} + + {% endif %} + + {% render_field field class='form-control' %} + {% endif %} + + {{ field.help_text }} + + {% if field.errors %} + + {% endif %} +
+ {% endfor %} +
+
+ +
+
+
+ + \ No newline at end of file diff --git a/topics/templates/topics/create.html b/topics/templates/topics/create.html new file mode 100644 index 0000000..c2a26c5 --- /dev/null +++ b/topics/templates/topics/create.html @@ -0,0 +1,20 @@ +{% extends 'subjects/view.html' %} + +{% load django_bootstrap_breadcrumbs %} + +{% block breadcrumbs %} + {{ block.super }} + {% breadcrumb 'Create Topic' 'topics:create' subject.slug %} +{% endblock %} + +{% block content %} +
+
+
+ {% include 'topics/_form.html' %} +
+
+
+
+
+{% endblock %} diff --git a/topics/urls.py b/topics/urls.py new file mode 100644 index 0000000..67e6698 --- /dev/null +++ b/topics/urls.py @@ -0,0 +1,8 @@ +from django.conf.urls import url +from django.contrib.auth import views as auth_views + +from . import views + +urlpatterns = [ + url(r'^create/(?P[\w_-]+)/$', views.CreateView.as_view(), name = 'create'), +] diff --git a/topics/views.py b/topics/views.py index 91ea44a..e418cfb 100644 --- a/topics/views.py +++ b/topics/views.py @@ -1,3 +1,57 @@ -from django.shortcuts import render +from django.shortcuts import get_object_or_404, redirect, render +from django.views import generic +from django.contrib import messages +from django.core.urlresolvers import reverse, reverse_lazy +from django.utils.translation import ugettext_lazy as _ +from django.contrib.auth.mixins import LoginRequiredMixin -# Create your views here. +from subjects.models import Subject + +from .models import Topic +from .forms import TopicForm + +class CreateView(LoginRequiredMixin, generic.edit.CreateView): + login_url = reverse_lazy("users:login") + redirect_field_name = 'next' + + template_name = 'topics/create.html' + form_class = TopicForm + + def get_initial(self): + initial = super(CreateView, self).get_initial() + + slug = self.kwargs.get('slug', '') + + initial['subject'] = get_object_or_404(Subject, slug = slug) + + return initial + + def form_valid(self, form): + self.object = form.save(commit = False) + + slug = self.kwargs.get('slug', '') + subject = get_object_or_404(Subject, slug = slug) + + self.object.subject = subject + self.object.order = subject.topic_subject.count() + 1 + + self.object.save() + + return super(CreateView, self).form_valid(form) + + def get_context_data(self, **kwargs): + context = super(CreateView, self).get_context_data(**kwargs) + + context['title'] = _('Create Topic') + + slug = self.kwargs.get('slug', '') + subject = get_object_or_404(Subject, slug = slug) + + context['subject'] = subject + + return context + + def get_success_url(self): + messages.success(self.request, _('Topic "%s" was created on virtual enviroment "%s" successfully!')%(self.object.name, self.object.subject.name)) + + return reverse_lazy('subjects:view', kwargs = {'slug': self.object.subject.slug}) -- libgit2 0.21.2