cursor = db.cursor() sql_string = "DELETE FROM User WHERE username = '******';" print(sql_string) try: cursor.execute(sql_string) except (pymysql.Error, pymysql.Warning) as e: print(e) return return "deleted" else: return "not logged in" app.add_url_rule('/add_visit', 'add_visit', Visits.add_visit, methods=['POST']) app.add_url_rule('/get_user_visits', 'get_user_visits', Visits.get_user_visits, methods=['GET']) app.add_url_rule('/remove_visit', 'remove_visit', Visits.remove_visit, methods=['POST']) app.add_url_rule('/get_visitors', 'get_visitors', Visits.get_visitors, methods=['GET']) app.add_url_rule('/remove_all_visits', 'remove_all_visits', Visits.remove_all_visits,
"""将context载入模板""" return render_template(self.get_template_name, **context) def get_objects(self): """获取数据表中数据""" cur = g.db.execute('select title, text from entries order by id desc') return [dict(title=row[0], text=row[1]) for row in cur.fetchall()] def dispatch_request(self): """将封装数据,并载入模板""" if request.method in ['POST', 'GET']: context = {'entries': self.get_objects()} return self.render_template(context) # 动态的添加handler入口,和@router效果一致 app.add_url_rule('/', view_func=ShowEntries.as_view('show_entries')) # some_func路径指向app是flaskr的views文件中的somefunc这个函数 # app.add_url_rule('/some_func', view_func=flaskr.views.somefunc) # 基于http方法(GET/POST)的调度,将http请求的方法和类中定义的方法匹配,做对应的动作,restfull api ''' class UserAPI(MethodView): def get(self, user_id): if user_id is None: # 返回一个包含所有用户的列表 pass else: # 显示一个用户 pass
except (pymysql.Error, pymysql.Warning) as e: cursor.close() return "Invalid" return json.dumps(cursor.fetchall(), sort_keys=True, indent=4, separators=(',', ': ')) else: return "not logged in" def hash_password(password): return hashlib.md5(bytes(password.encode())).hexdigest() app.add_url_rule('/register', 'register', User.register, methods=['POST']) app.add_url_rule('/login', 'login', User.login, methods=['POST']) app.add_url_rule('/logout', 'logout', User.logout, methods=['GET']) app.add_url_rule('/get_session', 'get_session', User.get_session, methods=['GET']) app.add_url_rule('/remove_user', 'remove_user', User.remove_user, methods=['POST']) app.add_url_rule('/get_all_users', 'get_all_users', User.get_all_users, methods=['GET'])
cursor.execute(sql_string) except (pymysql.Error, pymysql.Warning) as e: print(e) return return_string = json.dumps(cursor.fetchall(), sort_keys=True, indent=4, separators=(',', ': ')) return return_string else: return "not logged in" app.add_url_rule('/add_produce', 'add_produce', Produce.add_produce, methods=['POST']) app.add_url_rule('/delete_produce', 'delete_produce', Produce.delete_produce, methods=['POST']) app.add_url_rule('/approve_produce', 'approve_produce', Produce.approve_produce, methods=['POST']) app.add_url_rule('/get_unapproved_produce', 'get_unapproved_produce', Produce.get_unapproved_produce, methods=['GET']) app.add_url_rule('/get_approved_produce', 'get_approved_produce',
try: cursor.execute(sql_string) except (pymysql.Error, pymysql.Warning) as e: print (e) return list = cursor.fetchall() print (list) for dict in list: print (dict) dict['isPublic'] = str(dict['isPublic']) dict['isCommercial'] = str(dict['isCommercial']) return json.dumps(list, sort_keys=True, indent=4, separators=(',', ': ')) else: return "not logged in" app.add_url_rule('/add_property', 'add_property', Property.add_property, methods=['POST']) app.add_url_rule('/delete_property', 'delete_property', Property.delete_property, methods=['POST']) app.add_url_rule('/get_user_properties', 'get_user_properties', Property.get_user_properties, methods=['POST']) app.add_url_rule('/get_other_user_properties', 'get_other_user_properties', Property.get_other_user_properties, methods=['POST']) app.add_url_rule('/update_property', 'update_property', Property.update_property, methods=['POST']) app.add_url_rule('/get_properties_from_attribute', 'get_properties_from_attribute', Property.get_properties_from_attribute, methods=['POST']) app.add_url_rule('/get_property_by_id', 'get_property_by_id', Property.get_property_by_id, methods=['POST']) app.add_url_rule('/get_detailed_property', 'get_detailed_property', Property.get_detailed_property, methods=['POST']) app.add_url_rule('/get_unconfirmed_properties', 'get_unconfirmed_properties', Property.get_unconfirmed_properties, methods=['GET']) app.add_url_rule('/update_multiple_attributes', 'update_multiple_attributes', Property.update_multiple_attributes, methods=['POST']) app.add_url_rule('/get_user_properties_from_attribute', 'get_user_properties_from_attribute', Property.get_user_properties_from_attribute, methods=['POST'])
from flask import request, session, Response from flaskr import app, db class FarmGrows(): @staticmethod def add_relation(): if 'user' in session.keys(): parsed_json = request.get_json() id = parsed_json['id'] produceName = parsed_json['produceName'] cursor = db.cursor() sql_string = "INSERT INTO FarmGrows (id, produceName) VALUES (" \ + id + ", '" + produceName + "')" try: cursor.execute(sql_string) except (pymysql.Error, pymysql.Warning) as e: print(e) return return "Added produce" else: return "not logged in" app.add_url_rule('/add_relation', 'add_relation', FarmGrows.add_relation, methods=['POST'])