Esempio n. 1
0
def register(request):
    if request.method == "POST":

        # Get the user data from POST
        username = request.POST.get("username")
        email = str(request.POST.get("email")).lower()
        # Encode the plain password into bytes
        password = str(request.POST.get("password")).encode('utf-8')

        # Hash the password with salt
        password_hashed = bcrypt.hashpw(password, bcrypt.gensalt())

        # Check if email has already been registered
        if not User.email_already_exists(email):
            # Get password back into str before storing it in database
            password_hashed = password_hashed.decode('utf-8')
            user_inserted = User.insert(username, email, password_hashed)
            # Insert a default picture that the user sees
            link = "https://photos.alexfreyr.com/profile/default-profile.png"
            alt = "Default profile picture for user"
            UserPhoto.insert(user_inserted, link, alt)

            # Create the session for the user and redirect him to his account page
            request.session['user_id'] = user_inserted.id
            response = json.dumps({'status': 200, 'message': '/account/'})
            return HttpResponse(response, content_type='application/json')
        else:
            response = json.dumps({
                'status': 0,
                'message': 'This email is already in use'
            })
            return HttpResponse(response, content_type='application/json')
    return render(request,
                  'login/index.html',
                  context={'page_login': '******'})
Esempio n. 2
0
def edit(request):
    user_id = request.session.get('user_id')
    if user_id is not None:
        if request.method == "POST":
            email = str(request.POST.get("email")).lower()
            address = request.POST.get("address")
            country = request.POST.get("country")
            city = request.POST.get("city")
            a_zip = request.POST.get("zip")
            photo_url = request.POST.get('picture')

            not_same_email = True

            # If the email stored in database is the same as entered
            if User.objects.get(id=user_id).email == email:
                not_same_email = False

            # If the email is already in use by another account
            if User.email_already_exists(email) and not_same_email:
                response = json.dumps({
                    'status':
                    0,
                    'message':
                    'Email already in use by another account'
                })
                return HttpResponse(response, content_type='application/json')
            else:
                if not_same_email:
                    User.objects.filter(id=user_id).update(email=email)
                Address.insert(User.objects.get(id=user_id), address, city,
                               country, a_zip)

                if photo_url != "":
                    UserPhoto.update_photo(user_id, photo_url)

                response = json.dumps({'status': 200, 'message': '/account/'})
                return HttpResponse(response, content_type='application/json')
        else:
            context = {
                'page_account': 'edit_profile',
            }
            context = base_context(user_id, context)
            return render(request, 'account/index.html', context)
    else:
        return render(request,
                      'login/index.html',
                      context={'page_login': '******'})