Moving widgets out of an app to close #416
[oweals/karmaworld.git] / karmaworld / apps / notes / forms.py
1 #!/usr/bin/env python
2 # -*- coding:utf8 -*-
3 # Copyright (C) 2012  FinalsClub Foundation
4 from django.forms import ModelForm, IntegerField, HiddenInput, Form, CharField, Textarea
5 from django.forms import TextInput
6 from django_filepicker.widgets import FPFileWidget
7 from django.template.loader import render_to_string
8 from karmaworld.utils.widgets import RichTextEditor
9
10 from karmaworld.apps.notes.models import Note, NoteMarkdown
11
12
13 class NoteForm(ModelForm):
14     html = CharField(widget=RichTextEditor)
15
16     def save(self, *args, **kwargs):
17         # TODO: use transaction.atomic for this when we switch to Django 1.6+
18         instance = super(NoteForm, self).save(*args, **kwargs)
19         instance.tags.set(*self.cleaned_data['tags'])
20         if instance.is_hidden:
21             instance.is_hidden = False
22             instance.save()
23         if instance.is_editable() and self.cleaned_data.get('html'):
24             try:
25                 note_markdown = instance.notemarkdown
26             except NoteMarkdown.DoesNotExist:
27                 note_markdown = NoteMarkdown(note=instance)
28             note_markdown.html = self.cleaned_data['html']
29             note_markdown.full_clean()
30             note_markdown.save()
31         return instance
32
33     class Meta:
34         model = Note
35         fields = ('name', 'tags', 'html')
36         widgets = {
37           'name': TextInput()
38         }
39
40 class NoteDeleteForm(Form):
41     note = IntegerField(widget=HiddenInput())
42
43 class FileUploadForm(ModelForm):
44     auto_id = False
45     class Meta:
46         model = Note
47         fields = ('fp_file',)
48         widgets = {
49           'fp_file': FPFileWidget(attrs={
50                        'id': 'filepicker-file-upload',
51                      }),
52         }