Advertisement
Guest User

Untitled

a guest
Mar 9th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.99 KB | None | 0 0
  1. from django.db import models
  2. from django.contrib.auth.models import User
  3. from django.contrib.auth.models import (BaseUserManager, AbstractBaseUser)
  4.  
  5.  
  6.  
  7.  
  8.  
  9. class MyUserManager(BaseUserManager):
  10. def create_user(self,ID,Password,Email,Nationality,Mother_Language,Mother_Language2,Wish_Language,Wish_Languge2):
  11. if not ID:
  12. raise ValueError('Please, put your ID')
  13.  
  14. if not Password:
  15. raise ValueError('Please, put your Password')
  16.  
  17. user = self.model(
  18. ID=self.normalize_email(ID),
  19. Password=Password,
  20. Email=Email,
  21. Nationality=Nationality,
  22. Mother_Language=Mother_Language,
  23. Mother_Language2=Mother_Language2,
  24. Wish_Language=Wish_Language,
  25. Wish_Languge2=Wish_Languge2,
  26.  
  27. )
  28.  
  29. user.set_password(password)
  30. user.save(using=self._db)
  31. return user
  32.  
  33. def create_superuser(self,ID,Password,Email,Nationality,Mother_Language,Mother_Language2,Wish_Language,Wish_Languge2):
  34. if not ID:
  35. raise ValueError('Please, put your ID')
  36.  
  37. if not Password:
  38. raise ValueError('Please, put your Password')
  39.  
  40. user = self.create_user(
  41. ID=self.normalize_email(ID),
  42. Password=Password,
  43. Email=Email,
  44. Nationality=Nationality,
  45. Mother_Language=Mother_Language,
  46. Mother_Language2=Mother_Language2,
  47. Wish_Language=Wish_Language,
  48. Wish_Languge2=Wish_Languge2,
  49.  
  50. )
  51. user.is_admin = True
  52. user.save(using=self._db)
  53. return user
  54.  
  55.  
  56. class MyUser(AbstractBaseUser):
  57. ID = models.CharField(
  58. verbose_name='ID',
  59. max_length=7,
  60. unique=True,
  61. )
  62. Password = models.CharField(max_length=17,null=False)
  63.  
  64. email = models.EmailField(max_length = 128, unique = True, null = False)
  65.  
  66. nationality = models.CharField(max_length = 128, null = False)
  67. Mother_Language = models.CharField(max_length = 128, null = False)
  68. Mother_Language2 = models.CharField(max_length = 128)
  69. Wish_Languge = models.CharField(max_length = 128, null = False)
  70. Wish_Languge2 = models.CharField(max_length = 128)
  71.  
  72.  
  73.  
  74. is_active = models.BooleanField(default=True)
  75. is_admin = models.BooleanField(default=False)
  76.  
  77. objects = MyUserManager()
  78.  
  79. USERNAME_FIELD = 'ID'
  80. REQUIRED_FIELDS = ['Password','Email','Nationality','Mother_Language','Mother_Language2','Wish_Language','Wish_Languge2']
  81.  
  82. def get_full_name(self):
  83. # The user is identified by their email address
  84. return self.ID
  85.  
  86. def get_short_name(self):
  87. # The user is identified by their email address
  88. return self.ID
  89.  
  90. def __str__(self): # __unicode__ on Python 2
  91. return self.ID
  92.  
  93. def has_perm(self, perm, obj=None):
  94. "Does the user have a specific permission?"
  95. # Simplest possible answer: Yes, always
  96. return True
  97.  
  98. def has_module_perms(self, app_label):
  99. "Does the user have permissions to view the app `app_label`?"
  100. # Simplest possible answer: Yes, always
  101. return True
  102.  
  103. @property
  104. def is_staff(self):
  105. "Is the user a member of staff?"
  106. # Simplest possible answer: All admins are staff
  107. return self.is_admin
  108.  
  109. from django.contrib import admin
  110. from django import forms
  111. from simplemathcaptcha.fields import MathCaptchaField
  112. from django.contrib import admin
  113. from django.contrib.auth.models import Group
  114. from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
  115. from django.contrib.auth.forms import ReadOnlyPasswordHashField
  116. from .models import MyUser
  117.  
  118.  
  119.  
  120. class UserCreationForm(forms.ModelForm):
  121. password1 = forms.CharField(label='Password', widget=forms.PasswordInput(attrs={'class':'form-control','placeholder':'Password'}))
  122. password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput(attrs={'class':'form-control','placeholder':'confirm password'}))
  123. #captcha = MathCaptchaField()
  124. class Meta:
  125. model = MyUser
  126. fields = ('ID','Password','Email','Nationality','Mother_Language','Mother_Language2','Wish_Language','Wish_Languge2')
  127. def __init__(self, *args, **kwargs):
  128. super(UserCreationForm,self).__init__(*args,**kwargs)
  129. self.fields['ID'].widget.attrs={'class':'form-control','placeholder':'ID'}
  130. self.fields['Password'].widget.attrs={'class':'form-control','placeholder':'Password'}
  131. self.fields['Nationality'].widget.attrs={'class':'form-control','placeholder':'Nationality'}
  132. self.fields['Mother_Language'].widget.attrs={'class':'form-control','placeholder':'Mother_Language'}
  133. self.fields['Mother_Language2'].widget.attrs={'class':'form-control','placeholder':'Mother_Language2'}
  134. self.fields['Wish_Language'].widget.attrs={'class':'form-control','placeholder':'Email'}
  135. self.fields['Wish_Language2'].widget.attrs={'class':'form-control','placeholder':'Wish_Language2'}
  136.  
  137. def clean_password2(self):
  138. password1 = self.cleaned_data.get("password1")
  139. password2 = self.cleaned_data.get("password2")
  140. if password1 and password2 and password1 != password2:
  141. raise forms.ValidationError("Passwords don't match")
  142. return password2
  143. def save(self, commit=True):
  144. user = super(UserCreationForm, self).save(commit=False)
  145. user.set_password(self.cleaned_data["password1"])
  146. if commit:
  147. user.save()
  148. return user
  149.  
  150.  
  151. class UserAdmin(BaseUserAdmin):
  152. # The forms to add and change user instances
  153. add_form = UserCreationForm
  154. list_display = ('ID','Password','Email','Nationality','Mother_Language','Mother_Language2','Wish_Language','Wish_Languge2', 'is_admin')
  155. list_filter = ('is_admin',)
  156. fieldsets = (
  157. (None, {'fields': ('email', 'password')}),
  158. ('Personal info', {'fields': ('Email','Nationality','Mother_Language','Mother_Language2','Wish_Language','Wish_Languge2',)}),
  159. ('Permissions', {'fields': ('is_admin',)}),
  160. )
  161. add_fieldsets = (
  162. (None, {
  163. 'classes': ('wide',),
  164. 'fields': ('ID','Password','Email','Nationality','Mother_Language','Mother_Language2','Wish_Language','Wish_Languge2',)}
  165. ),
  166. )
  167. search_fields = ('ID',)
  168. ordering = ('ID',)
  169. filter_horizontal = ()
  170.  
  171. admin.site.register(MyUser, UserAdmin)
  172. admin.site.unregister(Group)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement