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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
''' Kind reminders, (please review before editing!)
    - Forms are for validating input, NOT business logic.
    - Only include model specific logic in forms if it is required for input.
      Example: user should be able to select only the groups it is member of.
    '''

from django import forms
from django.contrib.comments.forms import CommentForm

from bashoneliners.main.models import OneLiner, HackerProfile, User, Question


class CommonOneLinerForm(forms.ModelForm):
    user = None
    next_url = forms.URLField(required=False)
    action = forms.CharField()

    def __init__(self, user, *args, **kwargs):
	self.user = user
	super(CommonOneLinerForm, self).__init__(*args, **kwargs)

    class Meta:
	model = OneLiner

	widgets = {
		'summary': forms.TextInput(attrs={'class': 'span6', }),
		'line': forms.TextInput(attrs={'class': 'span6', }),
		'explanation': forms.Textarea(attrs={'rows': 10, 'class': 'span6', }),
		'limitations': forms.Textarea(attrs={'rows': 3, 'class': 'span6', }),
		}

	fields = (
		'line',
		'summary',
		'explanation',
		'limitations',
		'is_published',
		)


class PostOneLinerForm(CommonOneLinerForm):
    title = 'Post a One-Liner'
    actions = ({'name': 'Post one-liner', 'cssclass': 'btn-primary'},)

    def save(self):
	self.instance.user = self.user
	return super(PostOneLinerForm, self).save()


class EditOneLinerForm(CommonOneLinerForm):
    title = 'Edit one-liner'
    action_save = {'name': 'Save one-liner', 'cssclass': 'btn-primary'}
    action_delete = {'name': 'Delete one-liner', 'cssclass': 'btn-danger'}
    actions = (action_save, action_delete)
    edit = True
    is_save = False
    is_delete = False

    def clean_action(self):
	action = self.cleaned_data['action']
	if action == self.action_save['name']:
	    self.is_save = True
	elif action == self.action_delete['name']:
	    self.is_delete = True
	return action

    def clean(self):
	if self.instance.user != self.user:
	    raise forms.ValidationError('User %s is not the owner of this OneLiner' % self.user)

	return self.cleaned_data


class SearchOneLinerForm(forms.Form):
    query = forms.CharField(max_length=100, widget=forms.TextInput(attrs={'placeholder': 'Search', 'class': 'search-query', }))


class EditHackerProfileForm(forms.ModelForm):
    next_url = forms.URLField(required=False)

    def clean_display_name(self):
	display_name = self.cleaned_data['display_name']
	if display_name == '':
	    display_name = None
	return display_name

    class Meta:
	model = HackerProfile

	widgets = {
		'display_name': forms.TextInput(attrs={'class': '', }),
		'twitter_name': forms.TextInput(attrs={'class': '', }),
		'blog_url': forms.TextInput(attrs={'class': 'span6', }),
		'homepage_url': forms.TextInput(attrs={'class': 'span6', }),
		}

	exclude = (
		'user',
		)


class CommonQuestionForm(forms.ModelForm):
    user = None
    next_url = forms.URLField(required=False)
    action = forms.CharField()

    def __init__(self, user, *args, **kwargs):
	self.user = user
	super(CommonQuestionForm, self).__init__(*args, **kwargs)

    class Meta:
	model = Question

	widgets = {
		'summary': forms.TextInput(attrs={'class': 'span6', }),
		'explanation': forms.Textarea(attrs={'rows': 5, 'class': 'span6', }),
		}

	fields = (
		'summary',
		'explanation',
		'is_published',
		'is_answered',
		)


class PostQuestionForm(CommonQuestionForm):
    title = 'Post a question'
    actions = ({'name': 'Post question', 'cssclass': 'btn-primary'},)

    def save(self):
	self.instance.user = self.user
	return super(PostQuestionForm, self).save()


class EditQuestionForm(CommonQuestionForm):
    title = 'Edit question'
    action_save = {'name': 'Save question', 'cssclass': 'btn-primary'}
    action_delete = {'name': 'Delete question', 'cssclass': 'btn-danger'}
    actions = (action_save, action_delete)
    edit = True
    is_save = False
    is_delete = False

    def clean_action(self):
	action = self.cleaned_data['action']
	if action == self.action_save['name']:
	    self.is_save = True
	elif action == self.action_delete['name']:
	    self.is_delete = True
	return action

    def clean(self):
	if self.instance.user != self.user:
	    raise forms.ValidationError('User %s is not the owner of this Question' % self.user)

	return self.cleaned_data


class PostCommentOnOneLinerForm(CommentForm):
    next_url = forms.URLField(required=False)

    def __init__(self, *args, **kwargs):
	super(PostCommentOnOneLinerForm, self).__init__(*args, **kwargs)
	self.fields['comment'].widget = forms.Textarea(attrs={'rows': 5, 'class': 'span6', })


# eof