Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from django.db import models
- from django.contrib.auth.models import User
- from django.contrib.auth.models import (BaseUserManager, AbstractBaseUser)
- class MyUserManager(BaseUserManager):
- def create_user(self,ID,Password,Email,Nationality,Mother_Language,Mother_Language2,Wish_Language,Wish_Languge2):
- if not ID:
- raise ValueError('Please, put your ID')
- if not Password:
- raise ValueError('Please, put your Password')
- user = self.model(
- ID=self.normalize_email(ID),
- Password=Password,
- Email=Email,
- Nationality=Nationality,
- Mother_Language=Mother_Language,
- Mother_Language2=Mother_Language2,
- Wish_Language=Wish_Language,
- Wish_Languge2=Wish_Languge2,
- )
- user.set_password(password)
- user.save(using=self._db)
- return user
- def create_superuser(self,ID,Password,Email,Nationality,Mother_Language,Mother_Language2,Wish_Language,Wish_Languge2):
- if not ID:
- raise ValueError('Please, put your ID')
- if not Password:
- raise ValueError('Please, put your Password')
- user = self.create_user(
- ID=self.normalize_email(ID),
- Password=Password,
- Email=Email,
- Nationality=Nationality,
- Mother_Language=Mother_Language,
- Mother_Language2=Mother_Language2,
- Wish_Language=Wish_Language,
- Wish_Languge2=Wish_Languge2,
- )
- user.is_admin = True
- user.save(using=self._db)
- return user
- class MyUser(AbstractBaseUser):
- ID = models.CharField(
- verbose_name='ID',
- max_length=7,
- unique=True,
- )
- Password = models.CharField(max_length=17,null=False)
- email = models.EmailField(max_length = 128, unique = True, null = False)
- nationality = models.CharField(max_length = 128, null = False)
- Mother_Language = models.CharField(max_length = 128, null = False)
- Mother_Language2 = models.CharField(max_length = 128)
- Wish_Languge = models.CharField(max_length = 128, null = False)
- Wish_Languge2 = models.CharField(max_length = 128)
- is_active = models.BooleanField(default=True)
- is_admin = models.BooleanField(default=False)
- objects = MyUserManager()
- USERNAME_FIELD = 'ID'
- REQUIRED_FIELDS = ['Password','Email','Nationality','Mother_Language','Mother_Language2','Wish_Language','Wish_Languge2']
- def get_full_name(self):
- # The user is identified by their email address
- return self.ID
- def get_short_name(self):
- # The user is identified by their email address
- return self.ID
- def __str__(self): # __unicode__ on Python 2
- return self.ID
- def has_perm(self, perm, obj=None):
- "Does the user have a specific permission?"
- # Simplest possible answer: Yes, always
- return True
- def has_module_perms(self, app_label):
- "Does the user have permissions to view the app `app_label`?"
- # Simplest possible answer: Yes, always
- return True
- @property
- def is_staff(self):
- "Is the user a member of staff?"
- # Simplest possible answer: All admins are staff
- return self.is_admin
- from django.contrib import admin
- from django import forms
- from simplemathcaptcha.fields import MathCaptchaField
- from django.contrib import admin
- from django.contrib.auth.models import Group
- from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
- from django.contrib.auth.forms import ReadOnlyPasswordHashField
- from .models import MyUser
- class UserCreationForm(forms.ModelForm):
- password1 = forms.CharField(label='Password', widget=forms.PasswordInput(attrs={'class':'form-control','placeholder':'Password'}))
- password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput(attrs={'class':'form-control','placeholder':'confirm password'}))
- #captcha = MathCaptchaField()
- class Meta:
- model = MyUser
- fields = ('ID','Password','Email','Nationality','Mother_Language','Mother_Language2','Wish_Language','Wish_Languge2')
- def __init__(self, *args, **kwargs):
- super(UserCreationForm,self).__init__(*args,**kwargs)
- self.fields['ID'].widget.attrs={'class':'form-control','placeholder':'ID'}
- self.fields['Password'].widget.attrs={'class':'form-control','placeholder':'Password'}
- self.fields['Nationality'].widget.attrs={'class':'form-control','placeholder':'Nationality'}
- self.fields['Mother_Language'].widget.attrs={'class':'form-control','placeholder':'Mother_Language'}
- self.fields['Mother_Language2'].widget.attrs={'class':'form-control','placeholder':'Mother_Language2'}
- self.fields['Wish_Language'].widget.attrs={'class':'form-control','placeholder':'Email'}
- self.fields['Wish_Language2'].widget.attrs={'class':'form-control','placeholder':'Wish_Language2'}
- def clean_password2(self):
- password1 = self.cleaned_data.get("password1")
- password2 = self.cleaned_data.get("password2")
- if password1 and password2 and password1 != password2:
- raise forms.ValidationError("Passwords don't match")
- return password2
- def save(self, commit=True):
- user = super(UserCreationForm, self).save(commit=False)
- user.set_password(self.cleaned_data["password1"])
- if commit:
- user.save()
- return user
- class UserAdmin(BaseUserAdmin):
- # The forms to add and change user instances
- add_form = UserCreationForm
- list_display = ('ID','Password','Email','Nationality','Mother_Language','Mother_Language2','Wish_Language','Wish_Languge2', 'is_admin')
- list_filter = ('is_admin',)
- fieldsets = (
- (None, {'fields': ('email', 'password')}),
- ('Personal info', {'fields': ('Email','Nationality','Mother_Language','Mother_Language2','Wish_Language','Wish_Languge2',)}),
- ('Permissions', {'fields': ('is_admin',)}),
- )
- add_fieldsets = (
- (None, {
- 'classes': ('wide',),
- 'fields': ('ID','Password','Email','Nationality','Mother_Language','Mother_Language2','Wish_Language','Wish_Languge2',)}
- ),
- )
- search_fields = ('ID',)
- ordering = ('ID',)
- filter_horizontal = ()
- admin.site.register(MyUser, UserAdmin)
- admin.site.unregister(Group)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement