Ejemplo n.º 1
0
def signup(request):
    if request.method == 'POST':
        form = SignupForm(request.POST)
        if form.is_valid():
            user = form.save(commit=False)
            user.is_active = False
            user.save()
            current_site = get_current_site(request)
            mail_subject = 'Activate your blog account.'
            message = render_to_string(
                'authentication/acc_active_email.html', {
                    'user': user,
                    'domain': current_site.domain,
                    'uid': urlsafe_base64_encode(force_bytes(
                        user.pk)).decode(),
                    'token': account_activation_token.make_token(user),
                })
            to_email = form.cleaned_data.get('email')
            email = EmailMessage(mail_subject, message, to=[to_email])
            email.send()
            return HttpResponse(
                'Please confirm your email address to complete the registration'
            )
    else:
        form = SignupForm()
    return render(request, 'authentication/signup.html', {'form': form})
Ejemplo n.º 2
0
def signup(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if not form.is_valid():
            return render(request, 'authentication/signup.html',
                          {'form': form})

        else:
            user = form.save(commit=False)
            email = form.cleaned_data.get('email')
            password = form.cleaned_data.get('password')
            user.set_password(password)
            user.is_active = False
            user.save()

            current_site = get_current_site(request)
            subject = 'Activate Your Onlinebookshare Account'
            message = render_to_string('authentication/account_activation_email.html', {
                'user': user,
                'domain': current_site.domain,
                'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                'token': account_activation_token.make_token(user),
            })
            send_mail(subject, message, '*****@*****.**', [email])

            return redirect('account_activation_sent')

    else:
        return render(request, 'authentication/signup.html',
                      {'form': SignUpForm()})
Ejemplo n.º 3
0
def register_user(request):
    form = Registration()
    if request.method == 'POST':
        form = Registration(request.POST)
        if form.is_valid():
            user = form.save(commit=False)
            user.set_password(form.cleaned_data['password'])
            user.is_active = False
            user.save()

            ut = Usertype.objects.create(user=user, is_user=True)
            ut.save()
            profile = Profile.objects.create(profile=ut)
            profile.save()

            current_site = get_current_site(request)
            mail_subject = 'Activate your account.'
            message = render_to_string(
                'authentication/activate_email.html', {
                    'user': user,
                    'domain': current_site.domain,
                    'uid': urlsafe_base64_encode(force_bytes(
                        user.pk)).decode(),
                    'token': account_activation_token.make_token(user),
                })
            to_email = form.cleaned_data.get('email')
            email = EmailMessage(mail_subject, message, to=[to_email])
            email.send()
            return HttpResponse(
                'Please confirm your email address to complete the registration'
            )

    return render(request, 'authentication/register.html', {'form': form})
def register(request):
    if request.method == 'POST':
        form = CreateUserForm(request.POST)
        if form.is_valid():
            user_email = form.cleaned_data.get('email')
            user = form.save(commit=False)
            user.is_active = False
            user.save()
            current_site = get_current_site(request)
            email_subject = 'Activate your account'
            email_body = render_to_string(
                'auth/email_template.html', {
                    'user': user,
                    'domain': current_site.domain,
                    'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                    'token': account_activation_token.make_token(user),
                })
            email = EmailMessage(
                email_subject,
                email_body,
                '*****@*****.**',
                [user_email],
            )
            email.send(fail_silently=True)
            return HttpResponse(
                'Please confirm your email address to complete the registration'
            )
        else:
            messages.error(request, form.errors)
    else:
        form = CreateUserForm()
    context = {'form': form}
    return render(request, 'auth/register.html', context)
Ejemplo n.º 5
0
def register(request):

    #   if request.user is not None and request.user.is_authenticated:
    #       return render(request,"bookshy/index.html")
    errors = None
    if request.method == 'POST':
        form = SignUpForm(request.POST)

        if form.is_valid() and form.clean_email() and form.clean_password():
            ola = form.save(
                commit=False)  #dont save yet i want to change some params
            ola.is_active = False
            ola.save()  #okay go ahead and save
            current_site = get_current_site(request)
            subject = 'Activate Your MySite Account'
            message = render_to_string(
                'emails/account_activation_email.html', {
                    'user': user,
                    'domain': current_site.domain,
                    'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                    'token': account_activation_token.make_token(user),
                })
            user.email_user(subject, message)

            messages.success(
                request,
                ('Please Confirm your email to complete registration.'))
        errors = form.errors
    form = SignUpForm()
    context = {'form': form, 'errors': errors}

    return render(request, "bookshy/register.html", context)
Ejemplo n.º 6
0
def signup(request):
    if request.method == 'POST':
        form = SignUpForm(data=request.POST)
        if form.is_valid():
            user = form.save(commit=False)
            # Setting as False to prevent login without confirming email
            user.is_active = False
            user.save()
            current_site = get_current_site(request)
            subject = 'Verify your DjangoLogin Account'
            message = render_to_string(
                'authentication/account_activation_email.html', {
                    'user': user,
                    'domain': current_site.domain,
                    'uid': urlsafe_base64_encode(force_bytes(
                        user.pk)).decode(),
                    'token': account_activation_token.make_token(user),
                })
            user.email_user(subject, message)
            print("Mail Done!")
            return redirect(to='authentication:account_activation_sent')
        else:
            return render(request,
                          template_name='authentication/signup.html',
                          context={'form': form})
    else:
        form = SignUpForm()
        return render(request,
                      template_name='authentication/signup.html',
                      context={'form': form})
Ejemplo n.º 7
0
def register_user(request):
    form = Registration()
    form_profile = CreateProfile()
    if request.method == 'POST':
        form = Registration(request.POST)
        form_profile = CreateProfile(request.POST)
        if form.is_valid() and form_profile.is_valid():
            user = form.save(commit=False)
            user.set_password(form.cleaned_data['password'])
            user.is_active = False
            user.save()

            # ut = Usertype.objects.create(user=user, is_user=True)
            # ut.save()
            with connection.cursor() as cursor:
                cursor.execute(
                    'insert into authentication_usertype(user_id, is_user, is_party) values (%s,%s,%s)',
                    [user.pk, True, False])

            # profile = form_profile.save(commit=False)
            # profile.profile = ut
            # profile.save()
            first_name = form_profile.cleaned_data['first_name']
            last_name = form_profile.cleaned_data['last_name']
            phone_num = form_profile.cleaned_data['phone_num']
            location = form_profile.cleaned_data['location']
            gender = form_profile.cleaned_data['gender']
            with connection.cursor() as cursor:
                cursor.execute(
                    'insert into authentication_profile(first_name, last_name, phone_num, location, gender, profile_id) values (%s,%s,%s, %s,%s,%s)',
                    [
                        first_name, last_name, phone_num, location, gender,
                        user.pk
                    ])

            current_site = get_current_site(request)
            mail_subject = 'Activate your account.'
            message = render_to_string(
                'authentication/activate_email.html', {
                    'user': user,
                    'domain': current_site.domain,
                    'uid': urlsafe_base64_encode(force_bytes(
                        user.pk)).decode(),
                    'token': account_activation_token.make_token(user),
                })
            to_email = form.cleaned_data.get('email')
            email = EmailMessage(mail_subject, message, to=[to_email])
            email.send()
            return HttpResponse(
                'Please confirm your email address to complete the registration'
            )
    return render(request, 'authentication/register.html', {
        'form': form,
        'form_profile': form_profile
    })
Ejemplo n.º 8
0
def send_activation_email(request, user):
    current_site = get_current_site(request)
    message = render_to_string(
        'registration/confirm_email.html', {
            'user': user,
            'domain': current_site.domain,
            'uid': urlsafe_base64_encode(force_bytes(user.pk)),
            'token': account_activation_token.make_token(user),
        })
    mail_subject = 'Activate your Daily Inquirer Account'
    to_email = user.email
    email = EmailMessage(mail_subject, message,
                         "Beep Boop <*****@*****.**>", [to_email])
    email.send()
Ejemplo n.º 9
0
 def post(self, request, *args, **kwargs):
     serializer = SignupSerializer(data=request.data)
     if serializer.is_valid(
             raise_exception=True
     ):  #below statement will be executed if serializers data is valid otherwise it will raise exceptions
         email = serializer.validated_data['email']
         phone = serializer.validated_data['phone']
         user_type = serializer.validated_data['user_type']
         first_name = serializer.validated_data['first_name']
         last_name = serializer.validated_data['last_name']
         password = serializer.validated_data['password']
         user = UserModel.objects.create(first_name=first_name,
                                         last_name=last_name,
                                         email=email,
                                         phone=phone,
                                         user_type=user_type,
                                         is_active=False)
         try:
             self.validate_password(password)
         except ValidationError as error:
             raise exceptions.ValidationError(error)
         user.set_password(password)
         user.save()
         #sending mail to user mail account
         context = {
             'user': user,
             'domain': '127.0.0.1:8000',
             'uid': urlsafe_base64_encode(force_bytes(user.pk)),
             'token': account_activation_token.make_token(user),
         }
         mail_subject = 'Activate your Accout'
         message = render_to_string('verification.html', context)
         email_plaintext_message = render_to_string('verification.txt',
                                                    context)
         to_email = email
         success = emailing.EmailThread(mail_subject, message, context, [
             email,
         ]).start()
         return Response(
             {
                 'data': serializer.data,
                 'success': 'User registered sucessfully'
             },
             status=status.HTTP_201_CREATED)
Ejemplo n.º 10
0
def email_signup(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            user = form.save(commit=False)
            user.is_active = False
            user.save()
            # user.refresh_from_db()

            current_site = get_current_site(request)
            subject = 'Activate Your Fleet Manager Account'
            message = render_to_string('account_activation_email.html', {
                'user': user,
                'domain': current_site.domain,
                # 'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                'uid': str(user.pk),
                'token': account_activation_token.make_token(user)
            })
            user.email_user(subject, message)
            return redirect('account_activation_sent')
    else:
        form = SignUpForm()
    return render(request, 'signup.html', {'form': form})
Ejemplo n.º 11
0
    def post(self, request, *args, **kwargs):
        form = self.form_class(request.POST)
        if form.is_valid():

            user = form.save(commit=False)
            user.is_active = False # Deactivate account till it is confirmed
            user.save()

            current_site = get_current_site(request)
            print(current_site)
            subject = 'Activate Your Example Blog Account'
            message = render_to_string('emails/account_activation_email.html', {
                'user': user,
                'domain': current_site.domain,
                'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                'token': account_activation_token.make_token(user),
            })
            user.email_user(subject, message)

            messages.success(request, ('Please Confirm your email to complete registration.'))

            return redirect('login')

        return render(request, self.template_name, {'form': form})
Ejemplo n.º 12
0
def sign_up_view(request):
    if request.user.is_authenticated and request.user.is_school:
        return redirect(reverse('schoolPortal:dashboard'))
    elif request.user.is_authenticated and request.user.is_admin:
        return redirect(reverse('adminPortal:dashboard'))
    elif request.user.is_authenticated and request.user.is_professional:
        return redirect(reverse('profPortal:dashboard'))
    else:
        login_form = ''

        if request.method == 'POST' and 'is_school' in request.POST:
            form = SignUp(request.POST)
            if form.is_valid():
                user = form.save(commit=False)
                user.is_active = False
                user.set_password(user.password)
                user.save()
                current_site = get_current_site(request)
                subject = 'Account Activation Link'
                message = render_to_string(
                    'auth/account_activation_email.html', {
                        'user': user,
                        'domain': current_site.domain,
                        'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                        'token': account_activation_token.make_token(user),
                    })
                user.email_user(subject, message)
                return redirect('Auth:account_activation_sent')
        else:
            form = SignUp()

        if request.method == 'POST' and 'is_professional' in request.POST:
            prof_form = ProfSignUp(request.POST)
            # print(prof_form)
            print(prof_form.errors)
            if prof_form.is_valid():
                user = prof_form.save(commit=False)
                user.is_active = False
                print(user.programme)
                if user.programme == 'Dental Therapist':
                    user.code = 'RDTH' + user.code
                elif user.programme == 'Dental Nurses':
                    user.code = 'RDSN' + user.code
                elif user.programme == 'Dental Surgery Assistant':
                    user.code = 'RDSA' + user.code
                elif user.programme == 'Dental Surgery Technician':
                    user.code = 'RDST' + user.code
                user.username = user.code
                # elif user.programme is 'Dental Nurses':
                #     user.code = Rdsn''user.code
                user.set_password(user.password)
                user.save()
                current_site = get_current_site(request)
                subject = 'Account Activation Link'
                message = render_to_string(
                    'auth/account_activation_email.html', {
                        'user': user,
                        'domain': current_site.domain,
                        'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                        'token': account_activation_token.make_token(user),
                    })
                user.email_user(subject, message)
                return redirect('Auth:account_activation_sent')
            # else:
            #     print('The form isn\'t valid')
        else:
            prof_form = ProfSignUp()

        if request.method == 'POST' and request.POST.get(
                'loginSubmit') == 'login_all':
            login_form = LoginForm(request.POST)
            print('login_form')
            if login_form.is_valid():
                email = login_form.cleaned_data.get('email')
                password = login_form.cleaned_data.get('password')
                try:
                    get_user_name = User.objects.get(email=email)
                    print(get_user_name)
                    user = authenticate(username=get_user_name,
                                        password=password)
                    if user and user.is_active and user.suspend is False and user.block is False:
                        print('Valid')

                        login(request, user)
                        print(user.email)
                        if User.objects.filter(email=user.email,
                                               is_school=True,
                                               profile_update=False):
                            sweetify.success(request,
                                             'Login Successful',
                                             button='Great!')
                            return redirect("schoolPortal:schoolProfile")

                        elif User.objects.filter(email=user.email,
                                                 is_school=True,
                                                 profile_update=True):
                            sweetify.success(request,
                                             'Login Successful',
                                             button='Great!')
                            print('This is working  ')
                            return redirect("schoolPortal:dashboard")

                        elif User.objects.filter(email=user.email,
                                                 is_professional=True):
                            sweetify.success(request,
                                             'Login Successful',
                                             button='Great!')
                            return redirect("profPortal:dashboard")

                        elif User.objects.filter(email=user.email,
                                                 is_admin=True):
                            print('Admin is true')
                            sweetify.success(request,
                                             'Login Successful',
                                             button='Great!')
                            return redirect('adminPortal:dashboard')

                    elif user and user.is_active and user.suspend is True and user.block is False:
                        sweetify.error(
                            request,
                            'School Has Been Suspended',
                            text='Contact the board for more details',
                            button='Great!')
                        return HttpResponseRedirect(reverse("Auth:Register"))

                    elif user and user.is_active and user.suspend is False and user.block is True:
                        sweetify.error(
                            request,
                            'School Has Been Blocked',
                            text='Contact the board for more details',
                            button='Great!')
                        return HttpResponseRedirect(reverse("Auth:Register"))
                    else:
                        sweetify.error(request, 'Invalid Username or Password')

                        return HttpResponseRedirect(reverse("Auth:Register"))
                except:
                    sweetify.success(request, 'Invalid Username or Password')
                    return HttpResponseRedirect(reverse("Auth:Register"))
            else:
                login_form = LoginForm()
                print(login_form.errors)
                print("Login Not Valid")
        return render(request, 'auth/sch_register.html', {
            'form': form,
            'prof_form': prof_form,
            'login_form': login_form
        })
Ejemplo n.º 13
0
    def post(self, request, *args, **kwargs):
        form = CompanyForm(self.request.POST)
        # if the form is valid we need to grab all information from it
        if form.is_valid():
            company_name = form.cleaned_data.get('company_name')
            company_email = form.cleaned_data.get('company_email')
            country = form.cleaned_data.get('country')
            password1 = form.cleaned_data.get('password2')
            password2 = form.cleaned_data.get('password2')
            agreed = request.POST.get('agreed')
            # checking for valid email address
            if not validate_email(company_email):
                messages.error(request, 'enter valid email address')
                return redirect('sign_in')

            # trying to check if company details that is being registered is exists
            if not (Company.objects.filter(company_name=company_name).exists()
                    and Company.objects.filter(
                        company_email=company_email).exists()):
                if not (User.objects.filter(email=company_email).exists() and
                        User.objects.filter(username=company_name).exists()):
                    if agreed:
                        if password1 == password2:
                            new_company = Company()
                            new_company.company_name = company_name
                            new_company.country = country
                            new_company.company_email = company_email
                            new_company.password1 = get_hashed_password(
                                password1)
                            new_company.agreed_to_terms = True
                            new_company.save()

                            # authenticate the company so that to able to login in the future

                            User.objects.create_user(company_email,
                                                     password1,
                                                     username=company_name,
                                                     is_active=False)
                            user = User.objects.get(username=company_name,
                                                    email=company_email)
                            # TODO send email address to login user
                            """ Here i can decide to  tell the user to login or send Email for account activation"""
                            current_site = get_current_site(request)
                            mail_subject = 'Activate your account now'
                            message = render_to_string(
                                'authentication/account_activate_email.html', {
                                    'user':
                                    user,
                                    'domain':
                                    current_site.domain,
                                    'uid':
                                    urlsafe_base64_encode(force_bytes(
                                        user.pk)),
                                    'token':
                                    account_activation_token.make_token(user)
                                })
                            to_email = user.email
                            email_to_send = EmailMessage(mail_subject,
                                                         message,
                                                         to=[to_email])
                            email_to_send.send()

                            messages.info(
                                self.request,
                                f" activate your account at {company_email[3:]}"
                            )
                            return redirect('sign_in')
                        else:
                            messages.info(self.request,
                                          "password doesn't match")
                            return redirect('sign_in')

                    else:
                        messages.info(self.request,
                                      'Agree to terms and condition')
                        return redirect('sign_in')
                else:

                    messages.warning(
                        request,
                        'company name or email address is already taken')
                    return redirect('sign_in')

            else:
                messages.warning(
                    request, 'company name or email address is already taken')
                return redirect('sign_in')

        messages.warning(request, 'Try to fill all information')
        return redirect('sign_in')