コード例 #1
0
ファイル: views.py プロジェクト: gaoyue001/Qsystem
def register(request):
    if request.method == "POST":
        uf = UserForm(request.POST)
        if uf.is_valid(): 
            #返回注册成功页面

            #往Django user表里再插入一条数据
            username = uf.cleaned_data['username']
            password = uf.cleaned_data['password']
            realname = uf.cleaned_data['realname']
            email = username+"@lyi.com"
            
            try:
                user = User.objects.create_user(username=username, email=email, password=password)
                user.save()
            except:
                uf = UserForm()
                return render_to_response('register.html',{'list':department.objects.all(), 'error':'注册的用户名已存在'},context_instance=RequestContext(request))

            user_new = uf.save();

            #登录
            uid = models.user.objects.filter(username=username)[0].id
            request.session['username'] = username
            request.session['realname'] = realname
            request.session['id'] = uid

            #Django 认证系统的登录
            user = auth.authenticate(username=username, password=password)
            auth.login(request, user)
            return HttpResponseRedirect("/personal_homepage")
    else:
        uf = UserForm()

    return render_to_response('register.html',{'list':department.objects.all()},context_instance=RequestContext(request))
コード例 #2
0
def password_reset(username, code):
    require_current = False

    form = PasswordResetForm()

    user = User.objects.filter(username=username).first()

    if not user or code != user.change_configuration.get(
            'password_reset_code'):
        abort(404)

    if request.method == 'POST':
        del form.current_password
        if form.validate_on_submit():
            salt = gensalt()
            hashed_password = hashpw(form.password.data.encode('utf8'),
                                     salt).decode()
            user.password = hashed_password
            user.change_configuration = {}
            user.save()
            # if user is logged in, log him out
            if session.get('username'):
                session.pop('username')

            return redirect(url_for('user_app.password_reset_complete'))

    return render_template('user/password_reset.html',
                           form=form,
                           require_current=require_current,
                           username=username,
                           code=code)
コード例 #3
0
def register():
    form = RegisterForm()
    if form.validate_on_submit():
        salt = gensalt()
        hashed_password = hashpw(form.password.data.encode('utf8'), salt)
        code = str(uuid.uuid4())
        user = User(username=form.username.data,
                    password=hashed_password,
                    email=form.email.data,
                    first_name=form.first_name.data,
                    last_name=form.last_name.data,
                    change_configuration={
                        "new_email": form.email.data,
                        "confirmation_code": code
                    })

        # email the user
        body_html = render_template('mail/user/register.html',
                                    user=user,
                                    host=WEBSITE_ADDRESS)
        body_text = render_template('mail/user/register.txt',
                                    user=user,
                                    host=WEBSITE_ADDRESS)
        email(user.email, "Welcome to the social network", body_html,
              body_text)
        user.save()

        return redirect(url_for('user_app.login'))
    return render_template('user/register.html', form=form)
コード例 #4
0
def confirm(username, code):

    edit_profile = False
    user = User.objects.filter(username=username).first()

    if user and user.change_configuration and user.change_configuration.get(
            'confirmation_code'):
        if code == user.change_configuration.get('confirmation_code'):
            user.email = user.change_configuration.get('new_email')
            user.change_configuration = {}
            user.email_confirmed = True
            user.save()
            return render_template('user/email_confirmed.html')
    abort(404)
コード例 #5
0
def change_password():

    require_current = True
    error = None
    form = PasswordResetForm()

    formModalMessage = PrivateMessageForm()

    user = User.objects.filter(username=session.get('username')).first()

    if not user:
        abort(404)

    if request.method == 'POST':
        if form.validate_on_submit():

            if user.password == hashpw(
                    form.current_password.data.encode('utf8'),
                    user.password.encode('utf8')).decode():

                salt = gensalt()
                hashed_password = hashpw(form.password.data.encode('utf8'),
                                         salt).decode()
                print(type(hashed_password))
                user.password = hashed_password
                user.save()
                # if user is logged in, log him out
                if session.get('username'):
                    session.pop('username')
                return redirect(url_for('user_app.password_reset_complete'))
            else:
                error = "Incorrect password"

    fetchNotifications(user)

    return render_template('user/password_reset.html',
                           form=form,
                           require_current=require_current,
                           error=error,
                           formModalMessage=formModalMessage)
コード例 #6
0
ファイル: views.py プロジェクト: Carl1993/schedule
def password(request):
    tip = ''
    if request.POST:
        post = request.POST
        user = request.user
        if user.is_authenticated():
            if auth.authenticate(username=user.username,
                                 password=post['prepasswd']) != None:
                if post['passwd'] == post['newpasswd']:
                    if post['passwd'] != '':
                        user.set_password(post['passwd'])
                        user.save()
                        auth.logout(request)
                        return HttpResponseRedirect('../')
                    else:
                        tip = 'New password can\'t be null!'
                else:
                    tip = 'New passwords are not same!'
            else:
                tip = 'Password is incorrect!'
    c = Context({'username': request.user.username, 'tip': tip})
    return render_to_response('change_password.html', c)
コード例 #7
0
def update(id):
    user = User.get_or_none(User.id == id)

    username = request.form.get('username')
    email = request.form.get('email')

    user.email = email
    user.username = username

    if user.save():
        flash("Succesfully updated info!")
        return redirect(url_for('users.edit', id=user.id))
    else:
        flash("Not Succesfull in updating info")
        return render_template('users/edit.html')
コード例 #8
0
def forgot():
    error = None
    message = None
    form = ForgotForm()
    if form.validate_on_submit():
        user = User.objects.filter(email=form.email.data).first()
        if user:
            code = str(uuid.uuid4())
            user.change_configuration = {"password_reset_code": code}
            user.save()
            # email the user
            body_html = render_template('mail/user/password_reset.html',
                                        user=user,
                                        host=WEBSITE_ADDRESS)
            body_text = render_template('mail/user/password_reset.txt',
                                        user=user,
                                        host=WEBSITE_ADDRESS)
            email(user.email, "Password reset request", body_html, body_text)

        message = "You will receive a password reset email if we find that email in our system"
    return render_template('user/forgot.html',
                           form=form,
                           error=error,
                           message=message)
コード例 #9
0
def upload_image(id):
    user = User.get_or_none(User.id == id)

    profile_image = request.files.get('user_file')

    user.profile_image = profile_image.filename

    if user.save():
        flash("Succesfully uploaded image!")

        if 'user_file' not in request.files:
            return "No user_file key in request.files"

        file = request.files.get("user_file")

        if file.filename == "":
            return "Please select a file"

        if file:
            file.filename = secure_filename(file.filename)
            output = upload_file_to_s3(file)

    return redirect(url_for('users.upload', id=user.id))
コード例 #10
0
def edit():
    error = None
    message = None
    user = User.objects.filter(username=session.get('username')).first()

    formModalMessage = PrivateMessageForm()

    if user:
        form = EditForm(obj=user)

        fetchNotifications(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 = "images/user/" + str(uuid.uuid4()) + filename
                file_pathB = "static/" + file_path
                form.image.data.save(file_pathB)
                image_ts = str(file_path)

                print(image_ts)

            # check if new 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()
            # check if new 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 to complete this change"

                    # email the user
                    body_html = render_template('mail/user/change_email.html',
                                                user=user,
                                                host=WEBSITE_ADDRESS)
                    body_text = render_template('mail/user/change_email.txt',
                                                user=user,
                                                host=WEBSITE_ADDRESS)
                    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
                    print("image_ts")
                    print(image_ts)
                user.save()

                if not message:
                    message = "Profile updated"

        return render_template('user/edit.html',
                               form=form,
                               error=error,
                               message=message,
                               user=user,
                               formModalMessage=formModalMessage)
    else:
        abort(404)