~chipaca/unity-lens-video/custom-user-agent

« back to all changes in this revision

Viewing changes to oneliners/forms.py

  • Committer: Janos Gyerik
  • Date: 2012-05-21 18:31:58 UTC
  • Revision ID: janos@axiom-20120521183158-kvtqzuo6yhe3mdzq
added example configuration for logging to file

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
    '''
6
6
 
7
7
from django import forms
8
 
 
9
 
from bashoneliners.main.models import OneLiner
10
 
 
11
 
''' constants '''
12
 
 
13
 
#
14
 
 
15
 
 
16
 
''' forms '''
17
 
 
18
 
class PostOneLinerForm(forms.ModelForm):
19
 
    def save(self, hacker):
20
 
        self.instance.hacker = hacker
21
 
        return super(PostOneLinerForm, self).save()
22
 
 
23
 
    class Meta:
24
 
        model = OneLiner
25
 
 
26
 
        widgets = {
27
 
                'line': forms.Textarea(attrs={'cols': 80, 'rows': 3, }),
28
 
                'summary': forms.Textarea(attrs={'cols': 80, 'rows': 3, }),
29
 
                'explanation': forms.Textarea(attrs={'cols': 80, 'rows': 10, }),
30
 
                'caveats': forms.Textarea(attrs={'cols': 80, 'rows': 3, }),
31
 
                }
32
 
 
33
 
        fields = (
34
 
                'line',
35
 
                'summary',
36
 
                'explanation',
37
 
                'caveats',
38
 
                'is_published',
39
 
                )
 
8
from django.contrib.comments.forms import CommentForm
 
9
 
 
10
from oneliners.models import OneLiner, HackerProfile, Question
 
11
 
 
12
 
 
13
class CommonOneLinerForm(forms.ModelForm):
 
14
    user = None
 
15
    next_url = forms.URLField(required=False)
 
16
    action = forms.CharField()
 
17
 
 
18
    def __init__(self, user, *args, **kwargs):
 
19
        self.user = user
 
20
        super(CommonOneLinerForm, self).__init__(*args, **kwargs)
 
21
 
 
22
    class Meta:
 
23
        model = OneLiner
 
24
 
 
25
        widgets = {
 
26
                'summary': forms.TextInput(attrs={'class': 'span6', }),
 
27
                'line': forms.TextInput(attrs={'class': 'span6', }),
 
28
                'explanation': forms.Textarea(attrs={'rows': 10, 'class': 'span6', }),
 
29
                'limitations': forms.Textarea(attrs={'rows': 3, 'class': 'span6', }),
 
30
                }
 
31
 
 
32
        fields = (
 
33
                'line',
 
34
                'summary',
 
35
                'explanation',
 
36
                'limitations',
 
37
                'is_published',
 
38
                )
 
39
 
 
40
 
 
41
class PostOneLinerForm(CommonOneLinerForm):
 
42
    title = 'Post a One-Liner'
 
43
    actions = ({'name': 'Post one-liner', 'cssclass': 'btn-primary'},)
 
44
 
 
45
    def save(self):
 
46
        self.instance.user = self.user
 
47
        return super(PostOneLinerForm, self).save()
 
48
 
 
49
 
 
50
class EditOneLinerForm(CommonOneLinerForm):
 
51
    title = 'Edit one-liner'
 
52
    action_save = {'name': 'Save one-liner', 'cssclass': 'btn-primary'}
 
53
    action_delete = {'name': 'Delete one-liner', 'cssclass': 'btn-danger'}
 
54
    actions = (action_save, action_delete)
 
55
    edit = True
 
56
    is_save = False
 
57
    is_delete = False
 
58
 
 
59
    def clean_action(self):
 
60
        action = self.cleaned_data['action']
 
61
        if action == self.action_save['name']:
 
62
            self.is_save = True
 
63
        elif action == self.action_delete['name']:
 
64
            self.is_delete = True
 
65
        return action
 
66
 
 
67
    def clean(self):
 
68
        if self.instance.user != self.user:
 
69
            raise forms.ValidationError('User %s is not the owner of this OneLiner' % self.user)
 
70
        return self.cleaned_data
 
71
 
 
72
 
 
73
class SearchOneLinerForm(forms.Form):
 
74
    query = forms.CharField(max_length=100, widget=forms.TextInput(attrs={'class': 'span4', }))
 
75
    is_advanced = forms.BooleanField(required=False)
 
76
    match_summary = forms.BooleanField(initial=True, required=False)
 
77
    match_line = forms.BooleanField(initial=True, required=False)
 
78
    match_explanation = forms.BooleanField(initial=True, required=False)
 
79
    match_limitations = forms.BooleanField(initial=True, required=False)
 
80
    match_whole_words = forms.BooleanField(initial=False, required=False)
 
81
 
 
82
 
 
83
class EditHackerProfileForm(forms.ModelForm):
 
84
    next_url = forms.URLField(required=False)
 
85
 
 
86
    def clean_display_name(self):
 
87
        display_name = self.cleaned_data['display_name']
 
88
        if display_name == '':
 
89
            display_name = None
 
90
        return display_name
 
91
 
 
92
    class Meta:
 
93
        model = HackerProfile
 
94
 
 
95
        widgets = {
 
96
                'display_name': forms.TextInput(attrs={'class': '', }),
 
97
                'twitter_name': forms.TextInput(attrs={'class': '', }),
 
98
                'blog_url': forms.TextInput(attrs={'class': 'span6', }),
 
99
                'homepage_url': forms.TextInput(attrs={'class': 'span6', }),
 
100
                }
 
101
 
 
102
        exclude = (
 
103
                'user',
 
104
                )
 
105
 
 
106
 
 
107
class CommonQuestionForm(forms.ModelForm):
 
108
    user = None
 
109
    next_url = forms.URLField(required=False)
 
110
    action = forms.CharField()
 
111
 
 
112
    def __init__(self, user, *args, **kwargs):
 
113
        self.user = user
 
114
        super(CommonQuestionForm, self).__init__(*args, **kwargs)
 
115
 
 
116
    class Meta:
 
117
        model = Question
 
118
 
 
119
        widgets = {
 
120
                'summary': forms.TextInput(attrs={'class': 'span6', }),
 
121
                'explanation': forms.Textarea(attrs={'rows': 5, 'class': 'span6', }),
 
122
                }
 
123
 
 
124
        fields = (
 
125
                'summary',
 
126
                'explanation',
 
127
                'is_published',
 
128
                'is_answered',
 
129
                )
 
130
 
 
131
 
 
132
class PostQuestionForm(CommonQuestionForm):
 
133
    title = 'Post a question'
 
134
    actions = ({'name': 'Post question', 'cssclass': 'btn-primary'},)
 
135
 
 
136
    def save(self):
 
137
        self.instance.user = self.user
 
138
        return super(PostQuestionForm, self).save()
 
139
 
 
140
 
 
141
class EditQuestionForm(CommonQuestionForm):
 
142
    title = 'Edit question'
 
143
    action_save = {'name': 'Save question', 'cssclass': 'btn-primary'}
 
144
    action_delete = {'name': 'Delete question', 'cssclass': 'btn-danger'}
 
145
    actions = (action_save, action_delete)
 
146
    edit = True
 
147
    is_save = False
 
148
    is_delete = False
 
149
 
 
150
    def clean_action(self):
 
151
        action = self.cleaned_data['action']
 
152
        if action == self.action_save['name']:
 
153
            self.is_save = True
 
154
        elif action == self.action_delete['name']:
 
155
            self.is_delete = True
 
156
        return action
 
157
 
 
158
    def clean(self):
 
159
        if self.instance.user != self.user:
 
160
            raise forms.ValidationError('User %s is not the owner of this Question' % self.user)
 
161
        return self.cleaned_data
 
162
 
 
163
 
 
164
class PostCommentOnOneLinerForm(CommentForm):
 
165
    next_url = forms.URLField(required=False)
 
166
 
 
167
    def __init__(self, *args, **kwargs):
 
168
        super(PostCommentOnOneLinerForm, self).__init__(*args, **kwargs)
 
169
        self.fields['comment'].widget = forms.Textarea(attrs={'rows': 5, 'class': 'span6', })
40
170
 
41
171
 
42
172
# eof