def userRegistWithCode(code): ''' 用户注册,有无邀请码来区分不同的组册情况 ''' if current_user.is_authenticated: flash('Please logout before registe a new account') next = request.args.get('next') if next: return redirect(next) return redirect(url_for('index.indexPage')) else: form = RegisterForm() if code: form.inviteCode.data = code if form.validate_on_submit(): user = User() user.username = form.username.data user.email = form.email.data user.password = generate_password_hash(form.password.data) user.sex = form.sex.data user.age = form.age.data user.school = form.school.data user.location = form.location.data user.createdTime = date.today() user.isactive = True user.isauthenticated = False uniqueCode = form.inviteCode.data if uniqueCode: if Pair.query(bson.objectid.ObjectId(str(code))): flash('This invite code has been used') return redirect(url_for('login.userRegist')) user.save() login_user(user) if user.sex == 'male': try: pair = Pair() pair.boy = user pair.girl = User.query( bson.objectid.ObjectId(str(code))) pair.save() except Exception as e: print(str(e)) else: try: pair = Pair() pair.girl = user pair.boy = User.query( bson.objectid.ObjectId(str(code))) pair.save() except Exception as e: print(str(e)) return redirect(url_for('index.indexPage')) print(user.username) user.save() login_user(user) return redirect(url_for('index.indexPage')) return render_template('login/registe.html', form=form)
def active(uid): if uid: user = User.query(bson.objectid.ObjectId(str(uid))) if not user or user.is_authenticated: return redirect(url_for('index.indexPage')) user.update(isauthenticated=True) return redirect(url_for('index.indexPage'))
def getPosts(): ''' this route is provided to get user's posts ''' uid = current_user.userid user = User.query(bson.objectid.ObjectId(str(uid))) posts = Posts.objects(poster=user) return render_template('dairy/index.html', posts=posts)
def uploadFile(form=None, dbModel=None): form = form if form.validate_on_submit(): file = request.files['data'] uid = current_user.userid user = User.query(bson.objectid.ObjectId(str(uid))) post = dbModel() post.poster = user post.createdTime = datetime.today() post.data.put(form.data.data) post.des = form.desc.data post.filename = file.filename print(form.data.name) post.save()
def getFile(filename): if filename: print(filename) uid = current_user.userid user = User.query(bson.objectid.ObjectId(str(uid))) post = Posts.objects(poster=user, filename=filename).first() if post: data = post.data response = make_response(data.read()) response.headers[ "Content-Disposition"] = "attachment; filename={}".format( post.filename) return response else: abort(404)
def load_user(uid): return User.query(bson.objectid.ObjectId(str(uid)))