def index(request): if request.method == 'GET': user = current_user(request) todos = Todo.filter_by(user_id=user.id) body = template('index.html', username=user.username, todos=todos) return get_headers() + '\r\n' + body if request.method == 'POST': data = request.form() Todo.create_obj(user_id=current_user(request).id, **data) return get_headers(code=302, Location='/') + '\r\n'
def admin_index(): user = current_user() if user is None: error = 'no login' return render_template('admin/login.html') else: return redirect('/admin/basicinfo')
def client(): user = current_user() if not user: # session['id'] = 1 # temporary decision return redirect(url_for('register')) item = Client( client_id=gen_salt(40), client_secret=gen_salt(50), _redirect_uris=' '.join([ '%s/%s' % (settings.BASE_URL, 'authorized'), 'http://localhost:8000/authorized', 'http://127.0.0.1:8000/authorized', 'http://127.0.1:8000/authorized', 'http://127.1:8000/authorized', ]), _default_scopes='email', user_id=session['id'], ) db.session.add(item) db.session.commit() return jsonify( client_id=item.client_id, client_secret=item.client_secret, )
def delete_weibo(weibo_id): u = current_user() w = Weibo.query.get(weibo_id) is_auth, message = w.is_auth(u) if is_auth: w.delete() return ajax_response(is_auth, data=w.json(), message=message)
def delete(self, user_id): user = User.query.filter_by(id=user_id).first() if not user: return jsonify( status='Error', msg='User was not found.' ), 400 if current_user().id != user.id: return jsonify( status='Error', msg='Not permission' ), 403 try: db.session.delete(user) db.session.commit() return jsonify( status='Success', msg='User removed' ) except Exception: return jsonify( status='Error', msg='Can not remove user' ), 500
def delete_comment(comment_id): u = current_user() c = WeiboComment.query.get(comment_id) is_auth, message = c.is_auth(u) if is_auth: c.delete() return ajax_response(is_auth, data=c.json(), message=message)
def add_comment(): u = current_user() form = request.form c = bbsComment(form) c.author_id = u.id c.content_id = form.get('content_id', -1) c.save() return redirect(url_for('.content_get', id=c.content_id))
def authorize(*args, **kwargs): user = current_user() if not user: return redirect(url_for('login')) # if user is authorized return True return True
def todo_route(request): if request.Method == 'GET': user = current_user(request) todo_models = Todo.find(user_id=user.id) header = 'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection:Close\r\n' body = template('todo.html', todo_models=todo_models) r = header + '\r\n' + body return r
def route_index(request): uid = current_user(request) u = User.find_by(id=uid) if u is not None: username = u.username else: username = '******' return html_response('index.html', username=username)
def add_weibo(): u = current_user() form = request.form w = Weibo(form) valid, message = w.valid() if valid: w.author_id = u.id w.save() return ajax_response(valid=valid, data=w.json(), message=message)
def draft_add(): u = current_user() form = request.form c = bbsContent(form) c.add_node(form) c.gen_tags(form) c.user_id = u.id c.save() return redirect(url_for('.node_content', id=c.node_id))
def update_weibo(weibo_id): u = current_user() w = Weibo.query.get(weibo_id) form = request.form is_auth, message = w.is_auth(u) if is_auth: w.body = form.body w.save() return ajax_response(is_auth, data=w.json(), message=message)
def update_comment(comment_id): u = current_user() form = request.form c = WeiboComment.query.get(comment_id) is_auth, message = c.is_auth(u) if is_auth: c.body = form.get('body','') c.update_time = datetime.utcnow() c.save() return ajax_response(is_auth, data=c.json(), message=message)
def add_comment(): u = current_user() form = request.form c = WeiboComment(form) valid, message = c.valid() if valid: c.weibo_id = int(form.get('weibo_id', -1)) c.author_id = u.id c.save() return ajax_response(True, data=c.json(), message=message)
def add_todo(request): if request.Method == 'POST': form = request.form() user = current_user(request) if not user: return redirect('/404') form['user_id'] = user.id todo = Todo(form) if form.get('content', ''): print '我不是空的!' todo.save() return redirect('/todo')
def put(self, user_id): user = User.query.filter_by(id=user_id).first() if not user: return jsonify( status='Error', msg='User was not found.' ), 400 if current_user().id != user.id: return jsonify( status='Error', msg='Not permission' ), 403 form_data = request_fields_to_kwargs([ 'username', 'email', 'password', 'password_confirm' ]) is_change_password = True if not form_data['password']: form_data['password'] = '******' form_data['password_confirm'] = 'test_password' is_change_password = False form = RegisterForm(**form_data) if not form.validate(): return jsonify( status='Error', type='validation', **form.errors ), 400 user.name = form.username user.email = form.email if is_change_password: user.password_hash = form.password try: db.session.commit() return jsonify( status='Success', msg='User info updated' ) except Exception: return jsonify( status='Error', type='', msg='Can not update user.' ), 500
def index_route(request): """ 返回index页面的response """ user = current_user(request) if user: username = user.username else: username = u'【游客】' header = 'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection:Close\r\n' body = template('index.html', username=username) r = header + '\r\n' + body return r
def delete_todo(request): if request.Method == 'GET': user = current_user(request) todo_id = request.query.get('id', '') if not todo_id: return handle_404() t = Todo.find_by_id(int(todo_id)) if not t: return handle_404() if t.user_id != user.id: return handle_404() t.remove() return redirect('/todo')
def client(): c = Client( current_user(), [ 'http://localhost:8000/authorized', 'http://localhost/authorized', ], ['email']) assert c.default_realms == ['email'], repr(c.default_realms) assert c.realms == ['email'], c.realms clients.append(c) return jsonify( client_key=c.client_key, client_secret=c.client_secret)
def _grantsetter(client_id, code, request, *args, **kwargs): # decide the expires time yourself expires = datetime.utcnow() + timedelta(seconds=100) grant = Grant( client_id=client_id, code=code['code'], redirect_uri=request.redirect_uri, _scopes=' '.join(request.scopes), user=current_user(), expires=expires ) db.session.add(grant) db.session.commit() return grant
def get(self, user_id): user = User.query.filter_by(id=user_id).first() if not user: return jsonify( status='Error', msg='User was not found.' ), 400 if current_user().id != user.id: return jsonify( status='Error', msg='Not permission' ), 403 return jsonify( status='Success', msg='User info', name=user.name, email=user.email )
def save_verifier(token, verifier, *args, **kwargs): t = [t for t in request_tokens if t.token == token][0] t.verifier = verifier["oauth_verifier"] t.user = current_user() return t
def before_request(): g.user = current_user()
def post(self): name = request.values.get('name', None) code = request.values.get('code', None) data = request.values.get('data', '') form = TaskForm(name=name, code=code, data_field=data) if not form.validate(): errors = {} if len(form.name.errors): errors['name'] = form.name.errors if len(form.code.errors): errors['code'] = form.code.errors if len(form.data_field.errors): errors['data'] = form.data_field.errors return jsonify( status='Error', type='Validation', msg=errors ), 400 try: created = datetime.utcnow() function = Function( name=name, created=created, user=current_user(), ) db.session.add(function) db.session.commit() data = Data(data=data) db.session.add(data) db.session.commit() updated = created task = Task( function_id=function.id, updated=updated, code=code, version=None, data_id=data.id ) db.session.add(task) db.session.commit() return jsonify( status='Success', msg='Task was created.', task_id=task.id ), 201 except Exception as e: db.session.rollback() return jsonify( status='Error', msg='Task was not created.', details=str(e) ), 500
def content_edit(id): u = current_user() c = bbsContent.query.get(id) return render_template('bbs/content_edit.html', content=c, u=u)
def content_edit_all(): u = current_user() c = bbsContent.query.all() return render_template('bbs/content_edit_all.html', contents=c, u=u)
def content_show(): u = current_user() c = bbsContent.query.order_by(bbsContent.create_time.desc()).all() return render_template('bbs/content_show.html', contents=c, u=u)
def save_verifier(token, verifier, *args, **kwargs): t = [t for t in request_tokens if t.token == token][0] t.verifier = verifier['oauth_verifier'] t.user = current_user() return t
def draft_show(): u = current_user() t = bbsTag.query.all() n = bbsNode.query.all() return render_template('bbs/draft.html', nodes=n, tags=t, u=u)
def node_content(id): u = current_user() n = bbsNode.query.get(id) c = bbsContent.query.filter_by(node_id=id).order_by( bbsContent.create_time.desc()).all() return render_template('bbs/content_list.html', contents=c, u=u, node=n)
def index(): weibos = Weibo.query.order_by(Weibo.create_time.desc()).all() u = current_user() return render_template('/weibo/weibo.html', weibos=weibos, u=u)