Ejemplo n.º 1
0
def register_page(request):
    #2, we create the instance of the form class with the request method
    register_form = RegisterForm(request.POST or None)
    #3, we create the context
    #4, we check if the form is valid
    if register_form.is_valid():
        #5 we get the cleaned datas
        username = register_form.cleaned_data.get('username')
        email = register_form.cleaned_data.get('email')
        password = register_form.cleaned_data.get('password')
        #6 we use the auth.user model to create the new_user
        new_user = User.objects.create_user(username=username,
                                            email=email,
                                            password=password)
        #7 we get the other data from the form
        fullname = register_form.cleaned_data.get('fullname')
        occupation = register_form.cleaned_data.get('occupation')
        address = register_form.cleaned_data.get('address')
        date_of_birth = register_form.cleaned_data.get('date_of_birth')
        #8 we store the data into the User Profile model, while inheriting for the auth.user
        new_user1 = UserProfile(user=new_user,
                                fullname=fullname,
                                occupation=occupation,
                                address=address,
                                date_of_birth=date_of_birth)
        #9 we save the updated user
        new_user1.save()
        #10 we check to make sure user exist
        if new_user1 is not None:
            #11 if user exist log in user
            login(request, new_user)
            #12 redirect user to home page
        return redirect('/')
    return render(request, 'register_page.html',
                  {'register_form': register_form})
Ejemplo n.º 2
0
def register_view(request):
    if request.method == "POST":
        # get all required fields
        username = request.POST.get("login", None)
        password = request.POST.get("password", None)
        mail = request.POST.get("email", None)
        # if no one of the required fields
        if not username or not password or not mail:
            return render(
                request, "articles/register.html", {"msg": "Укажите все необходимые данные (обозначенные (*))"}
            )
        # validate e-mail address
        if not validate_mail(mail):
            return render(request, "articles/register.html", {"msg": "Недопустимый адрес электронной почты"})
        # get non-required fields
        name = request.POST.get("name", None)
        second_name = request.POST.get("surname", None)
        profile_picture = request.FILES.get("image", None)
        gender = request.POST.get("gender", None)
        # create user
        u = User(username=username, password=password, email=mail)
        # add non-required fields if any
        if name:
            u.first_name = name
        if second_name:
            u.last_name = second_name
        # encrypt and set password
        u.set_password(password)
        u.last_login = timezone.now()
        # try to save
        try:
            u.save()
        # if user already exists
        except IntegrityError:
            return render(request, "articles/register.html", {"msg": "Такой пользователь уже существует!"})
        # create UserProfile instance
        profile = UserProfile(user=u)
        # if user uploaded profile picture
        if profile_picture:
            profile.picture = profile_picture
        # if user specified gender
        if gender:
            profile.gender = gender
        # save UserProfile instance
        profile.save()
        # redirect to login page
        return HttpResponseRedirect(reverse("articles:register_success"))
    # if GET request
    return render(request, "articles/register.html")
Ejemplo n.º 3
0
def gen_auth_key(sender, instance, created, **kwargs):
    user=instance
    
    if created:
        UserProfile.objects.create(user=user)
      
    profile = user.get_profile()
    if not profile:
        profile = UserProfile()
    
    if not profile.auth_key:
        new_key = random_md5like_hash()

        while UserProfile.objects.filter(auth_key=new_key).count() > 0:
            new_key = random_md5like_hash()
        profile.auth_key = new_key
    
    profile.assoc_social()
    
    profile.save()
Ejemplo n.º 4
0
def create_userprofile_from_cdata(username, email, password, role):
    # returns user, not userprofile
    user = User.objects.create_user(username, email, password)
    profile = UserProfile(user=user, role=role)
    profile.save()
    return user