Advertisement
Guest User

Untitled

a guest
Jul 17th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1. from django.contrib.auth.tests.custom_user import CustomUserManager
  2. from django.db import models
  3. from django.utils import timezone
  4. from django.utils.http import urlquote
  5. from django.utils.translation import ugettext_lazy as _
  6. from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
  7. from crm import settings
  8.  
  9.  
  10. class FreelancerManager(BaseUserManager):
  11. def create_user(self, name, skills, password=None):
  12. if not name:
  13. raise ValueError('Users must have a unique name ')
  14.  
  15. user = self.model(
  16. name=self.name,
  17. skills=skills,
  18. )
  19. user.set_password(password)
  20. user.save(using=self._db)
  21. return user
  22.  
  23. def create_superuser(self, name, skills, password):
  24. """
  25. Creates and saves a superuser with the given email, date of
  26. birth and password.
  27. """
  28. user = self.create_user(
  29. name,
  30. password=password,
  31. skills=skills,
  32. )
  33. user.is_admin = True
  34. user.save(using=self._db)
  35. return user
  36.  
  37.  
  38. class Freelancer(AbstractBaseUser, PermissionsMixin):
  39. name = models.CharField(verbose_name='name',
  40. max_length=20,
  41. unique=True, )
  42.  
  43. field_of_interest = models.CharField(max_length=200)
  44. skills = models.TextField()
  45. experience = models.TextField()
  46.  
  47. is_active = models.BooleanField(default=True)
  48. is_admin = models.BooleanField(default=False)
  49.  
  50. objects = FreelancerManager()
  51.  
  52. USERNAME_FIELD = 'name'
  53. REQUIRED_FIELDS = ['skills']
  54.  
  55. class Meta:
  56. db_table = 'auth_user'
  57. verbose_name = _('user')
  58. verbose_name_plural = _('users')
  59.  
  60. def get_absolute_url(self):
  61. return "/users/%s/" % urlquote(self.name)
  62.  
  63. def get_short_name(self):
  64. return self.name
  65.  
  66. def get_full_name(self):
  67. return self.name
  68.  
  69. def __str__(self): # __unicode__ on Python 2
  70. return self.name
  71.  
  72. def has_perm(self, perm, obj=None):
  73. # "Does the user have a specific permission?"
  74. # # Simplest possible answer: Yes, always
  75. return True
  76.  
  77. def has_module_perms(self, applabel):
  78. # "Does the user have permissions to view the app `app_label`?"
  79. # Simplest possible answer: Yes, always
  80. return True
  81.  
  82. @property
  83. def is_staff(self):
  84. # "Is the user a member of staff?"
  85. # "Simplest possible answer: All admins are staf"
  86. return self.is_admin
  87.  
  88. from django import forms
  89. from django.contrib import admin
  90. from django.contrib.auth.models import Group
  91. from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
  92. from django.contrib.auth.forms import ReadOnlyPasswordHashField
  93. from .models import Freelancer
  94.  
  95.  
  96. class UserCreationForm(forms.ModelForm):
  97. """A form for creating new users. Includes all the required
  98. fields, plus a repeated password."""
  99. password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
  100. password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
  101.  
  102. class Meta:
  103. model = Freelancer
  104. fields = ('name', 'skills')
  105.  
  106. def clean_password2(self):
  107. # Check that the two password entries match
  108. password1 = self.cleaned_data.get("password1")
  109. password2 = self.cleaned_data.get("password2")
  110. if password1 and password2 and password1 != password2:
  111. raise forms.ValidationError("Passwords don't match")
  112. return password2
  113.  
  114. def save(self, commit=True):
  115. # Save the provided password in hashed format
  116. user = super(UserCreationForm, self).save(commit=False)
  117. user.set_password(self.cleaned_data["password1"])
  118. if commit:
  119. user.save()
  120. return user
  121.  
  122.  
  123. class UserChangeForm(forms.ModelForm):
  124. """A form for updating users. Includes all the fields on
  125. the user, but replaces the password field with admin's
  126. password hash display field.
  127. """
  128. password = ReadOnlyPasswordHashField()
  129.  
  130. class Meta:
  131. model = Freelancer
  132. fields = ('name', 'password', 'skills','is_admin')
  133.  
  134. def clean_password(self):
  135. # Regardless of what the user provides, return the initial value.
  136. # This is done here, rather than on the field, because the
  137. # field does not have access to the initial value
  138. return self.initial["password"]
  139.  
  140.  
  141. class FreelancerAdmin(BaseUserAdmin):
  142. # The forms to add and change user instances
  143. form = UserChangeForm
  144. add_form = UserCreationForm
  145.  
  146. # The fields to be used in displaying the User model.
  147. # These override the definitions on the base UserAdmin
  148. # that reference specific fields on auth.User.
  149. list_display = ('name', 'skills', 'is_admin')
  150. list_filter = ('is_admin',)
  151. fieldsets = (
  152. (None, {'fields': ('name', 'password')}),
  153. ('Personal info', {'fields': ('skills',)}),
  154. ('Permissions', {'fields': ('is_admin',)}),
  155. )
  156. # add_fieldsets is not a standard ModelAdmin attribute. UserAdmin
  157. # overrides get_fieldsets to use this attribute when creating a user.
  158. add_fieldsets = (
  159. (None, {
  160. 'classes': ('wide',),
  161. 'fields': ('name', 'skills', 'password1', 'password2')}
  162. ),
  163. )
  164. search_fields = ('name',)
  165. ordering = ('name',)
  166. filter_horizontal = ()
  167.  
  168.  
  169. # Now register the new UserAdmin...
  170. admin.site.register(Freelancer, FreelancerAdmin)
  171. admin.site.unregister(Group)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement