Advertisement
Guest User

Untitled

a guest
Mar 8th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.51 KB | None | 0 0
  1. from django import forms
  2. from django.contrib import admin
  3. from django.contrib.auth.models import Group
  4. from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
  5. from django.contrib.auth.forms import ReadOnlyPasswordHashField
  6. from django.forms.widgets import CheckboxSelectMultiple
  7. from django.forms.extras.widgets import SelectDateWidget
  8. from lists.models import User, Position, Skill, states
  9. from django.forms.fields import DateField, CharField, EmailField
  10. import datetime
  11.  
  12. birth_years = ('1920', '1921', '1922', '1923', '1924', '1925','1926', '1927', '1928', '1929',
  13. '1930', '1931', '1932', '1933', '1934', '1935','1936', '1937', '1938', '1939',
  14. '1940', '1941', '1942', '1943', '1944', '1945','1946', '1947', '1948', '1949',
  15. '1950', '1951', '1952', '1953', '1954', '1955','1956', '1957', '1958', '1959',
  16. '1960', '1961', '1962', '1963', '1964', '1965','1966', '1967', '1968', '1969',
  17. '1970', '1971', '1972', '1973', '1974', '1975','1976', '1977', '1978', '1979',
  18. '1980', '1981', '1982', '1983', '1984', '1985','1986', '1987', '1988', '1989',
  19. '1990', '1991', '1992', '1993', '1994', '1995','1996', '1997', '1998', '1999',
  20. '2000', '2001', '2002', '2003', '2004', '2005','2006', '2007', '2008', '2009',
  21. '2010', '2011', '2012', '2013', '2014', '2015','2016',
  22. )
  23.  
  24. class UserUpdateProfile(forms.ModelForm):
  25. """A form for creating new users. Includes all the required
  26. fields, plus a repeated password."""
  27. date_of_birth = DateField(widget = SelectDateWidget(years=birth_years))
  28. skill = forms.ModelMultipleChoiceField(queryset=Skill.objects.all(), widget=forms.CheckboxSelectMultiple)
  29. class Meta:
  30. model = User
  31. fields = ('email', 'location_city', 'location_state', 'bio', 'age')
  32.  
  33. def save(self, commit=True):
  34. # Save the provided password in hashed format
  35. user = super(UserUpdateProfile, self).save(commit=False)
  36.  
  37. old_save_m2m = self.save_m2m
  38. def save_m2m():
  39. old_save_m2m()
  40. user.skill.clear()
  41. for skill in self.cleaned_data['skill']:
  42. user.skill.add(skill)
  43. self.save_m2m = save_m2m
  44.  
  45. if commit:
  46. user.save()
  47. self.save_m2m()
  48. return user
  49.  
  50. class OrgUpdateProfile(forms.ModelForm):
  51. """A form for creating new users. Includes all the required
  52. fields, plus a repeated password."""
  53. class Meta:
  54. model = User
  55. fields = ('email', 'location_city', 'location_state', 'bio')
  56.  
  57. def save(self, commit=True):
  58. # Save the provided password in hashed format
  59. user = super(OrgUpdateProfile, self).save(commit=False)
  60. if commit:
  61. user.save()
  62. return user
  63.  
  64. class FillPosition(forms.ModelForm):
  65. """A form for creating new users. Includes all the required
  66. fields, plus a repeated password."""
  67. class Meta:
  68. model = Position
  69. fields = ('job_name', 'child')
  70.  
  71. def save(self, commit=True):
  72. # Save the provided password in hashed format
  73. jn = self.cleaned_data['job_name']
  74. ch = self.cleaned_data['child']
  75. user = Position.objects.get(job_name=jn)
  76. user.child = ch
  77. if commit:
  78. user.save()
  79. return user
  80.  
  81. class UserCreationForm(forms.ModelForm):
  82. """A form for creating new users. Includes all the required
  83. fields, plus a repeated password."""
  84. date_of_birth = DateField(widget = SelectDateWidget(years=birth_years))
  85. password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
  86. password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
  87. skills = forms.ModelMultipleChoiceField(queryset=Skill.objects.all(), widget=forms.CheckboxSelectMultiple)
  88.  
  89. class Meta:
  90. model = User
  91. fields = ('user_name', 'email', 'location_city', 'location_state', 'bio', 'age')
  92.  
  93. def clean_password2(self):
  94. # Check that the two password entries match
  95. password1 = self.cleaned_data.get("password1")
  96. password2 = self.cleaned_data.get("password2")
  97. if password1 and password2 and password1 != password2:
  98. raise forms.ValidationError("Passwords don't match")
  99. return password2
  100.  
  101. def save(self, commit=True):
  102. # Save the provided password in hashed format
  103. user = super(UserCreationForm, self).save(commit=False)
  104. user.set_password(self.cleaned_data["password1"])
  105. user.date_of_birth = self.cleaned_data['date_of_birth']
  106.  
  107. old_save_m2m = self.save_m2m
  108. def save_m2m():
  109. old_save_m2m()
  110. user.skills.clear()
  111. for skill in self.cleaned_data['skills']:
  112. user.skills.add(skill)
  113. self.save_m2m = save_m2m
  114.  
  115. if commit:
  116. user.save()
  117. self.save_m2m()
  118. return user
  119.  
  120. class OrgCreationForm(forms.ModelForm):
  121. """A form for creating new orgs. Includes all the required
  122. fields, plus a repeated password."""
  123. password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
  124. password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
  125.  
  126. class Meta:
  127. model = User
  128. fields = ('user_name', 'email', 'location_city', 'location_state', 'bio')
  129.  
  130. def clean_password2(self):
  131. # Check that the two password entries match
  132. password1 = self.cleaned_data.get("password1")
  133. password2 = self.cleaned_data.get("password2")
  134. if password1 and password2 and password1 != password2:
  135. raise forms.ValidationError("Passwords don't match")
  136. return password2
  137.  
  138. def save(self, commit=True):
  139. # Save the provided password in hashed format
  140. user = super(OrgCreationForm, self).save(commit=False)
  141. user.set_password(self.cleaned_data["password1"])
  142. user.user_type = 'ORG'
  143. if commit:
  144. user.save()
  145. return user
  146.  
  147. class JobCreationForm(forms.ModelForm):
  148. """A form for creating new positions. Includes all the required
  149. fields, plus a repeated password."""
  150. start_date = DateField(widget = SelectDateWidget())
  151. end_date = DateField(widget = SelectDateWidget())
  152. skills_required = forms.ModelMultipleChoiceField(queryset=Skill.objects.all(), widget=forms.CheckboxSelectMultiple)
  153. class Meta:
  154. model = Position
  155. fields = ('job_name', 'info', 'location_city', 'location_state', 'email')
  156.  
  157. def save(self, commit=True):
  158. # Save the provided password in hashed format
  159. user = super(JobCreationForm, self).save(commit=False)
  160. user.start_date = self.cleaned_data['start_date']
  161. user.end_date = self.cleaned_data['end_date']
  162.  
  163. old_save_m2m = self.save_m2m
  164. def save_m2m():
  165. old_save_m2m()
  166. user.skills.clear()
  167. for skill in self.cleaned_data['skills_required']:
  168. user.skills.add(skill)
  169. self.save_m2m = save_m2m
  170.  
  171. if commit:
  172. user.save()
  173. self.save_m2m()
  174. return user
  175.  
  176. class UserChangeForm(forms.ModelForm):
  177. """A form for updating orgs. Includes all the fields on
  178. the user, but replaces the password field with admin's
  179. password hash display field.
  180. """
  181. password = ReadOnlyPasswordHashField()
  182.  
  183. class Meta:
  184. model = User
  185. fields = ('email', 'password', 'date_of_birth', 'user_name', 'location_city', 'location_state', 'age', 'bio', 'skills', 'user_type', 'is_active', 'is_admin')
  186.  
  187. def clean_password(self):
  188. # Regardless of what the user provides, return the initial value.
  189. # This is done here, rather than on the field, because the
  190. # field does not have access to the initial value
  191. return self.initial["password"]
  192.  
  193. class JobChangeForm(forms.ModelForm):
  194. """A form for updating jobs. Includes all the fields on
  195. the user, but replaces the password field with admin's
  196. password hash display field.
  197. """
  198. class Meta:
  199. model = Position
  200. fields = ('job_name', 'email', 'info', 'start_date', 'end_date', 'location_city', 'location_state', 'is_active', 'is_admin', 'skills')
  201.  
  202. class UserAdmin(BaseUserAdmin):
  203. # The forms to add and change user instances
  204. form = UserChangeForm
  205. add_form = UserCreationForm
  206.  
  207. # The fields to be used in displaying the User model.
  208. # These override the definitions on the base UserAdmin
  209. # that reference specific fields on auth.User.
  210. list_display = ('user_name', 'email', 'location_city', 'user_type', 'is_admin')
  211. list_filter = ('user_type',)
  212. fieldsets = (
  213. (None, {'fields': ('email', 'password','user_name')}),
  214. ('Personal info', {'fields': ('date_of_birth','location_city', 'location_state', 'age', 'bio', 'skills')}),
  215. ('Permissions', {'fields': ('user_type', 'is_admin',)}),
  216. )
  217. # add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
  218. # overrides get_fieldsets to use this attribute when creating a user.
  219. add_fieldsets = (
  220. (None, {
  221. 'classes': ('wide',),
  222. 'fields': ('email', 'date_of_birth', 'password1', 'password2','user_name', 'location_city', 'location_state', 'age', 'user_type','bio', 'skills')}
  223. ),
  224. )
  225. search_fields = ('email',)
  226. ordering = ('user_type',)
  227. filter_horizontal = ()
  228.  
  229. class JobAdmin(admin.ModelAdmin):
  230. # The forms to add and change user instances
  231. form = JobChangeForm
  232. add_form = JobCreationForm
  233.  
  234. # The fields to be used in displaying the Position model.
  235. # These override the definitions on the base UserAdmin
  236. # that reference specific fields on auth.User.
  237. list_display = ('job_name', 'start_date', 'end_date', 'location_city', 'location_state', 'is_admin')
  238. list_filter = ('is_admin',)
  239. fieldsets = (
  240. (None, {'fields': ('job_name', 'info', 'email')}),
  241. ('Personal info', {'fields': ('location_city', 'location_state', 'start_date', 'end_date', 'parent', 'child', 'skills')}),
  242. ('Permissions', {'fields': ('is_admin',)}),
  243. )
  244. # add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
  245. # overrides get_fieldsets to use this attribute when creating a user.
  246. add_fieldsets = (
  247. (None, {
  248. 'classes': ('wide',),
  249. 'fields': ('job_name', 'email', 'start_date', 'end_date', 'location_city', 'location_state', 'info', 'skills')}
  250. ),
  251. )
  252. search_fields = ('job_name',)
  253. ordering = ('job_name',)
  254. filter_horizontal = ()
  255.  
  256. # Now register
  257. admin.site.register(Skill)
  258. admin.site.register(User, UserAdmin)
  259. admin.site.register(Position, JobAdmin)
  260. # ... and, since we're not using Django's built-in permissions,
  261. # unregister the Group model from admin.
  262. admin.site.unregister(Group)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement