コード例 #1
0
ファイル: view.py プロジェクト: wuzeshixp/Happy
def edit():
    user = User.objects.filter(email=session['email']).first()
    if user:
        error = None
        message = None
        form = EditForm(obj=user)

        if request.method == 'POST' and form.validate():
            if user.email != form.email.data: #if change email
                if User.objects.filter(email=form.email.data).first():
                    error = "Email is already in use"
                else:
                    session['email'] = form.email.data.lower()
            if not error:
                form.populate_obj(user) # rewrite in to form
                image_url = upload_image_file(request.files.get('image'), 'proflie_image', str(user.id))
                if image_url:
                    user.profile_image = image_url

                user.save()
                message = "Profile updated"

        return render_template('user/edit.html', user=user,form=form,error =error, message=message)
    else:
        abort(404)
コード例 #2
0
ファイル: views.py プロジェクト: marshalmori/flask-blog-mongo
def edit():
    error = None
    message = None
    user = User.objects.filter(username=session.get('username')).first()
    if user:
        form = EditForm(obj=user)
        if form.validate_on_submit():
            if user.username != form.username.data:
                if User.objects.filter(
                        username=form.username.data.lower()).first():
                    erro = 'Esse nome de usuário já existe.'
                else:
                    session['username'] = form.username.data.lower()
                    form.username.data = form.username.data.lower()
            if user.email != form.email.data:
                if User.objects.filter(email=form.email.data.lower()).first():
                    error = "Esse email já foi cadastrado."
                else:
                    form.email.data = form.email.data.lower()
            if not error:
                form.populate_obj(user)
                user.save()
                message = 'Profile atualizado.'
        return render_template('user/edit.html',
                               form=form,
                               error=error,
                               message=message)
    else:
        abort(404)
コード例 #3
0
def edit():
    error = None
    message = None
    user = User.objects.filter(username=session.get('username')).first()
    if user:
        form = EditForm(obj=user)
        if form.validate_on_submit():
            if user.username != form.username.data:
                if User.objects.filter(
                        username=form.username.data.lower()).first():
                    error = "Username already exists"
                else:
                    session['username'] = form.username.data.lower()
                    form.username.data = form.username.data.lower()
            if user.email != form.email.data:
                if User.objects.filter(email=form.email.data.lower()).first():
                    error = "Email already exists"
                else:
                    form.email.data = form.email.data.lower()
            if not error:
                form.populate_obj(user)
                user.save()
                message = "Profile updated"
        return render_template("user/edit.html",
                               form=form,
                               error=error,
                               message=message)
    else:
        abort(404)
コード例 #4
0
def edit():
    error = None
    message = None
    user = User.objects.filter(username=session.get('username')).first()
    if user:
        form = EditForm(obj=user)  #pre populates form
        if form.validate_on_submit():
            #Check if image is of correct type
            image_ts = None
            if request.files.get('image'):
                filename = secure_filename(form.image.data.filename)
                file_path = os.path.join(UPLOAD_FOLDER, 'user', filename)
                form.image.data.save(
                    file_path)  #save form image under this path
                image_ts = str(
                    thumbnail_process(file_path, 'user', str(user.id)))
            if user.username != form.username.data.lower(
            ):  # check that user has changed own username
                if User.objects.filter(username=form.username.data.lower(
                )).first():  # check that username not already taken
                    error = "Username already taken"
                else:
                    session['username'] = form.username.data.lower()
                    form.username.data = form.username.data.lower()
            if user.email != form.email.data.lower(
            ):  # check that user has changed own email
                if User.objects.filter(email=form.email.data.lower()).first(
                ):  # check that email not already taken
                    error = "This email already exists"
                else:
                    code = str(uuid.uuid4())
                    user.change_configuration = {
                        "new_email": form.email.data.lower(),
                        "confirmation_code": code
                    }
                    user.email_confirmed = False
                    form.email.data = user.email
                    message = "You will need to confirm the new email, by clicking on the link sent to your email"
                    body_html = render_template('mail/user/change_email.html',
                                                user=user)
                    body_text = render_template('mail/user/change_email.txt',
                                                user=user)
                    email(user.change_configuration['new_email'],
                          "Please confirm email change", body_html, body_text)
                    user.save()
                    return "User details updated, pending email confirmation"
            if not error:
                form.populate_obj(user)  #populate form with user object
                if image_ts:  #if image was attached to form
                    user.profile_image = image_ts
                user.save()
                if not message:  #if user did not edit the email
                    message = "Profile updated"
        return render_template("user/edit.html",
                               form=form,
                               error=error,
                               message=message,
                               user=user)
    else:
        abort(404)
