summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--templates/general_form.html24
-rw-r--r--templates/todolists/add.html25
-rw-r--r--todolists/views.py65
3 files changed, 65 insertions, 49 deletions
diff --git a/templates/general_form.html b/templates/general_form.html
new file mode 100644
index 00000000..4c174817
--- /dev/null
+++ b/templates/general_form.html
@@ -0,0 +1,24 @@
+{% extends "base.html" %}
+
+{% block content %}
+ <div class="greybox">
+ <h2 class="title">{{title}}</h2>
+ <form method="post" action=".">
+ <table>
+ {% for field in form %}
+ <tr>
+ <td style="vertical-align:top">
+ <span {% if field.required %}style="fontweight: bold"{% endif%}>{{field.label}}:</span>
+ <br /><span style="font-size:x-small">{{field.help_text}}</span></td>
+ <td>{{field}}</td>
+ </tr>
+ {% endfor %}
+ <tr>
+ <td colspan="2" style="text-align:right">
+ <input type="submit" value=" {{submit_text}} " />
+ </td>
+ </tr>
+ </table>
+ </form>
+ </div>
+{% endblock %}
diff --git a/templates/todolists/add.html b/templates/todolists/add.html
deleted file mode 100644
index a53637ce..00000000
--- a/templates/todolists/add.html
+++ /dev/null
@@ -1,25 +0,0 @@
-{% extends "base.html" %}
-
-{% block content %}
- <div class="greybox">
- <h2 class="title">Add ToDo List</h2>
- <form method="post" action=".">
- <table>
- <tr>
- <td>Name:</td>
- <td><input type="text" name="name" size="30" value="" /></td>
- </tr><tr>
- <td style="vertical-align:top">Description:</td>
- <td><textarea name="description" cols="60" rows="4"></textarea></td>
- </tr><tr>
- <td style="vertical-align:top">List of Package Names:<br /><span style="font-size:x-small">(one per line)</span></td>
- <td><textarea name="packages" cols="60" rows="20"></textarea></td>
- </tr><tr>
- <td colspan="2" style="text-align:right">
- <input type="submit" value=" Create List " />
- </td>
- </tr>
- </table>
- </form>
- </div>
-{% endblock %}
diff --git a/todolists/views.py b/todolists/views.py
index 82f2d94a..d76c24b3 100644
--- a/todolists/views.py
+++ b/todolists/views.py
@@ -1,6 +1,7 @@
import django.newforms as forms
from django.http import HttpResponse, HttpResponseRedirect
+from django.template import RequestContext
from django.shortcuts import get_object_or_404
from django.contrib.auth.decorators import permission_required
from django.contrib.auth.models import User
@@ -12,6 +13,25 @@ from archweb_dev.main.models import Arch, Repo
import django.db
IntegrityError = django.db.backend.Database.IntegrityError
+class TodoListForm(forms.Form):
+ name = forms.CharField(max_length=255,
+ widget=forms.TextInput(attrs={'size': '30'}))
+ description = forms.CharField(required=False,
+ widget=forms.Textarea(attrs={'rows': '4', 'cols': '60'}))
+ packages = forms.CharField(required=False,
+ help_text='(one per line)',
+ widget=forms.Textarea(attrs={'rows': '20', 'cols': '60'}))
+
+ def clean_packages(self):
+ packages = []
+ for p in self.clean_data['packages'].split("\n"):
+ for pkg in Package.objects.filter(
+ pkgname=p.strip()).order_by('arch').distinct():
+ packages .append(pkg)
+
+ return packages
+
+
def flag(request, listid, pkgid):
list = get_object_or_404(Todolist, id=listid)
pkg = get_object_or_404(TodolistPkg, id=pkgid)
@@ -37,31 +57,28 @@ def list(request):
@permission_required('todolists.add_todolist')
def add(request):
if request.POST:
- try:
- m = User.objects.get(username=request.user.username)
- except User.DoesNotExist:
- return render_response(request, 'error_page.html',
- {'errmsg': 'Cannot find a maintainer record for you!'})
# create the list
- todo = Todolist(
- creator = m,
- name = request.POST.get('name'),
- description = request.POST.get('description'))
- todo.save()
- # now link in packages
- for p in request.POST.get('packages').split("\n"):
- for pkg in Package.objects.filter(
- pkgname=p.strip()).order_by('arch'):
- todopkg = TodolistPkg(
- list = todo,
- pkg = pkg)
- try:
- todopkg.save()
- except IntegrityError, (num, desc):
- if num != 1062: # duplicate entry aka dupe package on list
- raise
- return HttpResponseRedirect('/todo/')
- return render_response(request, 'todolists/add.html')
+ form = TodoListForm(request.POST)
+ if form.is_valid():
+ todo = Todolist(
+ creator = request.user,
+ name = form.clean_data['name'],
+ description = form.clean_data['description'])
+ todo.save()
+ # now link in packages
+ for pkg in form.clean_data['packages']:
+ todopkg = TodolistPkg(list = todo, pkg = pkg)
+ todopkg.save()
+ return HttpResponseRedirect('/todo/')
+ else:
+ form = TodoListForm()
+
+ page_dict = {
+ 'title': 'Add To-do List',
+ 'form': form,
+ 'submit_text': 'Create List'
+ }
+ return render_response(request, 'general_form.html', page_dict)
# vim: set ts=4 sw=4 et: