def handle(self, *args, **kwargs): Role.objects.all().delete() User.objects.all().delete() username = '******' user = User.objects.create_user(username=username, password='******', email='*****@*****.**', first_name='Admin', last_name='Example') user.is_superuser = False user.is_staff = True user.save() role = Role(user_id=user.id, is_admin=True) role.save() for i in range(5): fake = Faker() name = fake.first_name() user = User.objects.create_user(username=name.lower(), password='******', email=fake.email(), first_name=name, last_name=fake.last_name()) user.is_superuser = False user.is_staff = False user.save() role = Role(user_id=user.id, is_admin=False) role.save() self.stdout.write('Added new user')
def handle(self, *args, **kwargs): normal_role = Role.objects.filter(name='normal').first() if not normal_role: normal_role = Role(name='normal') normal_role.save() normal_role.permissions = NORMAL_USER_PERMISSIONS normal_role.save()
def create(self, validated_data): user = User(email=validated_data['email'], username=validated_data['username'], first_name=validated_data['first_name'], last_name=validated_data['last_name']) user.set_password(validated_data['password']) user.save() role = Role(user_id=user.id, is_admin=False) role.save() return user
def create(request): if request.method == 'POST': form = UserRegisterForm(request.POST) if form.is_valid(): form.save() messages.success(request, 'User registration by admin is successfully!') role = Role(user_id=User.objects.latest('id').id, role=request.POST.get('role')) role.save() return redirect('setting-index') return render(request, 'user/add.html', {'title': 'New User'})
def create_role(request): if len(request.POST['firstname']) < 5: return JsonResponse({'failure': 'Name of Role error'}) try: if 0 < int(request.POST['permission']) < 7: pass else: return JsonResponse({'failure': 'You must select permission'}) except: return JsonResponse({'failure': 'You must select permission'}) if request.POST['description'] == '': return JsonResponse({'failure': 'Description can not be blank'}) new_role = Role(Name=request.POST['firstname'], Permission=request.POST['permission'], Description=request.POST['description']) new_role.save() if new_role: return JsonResponse({'success': 'Create successfully a new Role'}) else: return JsonResponse({'failure': 'Create un-successfully a new Role'})