コード例 #5
0
ファイル: views.py プロジェクト: GorskayaVictoria/umschool
def edit(request):
    if request.method == "POST":
        form = EditForm(request.POST, request.FILES)
        if form.is_valid():
            username = form.cleaned_data["username"]
            first_name = form.cleaned_data["first_name"]
            last_name = form.cleaned_data["last_name"]
            profile_pic = form.cleaned_data["profile_pic"]
            email = form.cleaned_data["email"]
            if username != "":
                request.user.username = username
                request.user.save()
            if first_name != "":
                request.user.email = first_name
                request.user.save()
            if last_name != "":
                request.user.email = last_name
                request.user.save()
            if profile_pic != "":
                request.user.profile_pic = profile_pic
                request.user.save()
            if email != "":
                request.user.email = email
                request.user.save()
            user = authenticate(username=request.user.username,
                                password=request.user.password)
            login(request, user)
            return redirect(reverse("index"))
        else:
            return render(request, "edit.html", {"form": form})
    else:
        return render(request, "edit.html", {"form": EditForm()})
コード例 #6
0
def edit():
    error = None
    message = None
    user = User.objects.filter(username=session.get("username")).first()
    if user:
        form = EditForm(obj=user)  # Prepopulating the form with what's in user
        if form.validate_on_submit():
            # Check if image
            image_ts = None
            if request.files.get("image"):
                filename = secure_filename(form.image.data.filename)
                file_path = os.path.join(UPLOAD_FOLDER, "user", filename)
                form.image.data.save(file_path)
                image_ts = str(thumbnail_process(file_path, "user",
                               str(user.id)))
            # User changes their username
            if user.username != form.username.data.lower():
                if User.objects.filter(username=form.username.data.lower()).first():
                    error = "Username already exists"
                else:
                    session["username"] = form.username.data.lower()
                    form.username.data = form.username.data.lower()
            # User changes their email
            if user.email != form.email.data.lower():
                if User.objects.filter(email=form.email.data.lower()).first():
                    error = "Email already exists"
                else:
                    code = str(uuid.uuid4())
                    user.change_configuration = {
                        "new_email": form.email.data.lower(),
                        "confirmation_code": code
                    }
                    user.email_confirmed = False
                    form.email.data = user.email
                    message = "You will need to confirm the new email address \
                               to complete this change."
                    
                    # email the user
                    body_html = render_template("mail/user/change_email.html",
                                                user=user)
                    body_text = render_template("mail/user/change_email.txt",
                                                user=user)
                    email(user.change_configuration["new_email"],
                          "Confirm your new email", body_html, body_text)
                    
            if not error:
                # Populate database object with form's content
                form.populate_obj(user)
                # Add image if it exists
                if image_ts:
                    user.profile_image = image_ts
                user.save()
                if not message:
                    message = "Profile updated"

        return render_template("user/edit.html", form=form, error=error,
                               message=message, user=user)
    else:  # User wasn't found
        abort(404)
コード例 #7
0
ファイル: views.py プロジェクト: jiaweizhong/Flaskbook
def edit():
    error = None
    message = None
    user = User.objects.filter(username=session.get('username')).first()
    if user:
        form = EditForm(obj=user)
        if form.validate_on_submit():
            # check if image
            image_ts = None
            if request.files.get('image'):
                filename = secure_filename(form.image.data.filename)
                file_path = os.path.join(UPLOAD_FOLDER, 'user', filename)
                form.image.data.save(file_path)
                image_ts = str(
                    thumbnail_process(file_path, 'user', str(user.id)))
            if user.username != form.username.data.lower():
                if User.objects.filter(
                        username=form.username.data.lower()).first():
                    error = "Username already exists"
                else:
                    session['username'] = form.username.data.lower()
                    form.username.data = form.username.data.lower()
            if user.email != form.email.data.lower():
                if User.objects.filter(email=form.email.data.lower()).first():
                    error = "Email already exists"
                else:
                    code = str(uuid.uuid4())

                    user.change_configuration = {
                        "new_email": form.email.data.lower(),
                        "confirmation_code": code
                    }
                    user.email_confirmed = False
                    form.email.data = user.email
                    message = "You will need to confirm the new email to complete this change"

                    # email the user
                    body_html = render_template('mail/user/change_email.html',
                                                user=user)
                    body_text = render_template('mail/user/change_email.txt',
                                                user=user)
                    email(user.change_configuration['new_email'],
                          "Confirm your new email", body_html, body_text)

            if not error:
                form.populate_obj(user)
                if image_ts:
                    user.profile_image = image_ts
                user.save()
                if not message:
                    message = "Profile updated"

        return render_template("user/edit.html",
                               form=form,
                               error=error,
                               message=message,
                               user=user)
    else:
        abort(404)
コード例 #8
0
def edit():
    error = None
    message = None
    user = User.getByName(session['username'])
    if user:
        form = EditForm(obj=user)
        if form.validate_on_submit():
            image_ts = None
            if request.files.get('image'):
                filename = secure_filename(form.image.data.filename)
                folder_path = os.path.join(Config.UPLOAD_FOLDER, 'user_' + user.id)
                file_path = os.path.join(folder_path, filename)
                if not os.path.exists(folder_path):
                    os.makedirs(folder_path)
                form.image.data.save(file_path)
                image_ts = str(thumbnail_process(file_path, 'user_' + user.id, str(user.id)))

            if user.username != form.username.data.lower():
                if User.getByName(form.username.data.lower()):
                    error = "This username is already in use."
                else:
                    session['username'] = form.username.data.lower()
                    user.username = form.username.data.lower()

            if user.email != form.email.data.lower():
                if User.getByEmail(form.email.data.lower()):
                    error = "This email is already in use."
                else:
                    code = str(uuid.uuid4())

                    user.change_configuration = {
                        "new_email": form.email.data.lower(),
                        "confirmation_code": code
                    }

                    user.email_confirmation = False
                    message = "You will need to confirm the new email to complete this change"

                    # email the user
                    body_html = render_template('mail/user/change_email.html', user=user)
                    body_text = render_template('mail/user/change_email.txt', user=user)
                    email(user.change_configuration['new_email'], "Confirm your new email", body_html, body_text)

            if not error:
                form.populate_obj(user)
                if image_ts:
                    user.profile_image = image_ts
                user.update_record()
                if message:
                    return redirect(url_for('.logout'))
                else:
                    message = "Your info has been updated succefully ..!"

        return render_template('user/edit.html', form=form, error=error, message=message, user=user)

    else:
        abort(404)
コード例 #9
0
ファイル: views.py プロジェクト: hammer2383/print-app
def edit():
    error = None
    message = None
    user = User.objects.filter(email=session.get('email')).first()
    if user:
        form = EditForm()
        if form.validate_on_submit():
            # check if image
            image_ts = None
            if request.files.get('image'):
                if user.profile_image:
                    sizes = ["sm", "lg", "raw"]
                    for size in sizes:
                        filename = '%s.%s.%s.jpg' % (user.id,
                                                     user.profile_image, size)
                        os.remove(
                            os.path.join(UPLOAD_FOLDER_IMG, 'user', filename))
                filename = secure_filename(form.image.data.filename)
                file_path = os.path.join(UPLOAD_FOLDER_IMG, 'user', filename)
                form.image.data.save(file_path)
                image_ts = str(
                    thumbnail_process(file_path, 'user', str(user.id)))

            if user.username != form.username.data.lower():
                if User.objects.filter(
                        username=form.username.data.lower()).first():
                    error = "Username already exists"
                else:
                    session['username'] = form.username.data.lower()
                    user.username = form.username.data.lower()
            if not error:
                if image_ts:
                    user.profile_image = image_ts
                user.first_name = form.first_name.data
                user.last_name = form.last_name.data
                user.facebook_link = form.facebook_link.data
                user.tel = form.tel.data
                user.save()
                if not message:
                    message = "Profile updated"
        elif request.method == 'GET':
            form.username.data = user.username
            form.facebook_link.data = user.facebook_link
            form.first_name.data = user.first_name
            form.last_name.data = user.last_name
            form.tel.data = user.tel

        return render_template("user/edit.html",
                               form=form,
                               error=error,
                               message=message,
                               user=user)
    else:
        abort(404)
コード例 #10
0
ファイル: views.py プロジェクト: fromzeroedu/flaskbook
def edit():
    error = None
    message = None
    user = User.objects.filter(username=session.get('username')).first()
    if user:
        form = EditForm(obj=user)
        if form.validate_on_submit():
            # check if image
            image_ts = None
            if request.files.get('image'):
                filename = secure_filename(form.image.data.filename)
                file_path = os.path.join(UPLOAD_FOLDER, 'user', filename)
                form.image.data.save(file_path)
                image_ts = str(thumbnail_process(file_path, 'user', str(user.id)))
            if user.username != form.username.data.lower():
                if User.objects.filter(username=form.username.data.lower()).first():
                    error = "Username already exists"
                else:
                    session['username'] = form.username.data.lower()
                    form.username.data = form.username.data.lower()
            if user.email != form.email.data.lower():
                if User.objects.filter(email=form.email.data.lower()).first():
                    error = "Email already exists"
                else:
                    code = str(uuid.uuid4())
                    
                    user.change_configuration = {
                        "new_email": form.email.data.lower(),
                        "confirmation_code": code
                    }
                    user.email_confirmed = False
                    form.email.data = user.email
                    message = "You will need to confirm the new email to complete this change"
                    
                    # email the user
                    body_html = render_template('mail/user/change_email.html', user=user)
                    body_text = render_template('mail/user/change_email.txt', user=user)
                    email(user.change_configuration['new_email'], "Confirm your new email", body_html, body_text)
                    
            if not error:
                form.populate_obj(user)
                if image_ts:
                    user.profile_image = image_ts
                user.save()
                if not message:
                    message = "Profile updated"
    
        return render_template("user/edit.html", form=form, error=error, message=message, user=user)
    else:
        abort(404)
コード例 #11
0
ファイル: views.py プロジェクト: harmannd/flaskbook
def edit():
    error = None
    message = None
    user = User.objects.filter(username=session.get('username')).first()
    if user:
        form = EditForm(obj=user)
        if form.validate_on_submit():
            if user.username != form.username.data.lower():
                if User.objects.filter(
                        username=form.username.data.lower()).first():
                    error = "Username already exists"
                else:
                    session['username'] = form.username.data.lower()
                    form.username.data = form.username.data.lower()
            if user.email != form.email.data.lower():
                if User.objects.filter(email=form.email.data.lower()).first():
                    error = "Email already exists"
                else:
                    code = str(uuid.uuid4())

                    user.change_configuration = {
                        "new_email": form.email.data.lower(),
                        "confirmation_code": code
                    }
                    user.email_confirmed = False
                    form.email.data = user.email
                    message = "You will need to confirm the new email to complete this change"

                    # email the user
                    body_html = render_template('mail/user/change_email.html',
                                                user=user)
                    body_text = render_template('mail/user/change_email.txt',
                                                user=user)
                    email(user.change_configuration['new_email'],
                          "Confirm your new email", body_html, body_text)

            if not error:
                form.populate_obj(user)
                user.save()
                if not message:
                    message = "Profile updated"

        return render_template("user/edit.html",
                               form=form,
                               error=error,
                               message=message)
    else:
        abort(404)
コード例 #12
0
def edit_data(request):
    if request.method == 'POST':
        form = EditForm(request.POST)
        if form.is_valid():
            user = request.user
            user.first_name = form.cleaned_data["first_name"]
            user.last_name = form.cleaned_data["last_name"]
            user.fathers_name = form.cleaned_data["fathers_name"]
            user.credit_card_number = form.cleaned_data["credit_card_number"]
            if user.vk_link != form.cleaned_data["vk_link"]:
                # if form.cleaned_data["vk_link"] is not None and form.cleaned_data["vk_link"].find(
                #         "vk.com") != -1:
                #     config = ConfigAPI()
                #     config.init()
                #     query = form.cleaned_data["vk_link"].split("/")[3]
                #     vk_user = config.api.users.get(user_ids=query, fields="id")
                #     user_id = vk_user[0]["id"]
                #     friends = config.api.friends.get(user_id=user_id, fields="id, domain")
                #     for friend in friends["items"]:
                #         friend_model = User.objects.filter(
                #             vk_link__contains="vk.com/{}".format(friend["domain"])).first()
                #         if friend_model is not None:
                #             follow = Follow.objects.get_or_create(follower=user, follow_user=friend_model)
                #             follow = Follow.objects.get_or_create(follower=friend_model, follow_user=user)
                user.vk_link = form.cleaned_data["vk_link"]
            if user.instagram_link != form.cleaned_data["instagram_link"]:
                if form.cleaned_data["instagram_link"] is not None and form.cleaned_data["instagram_link"].find(
                        "instagram.com") != -1:
                    # init = InstagramInitializer()
                    # friends = init.get_friends(form.cleaned_data["instagram_link"].split("/")[3])
                    pass
                user.instagram_link = form.cleaned_data["instagram_link"]

            user.email = form.cleaned_data["email"]
            user.save()
            return redirect("profile")
        else:
            return render(request, "edit.html", {"user": request.user, "form": EditForm()})
    else:
        user = request.user
        return render(request, "edit.html", {"user": user, "form": EditForm()})
コード例 #13
0
def edit():
    error = None
    message = None
    user = User.objects.filter(username=session.get('username')).first()

    #  If the user was found
    if user:
        #  obj=user is wtfform special usage thta prfills the form with user object
        form = EditForm(obj=user)
        if form.validate_on_submit():
            #  Check to see if username is changing
            # also the case may have changes so potetially give a false positive
            # therefore set username lower case.
            if user.username != form.username.data.lower():
                # Check to see if username already exists
                if User.objects.filter(
                        username=form.username.data.lower()).first():
                    error = 'Username already exists'
                else:
                    # set the session to that of the username(lowercase)
                    session['username'] = form.username.data.lower()
                    #  set the username in the form to lowercase
                    form.username.data = form.username.data.lower()

            # Check if the email has chanmged
            if user.email != form.email.data:
                # The email has changed but check that it doesnt already exist
                if User.objects.filter(email=form.email.data.lower()).first():
                    error = 'email already exists'
                else:
                    # email has changed but does not already exist
                    # sent verification email
                    code = str(uuid.uuid4())
                    user.change_configuration = {
                        'new_email': form.email.data.lower(),
                        'confirmation_code': code
                    }

                    #set the email confirmation to false
                    user.email_confirmed = False
                    # Change the form email to the old email otherwise the new email
                    # will be changed without the confirmation
                    form.email.data = user.email
                    message = 'You will need to confirm the new email to complete this change'

                    # Email the user
                    body_html = render_template('mail/user/change_email.html',
                                                user=user)
                    body_text = render_template('mail/user/change_email.txt',
                                                user=user)
                    email(user.change_configuration['new_email'],
                          'Confirm your new email', body_html, body_text)

            #  If there are no errors Populate the user object with the new info
            if not error:
                # use a WTForm specuial usage to populate the user obj and
                # save (rather UPDATE) to DB
                form.populate_obj(user)
                user.save()
                # The new email has been confirmed
                if not message:
                    message = 'Profile updated'
        return render_template('user/edit.html',
                               form=form,
                               error=error,
                               message=message)
    else:
        # No user found
        abort(404)