def create_post(): post = Post() try: data = request.get_data() data = json.loads(data) post.title = data['title'] post.content = data['content'] post.category_id = data['category_id'] tags = data['tags'] post.region = data['region'] post.image_header_id = data['image_header_id'] except Exception: raise ServiceException(ErrorCode.PARAM_ERROR, 'param error') tag_ids = [] if tags is not None and tags != '': tags = tags.split(",") for tag_name in tags: try: tag = GroupService.get_tag_by_name(tag_name) except ServiceException: tag = GroupService.insert_tag(tag_name) tag_ids.append(str(tag.id)) post.tag_ids = ",".join(tag_ids) post = PostService.insert_post(post) return jsonify(json_resp(data=model2dict(post)))
def list_problems_by_company_name(name): """ @api {get} /api/v1/problems/companies/<name> 根据公司名称查询公司下面的题目 @apiVersion 1.0.0 @apiGroup LeetCode Problems @apiParam {int} [page] 页码 默认为1 @apiParam {int} [difficulty] 难度:1 easy, 2 medium, 3 hard @apiParam {int} [is_locked] 是否上锁:0否, 1是 @apiParam {int} [type] 题目类型:0算法,1数据库 @apiSuccess {int} lid 题号 @apiSuccess {int} qid LeetCode题目Id @apiSuccess {String} title 题目标题 @apiSuccess {int} difficulty 难度:1 easy, 2 medium, 3 hard @apiSuccess {int} is_locked 是否上锁:0否, 1是 @apiSuccess {int} type 题目类型:0算法,1数据库 @apiSuccess {int} difficulty 难度:1 easy, 2 medium, 3 hard @apiSuccess {float} frequency 题目热度 @apiSuccess {String} title_slug 题目title的url缩写 @apiSuccess {String} submit_url 题目代码提交url @apiError {int} code -1 失败,1成功,3参数错误,4资源没找到,5系统错误 @apiSuccessExample {json} Success-Response: { "code": 200, "data": { "data": [ { "create_time": "2018-07-26 11:56:05", "difficulty": 3, "frequency": 104.272, "id": 315, "is_locked": 1, "lid": 571, "qid": 571, "submit_url": "/problems/find-median-given-frequency-of-numbers/submit/", "title": "Find Median Given Frequency of Numbers", "title_slug": "find-median-given-frequency-of-numbers", "type": 0, "update_time": "2018-07-26 13:01:31" } ], "max_cnt": 1, "max_page": 1, "page": 1, "page_size": 50 }, "msg": "success" } """ page = request.args.get('page') if page is None: page = 1 page_size = app.config['PAGE_LARGE'] problems = ProblemService.list_problems_by_company_name( name, page, page_size) return jsonify(json_resp(data=model2dict(problems)))
def verify(username): AuthService.verify_email(username) url = app.config.get('HTTP_HEAD') + app.config.get('HTTP_HOST') + ':' + str( app.config.get('HTTP_PORT')) + '/register/' + get_email_token(username) executor = ThreadPoolExecutor(4) executor.submit(send_mail, 'Blockzone email verification', 'Please confirm your email by clicking this link: ' + url, [username]) return jsonify(json_resp())
def image_upload(): def allowed_file(filename): return '.' in filename and \ filename.rsplit('.', 1)[1] in app.config.get('ALLOWED_EXTENSIONS') file = request.files['image'] if file and allowed_file(file.filename): image = ImageService.upload_image(file) else: raise ServiceException(ErrorCode.PARAM_ERROR, 'file format error') return jsonify(json_resp(data=model2dict(image)))
def list_regions(): regions = [{ 'id': 1, 'name': 'China' }, { 'id': 2, 'name': 'South Korea' }, { 'id': 3, 'name': 'Singapore' }, { 'id': 4, 'name': 'Other Regions' }] return jsonify(json_resp(data=regions))
def list_drafts(): posts = PostService.list_drafts() posts = models2dict(posts) for post in posts: del post['content'] post['category'] = GroupService.get_category_by_id( post['category_id']).name tags_str = post['tag_ids'] tags = tags_str.split(",") L = [] if tags_str is not None and tags_str != '': for tag_id in tags: tag_id = int(tag_id) tag_name = GroupService.get_tag_by_id(tag_id).name L.append(tag_name) post['tags'] = L return jsonify(json_resp(data=posts))
def login(): try: data = request.get_data() data = json.loads(data) username = data['username'] password = data['password'] remember_me = data['remember_me'] except Exception: raise ServiceException(ErrorCode.PARAM_ERROR, 'param error') AuthService.login(username, password) resp = make_response(jsonify(json_resp())) if remember_me: resp.set_cookie('token', get_login_token(username, 7)) else: resp.set_cookie('token', get_login_token(username, 1)) return resp
def register(token): try: data = request.get_data() data = json.loads(data) password1 = data['password1'] password2 = data['password2'] remember_me = data['remember_me'] except Exception: raise ServiceException(ErrorCode.PARAM_ERROR, 'param error') username = decode_email_token(token) AuthService.register(username, password1, password2) resp = make_response(jsonify(json_resp())) if remember_me: resp.set_cookie('token', get_login_token(username, 7)) else: resp.set_cookie('token', get_login_token(username, 1)) return resp
def get_leetcode_problem_by_title_slug(title_slug): """ @api {get} /api/v1/problems/title/<title_slug> 根据LeetCode的title_slug获取题目信息 @apiVersion 1.0.0 @apiGroup LeetCode Problems @apiParam {title_slug} title_slug @apiSuccess {int} lid 题号 @apiSuccess {int} qid LeetCode题目Id @apiSuccess {String} title 题目标题 @apiSuccess {String} desc 题目描述 @apiSuccess {String} code_def 编辑器默认代码 @apiSuccess {int} difficulty 难度:1 easy, 2 medium, 3 hard @apiSuccess {int} is_locked 是否上锁:0否, 1是 @apiSuccess {int} type 题目类型:0算法,1数据库 @apiSuccess {int} difficulty 难度:1 easy, 2 medium, 3 hard @apiSuccess {float} frequency 题目热度 @apiSuccess {String} title_slug 题目title的url缩写 @apiSuccess {String} submit_url 题目代码提交url @apiError {int} code -1 失败,1成功,3参数错误,4资源没找到,5系统错误 @apiSuccessExample {json} Success-Response: { "code": 1, "data": { "code_def": "[{\"value\": \"cpp\", \"text\": \"C++\", \"defaultCode\": \"class Solution {\\r\\npublic:\\r\\n int findNthDigit(int n) {\\r\\n \\r\\n }\\r\\n};\"}, {\"value\": \"java\", \"text\": \"Java\", \"defaultCode\": \"class Solution {\\n public int findNthDigit(int n) {\\n \\n }\\n}\"}, {\"value\": \"python\", \"text\": \"Python\", \"defaultCode\": \"class Solution(object):\\r\\n def findNthDigit(self, n):\\r\\n \\\"\\\"\\\"\\r\\n :type n: int\\r\\n :rtype: int\\r\\n \\\"\\\"\\\"\\r\\n \"}, {\"value\": \"python3\", \"text\": \"Python3\", \"defaultCode\": \"class Solution:\\n def findNthDigit(self, n):\\n \\\"\\\"\\\"\\n :type n: int\\n :rtype: int\\n \\\"\\\"\\\"\\n \"}, {\"value\": \"c\", \"text\": \"C\", \"defaultCode\": \"int findNthDigit(int n) {\\r\\n \\r\\n}\"}, {\"value\": \"csharp\", \"text\": \"C#\", \"defaultCode\": \"public class Solution {\\r\\n public int FindNthDigit(int n) {\\r\\n \\r\\n }\\r\\n}\"}, {\"value\": \"javascript\", \"text\": \"JavaScript\", \"defaultCode\": \"/**\\r\\n * @param {number} n\\r\\n * @return {number}\\r\\n */\\r\\nvar findNthDigit = function(n) {\\r\\n \\r\\n};\"}, {\"value\": \"ruby\", \"text\": \"Ruby\", \"defaultCode\": \"# @param {Integer} n\\r\\n# @return {Integer}\\r\\ndef find_nth_digit(n)\\r\\n \\r\\nend\"}, {\"value\": \"swift\", \"text\": \"Swift\", \"defaultCode\": \"class Solution {\\r\\n func findNthDigit(_ n: Int) -> Int {\\r\\n \\r\\n }\\r\\n}\"}, {\"value\": \"golang\", \"text\": \"Go\", \"defaultCode\": \"func findNthDigit(n int) int {\\r\\n \\r\\n}\"}, {\"value\": \"scala\", \"text\": \"Scala\", \"defaultCode\": \"object Solution {\\n def findNthDigit(n: Int): Int = {\\n \\n }\\n}\"}, {\"value\": \"kotlin\", \"text\": \"Kotlin\", \"defaultCode\": \"class Solution {\\n fun findNthDigit(n: Int): Int {\\n \\n }\\n}\"}]", "create_time": "2018-07-26 13:05:19", "desc": "<p>Find the <i>n</i><sup>th</sup> digit of the infinite integer sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... </p>\r\n\r\n<p><b>Note:</b><br />\r\n<i>n</i> is positive and will fit within the range of a 32-bit signed integer (<i>n</i> < 2<sup>31</sup>).\r\n</p>\r\n\r\n<p><b>Example 1:</b>\r\n<pre>\r\n<b>Input:</b>\r\n3\r\n\r\n<b>Output:</b>\r\n3\r\n</pre>\r\n</p>\r\n\r\n<p><b>Example 2:</b>\r\n<pre>\r\n<b>Input:</b>\r\n11\r\n\r\n<b>Output:</b>\r\n0\r\n\r\n<b>Explanation:</b>\r\nThe 11th digit of the sequence 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ... is a 0, which is part of the number 10.\r\n</pre>\r\n</p>", "difficulty": 1, "frequency": 285.832, "id": 466, "is_locked": 0, "lid": 400, "qid": 400, "submit_url": "/problems/nth-digit/submit/", "title": "Nth Digit", "title_slug": "nth-digit", "type": 0, "update_time": "2018-07-26 12:53:44" }, "msg": "success" } """ problem = ProblemService.get_problem_by_title_slug(title_slug) return jsonify(json_resp(data=model2dict(problem)))
def get_post_by_id(id): post = PostService.get_post_by_id(id) post = model2dict(post) post['category'] = GroupService.get_category_by_id( post['category_id']).name L = [] if post['tag_ids'] is None or post['tag_ids'] == '': post['tags'] = [] else: tag_ids = post['tag_ids'].split(',') for id in tag_ids: tag_name = GroupService.get_tag_by_id(id).name L.append(tag_name) post['tags'] = L post['image_url'] = ImageService.get_image_by_id( post['image_header_id']).url return jsonify(json_resp(data=post))
def list_companies(): """ @api {get} /api/v1/problems/companies 列出所有公司 @apiVersion 1.0.0 @apiGroup LeetCode Problems @apiError {int} code -1 失败,1成功,3参数错误,4资源没找到,5系统错误 @apiSuccessExample {json} Success-Response: { "code": 1, "data": [ "Adobe", "Aetion", "Affirm", "Airbnb", "Akuna Capital", "Alibaba", "Amazon" ], "msg": "success" } """ companies = ProblemService.list_companies_order_by_problem_cnt() return jsonify(json_resp(data=companies))
def delete_post_by_id(id): post = PostService.change_post_status(id, Post.Status.DELETED.value) return jsonify(json_resp(data=model2dict(post)))
def list_leetcode_problems_by_frequency(): """ @api {get} /api/v1/problems/frequency 获取题目列表(根据题目频率排序) @apiVersion 1.0.0 @apiGroup LeetCode Problems @apiParam {int} [page] 页码 默认为1 @apiParam {int} [difficulty] 难度:1 easy, 2 medium, 3 hard @apiParam {int} [is_locked] 是否上锁:0否, 1是 @apiParam {int} [type] 题目类型:0算法,1数据库 @apiParam {int} [order] 排序类型:0升序,1降序 @apiSuccess {int} lid 题号 @apiSuccess {int} qid LeetCode题目Id @apiSuccess {String} title 题目标题 @apiSuccess {int} difficulty 难度:1 easy, 2 medium, 3 hard @apiSuccess {int} is_locked 是否上锁:0否, 1是 @apiSuccess {int} type 题目类型:0算法,1数据库 @apiSuccess {int} difficulty 难度:1 easy, 2 medium, 3 hard @apiSuccess {float} frequency 题目热度 @apiSuccess {String} title_slug 题目title的url缩写 @apiSuccess {String} submit_url 题目代码提交url @apiError {int} code -1 失败,1成功,3参数错误,4资源没找到,5系统错误 @apiSuccessExample {json} Success-Response: { "code": 200, "data": { "data": [ { "create_time": "2018-07-26 11:56:05", "difficulty": 3, "frequency": 104.272, "id": 315, "is_locked": 1, "lid": 571, "qid": 571, "submit_url": "/problems/find-median-given-frequency-of-numbers/submit/", "title": "Find Median Given Frequency of Numbers", "title_slug": "find-median-given-frequency-of-numbers", "type": 0, "update_time": "2018-07-26 13:01:31" } ], "max_cnt": 1, "max_page": 1, "page": 1, "page_size": 50 }, "msg": "success" } """ form = ListProblemsForm(formdata=request.args) validate_form(form) page_size = app.config['PAGE_LARGE'] if form.order is not None and form.order.data == 0: problems = ProblemService.list_problems_order_by_frequency_asc( form.page.data, page_size, difficulty=form.difficulty.data, is_locked=form.is_locked.data, type=form.type.data) else: problems = ProblemService.list_problems_order_by_frequency_desc( form.page.data, page_size, difficulty=form.difficulty.data, is_locked=form.is_locked.data, type=form.type.data) return jsonify(json_resp(data=model2dict(problems)))
def offline_post_by_id(id): post = PostService.change_post_status(id, Post.Status.DRAFT.value) return jsonify(json_resp(data=model2dict(post)))
def list_categories(): categories = GroupService.list_categories() categories = models2dict(categories) return jsonify(json_resp(data=categories))
def list_tags(): tags = GroupService.list_tags() tags = models2dict(tags) return jsonify(json_resp(data=tags))
def count_drafts(): cnt = PostService.count_drafts() data = {'num': cnt} return jsonify(json_resp(data=data))