diff options
author | Aurélien Bompard <aurelien@bompard.org> | 2013-07-10 19:02:39 +0200 |
---|---|---|
committer | Aurélien Bompard <aurelien@bompard.org> | 2013-07-10 19:02:39 +0200 |
commit | 5b18d42d67afc08f469ec216b1d55adeb19feda8 (patch) | |
tree | f159cfb488b7f1c25c0a78cd882daaafb794f0a4 /hyperkitty/views | |
parent | f2950f4b226b5a7d388c4eb2fb6c383e72ac6d54 (diff) | |
download | hyperkitty-5b18d42d67afc08f469ec216b1d55adeb19feda8.tar.gz hyperkitty-5b18d42d67afc08f469ec216b1d55adeb19feda8.tar.xz hyperkitty-5b18d42d67afc08f469ec216b1d55adeb19feda8.zip |
Add categories to threads
Diffstat (limited to 'hyperkitty/views')
-rw-r--r-- | hyperkitty/views/forms.py | 3 | ||||
-rw-r--r-- | hyperkitty/views/thread.py | 46 |
2 files changed, 48 insertions, 1 deletions
diff --git a/hyperkitty/views/forms.py b/hyperkitty/views/forms.py index 99f158f..19bba26 100644 --- a/hyperkitty/views/forms.py +++ b/hyperkitty/views/forms.py @@ -135,3 +135,6 @@ class PostForm(forms.Form): message = forms.CharField(widget=forms.Textarea) attachment = forms.FileField(required=False, label="", widget=AttachmentFileInput) + +class CategoryForm(forms.Form): + category = forms.ChoiceField(label="", required=False) diff --git a/hyperkitty/views/thread.py b/hyperkitty/views/thread.py index 56f9f5c..8b080ba 100644 --- a/hyperkitty/views/thread.py +++ b/hyperkitty/views/thread.py @@ -34,7 +34,7 @@ from django.utils.timezone import utc import robot_detection from hyperkitty.models import Tag, Favorite, LastView -from forms import AddTagForm, ReplyForm +from hyperkitty.views.forms import AddTagForm, ReplyForm, CategoryForm from hyperkitty.lib import get_months, get_store, stripped_subject from hyperkitty.lib.voting import set_message_votes @@ -99,6 +99,12 @@ def thread_index(request, mlist_fqdn, threadid, month=None, year=None): else: fav_action = "rm" + # Category + categories = [ (c, c.title()) for c in store.get_categories() ] \ + + [("", "no categories")] + category_form = CategoryForm(initial={"category": thread.category or ""}) + category_form["category"].field.choices = categories + # Extract relative dates today = datetime.date.today() days_old = today - thread.starting_email.date.date() @@ -151,6 +157,8 @@ def thread_index(request, mlist_fqdn, threadid, month=None, year=None): 'participants': thread.participants, 'last_view': last_view, 'unread_count': unread_count, + 'category_form': category_form, + 'category': thread.category, } context["participants"].sort(key=lambda x: x[0].lower()) @@ -286,3 +294,39 @@ def favorite(request, mlist_fqdn, threadid): raise SuspiciousOperation return HttpResponse("success", mimetype='text/plain') + +def set_category(request, mlist_fqdn, threadid): + """ Set the category for a given thread. """ + if not request.user.is_authenticated(): + return HttpResponse('You must be logged in to add a tag', + content_type="text/plain", status=403) + if request.method != 'POST': + raise SuspiciousOperation + + store = get_store(request) + categories = [ (c, c.title()) for c in store.get_categories() ] \ + + [("", "No categories")] + category_form = CategoryForm(request.POST) + category_form["category"].field.choices = categories + + if not category_form.is_valid(): + return HttpResponse("Error settings category: invalid data", + content_type="text/plain", status=500) + + category = category_form.cleaned_data["category"] + thread = store.get_thread(mlist_fqdn, threadid) + if category and category not in store.get_categories(): + raise Http404("No such category: %s" % category) + if category != thread.category: + thread.category = category + store.commit() + + # Now refresh the category widget + FakeMList = namedtuple("MailingList", ["name"]) + context = { + "category_form": category_form, + "mlist": FakeMList(name=mlist_fqdn), + "threadid": threadid, + "category": thread.category, + } + return render(request, "threads/category.html", context) |