def get(self, request, id=None): if request.user.is_authenticated: if request.user.is_superuser: if id == None: form = FormAdminData(instance=request.user) else: pi = User.objects.get(pk=id) form = FormAdminData(instance=pi) users = User.objects.all() else: form = FormUserData(instance=request.user) users = None fullname = self.request.user.get_full_name() groups = Group.objects.filter(user=self.request.user) return render( request, 'registration/profile.html', { 'form': form, 'user': self.request.user, 'users': users, 'groups': groups, 'fullname': fullname }) else: messages.info(request, 'you need to login in order to see your profile') return HttpResponseRedirect('/userlogin/')
def get(self, request): if not request.user.is_authenticated: form = FormRegister() return render(request, 'registration/register.html', {'form': form}) else: messages.info(request, 'you are already registered in') return HttpResponseRedirect('/')
def post(self, request): form = RegistrationForm(request.POST) if form.is_valid(): form.save() else: messages.error(request, 'Registration fail..') return render(request, 'accounts/registrations.html', {'form': form}) messages.info(request, 'Registration successfully..') return redirect('accounts:login')
def post(self, request): form = FormRegister(request.POST) if form.is_valid: user = form.save() group = Group.objects.get_or_create( name='manager') # get group if exists else creates group content_type = ContentType.objects.get(app_label='blogpost', model='modelpost') perms = Permission.objects.filter(content_type=content_type) group = Group.objects.get(name='manager') for p in perms: group.permissions.add(p) # add all model permission to group user.groups.add( group ) # adds user(form.save) to group(group.object.get(name='manager')) messages.info( request, 'you have been successfully registerd, and aded to manager group. plz login now' ) return HttpResponseRedirect('/userlogin/')
def post(self, request, id=None): if request.user.is_superuser: if id == None: form = FormAdminData(data=request.POST, instance=request.user) else: pi = User.objects.get(pk=id) form = FormAdminData(data=request.POST, instance=pi) else: form = FormUserData(data=request.POST, instance=request.user) if form.is_valid(): messages.info(request, 'saved successfully !') form.save() fullname = self.request.user.get_full_name() groups = Group.objects.filter(user=self.request.user) return render( request, 'registration/profile.html', { 'form': form, 'user': self.request.user, 'groups': groups, 'fullname': fullname })
def get(self, request, key=None, *args, **kwargs): if key is not None: qs = EmailActivation.objects.filter(key__iexact=key) confirm_qs = qs.conformable() if confirm_qs.count() == 1: obj = confirm_qs.first() obj.activate() messages.success( self.request, "Your email has been confirmed. You can login now.") return redirect('login') else: activated_qs = qs.filter(key__iexact=key, activated=True) if activated_qs.exists(): reset_link = reverse('password_reset') msg = """Your email has already confirmed! Did you mean <a href="{link}">reset your password</a>? """.format(link=reset_link) messages.info(self.request, mark_safe(msg)) return redirect('login') context = {'form': self.get_form(), 'key': self.key} return render(self.request, 'registration/activation_error.html', context)
def clean(self): data = self.cleaned_data email = data.get('email') password = data.get('password') qs = User.objects.filter(email=email) if qs.exists(): # user email is registered, check email is active or email activation not_active = qs.filter(is_active=False) if not_active.exists(): # check email activation confirm_email = EmailActivation.objects.filter(email=email) is_conformable = confirm_email.conformable().exists() if is_conformable: msg = messages.info( self.request, "Please check your email and confirm activation.") raise forms.ValidationError(msg) email_confirm_exists = EmailActivation.objects.email_exists( email).exists() if email_confirm_exists: msg = messages.info( self.request, "Please resend your conformation email.") raise forms.ValidationError(msg) if not is_conformable and not email_confirm_exists: msg = messages.error(self.request, "This user is not active!") raise forms.ValidationError(msg) user = authenticate(self.request, email=email, password=password) if user is None: msg = messages.error(self.request, "Invalid credentials.") raise forms.ValidationError(msg) login(self.request, user) self.user = user user_logged_in.send(user.__class__, instance=user, request=self.request) return data