def on_list(self, request): res = {'error': 1} if request.method == 'GET': keyword = request.args.get('keyword') user_id = request.args.get('user_id') page = cvtools.num(request.args.get('page')) per_page = cvtools.num(request.args.get('per_page')) images, count = cvtools.get_list(keyword, user_id, None, "i.created_at DESC", page, per_page) result = [] for img in images: temp = {} temp['ar_id'] = img['id'] temp['src_img'] = cvtools.get_abs_url(conf.UPLOAD_DIR_SRC[1:], img['src_name']) temp['target_type'] = img['target_type'] if temp['target_type'] == conf.TAR_TYPE_IMAGE: temp['target'] = cvtools.get_abs_url(conf.UPLOAD_DIR_TAR_IMG[1:], img['target']) elif temp['target_type'] == conf.TAR_TYPE_VIDEO: temp['target'] = cvtools.get_abs_url(conf.UPLOAD_DIR_TAR_VIDEO[1:], img['target']) else: temp['target'] = img['target'] temp['title'] = img['title'] temp['user'] = img['username'] temp['created_at'] = img['created_at'].isoformat() result.append(temp) res['error'] = 0 res['result'] = result res['total'] = count else: res['error_msg'] = 'request is not GET' response_str = json.dumps([res]) return Response(response_str, mimetype='application/json')
def on_recent(self, request): res = {'error': 1} if request.method == 'POST': user_id = request.form.get('user_id') session_id = request.form.get('session_id') if cvtools.user_logged_in(user_id, session_id): images = cvtools.get_recent_images(user_id) result = [] for img in images: temp = {} temp['ar_id'] = img['id'] temp['src_img'] = cvtools.get_abs_url(conf.UPLOAD_DIR_SRC[1:], img['src_name']) temp['target_type'] = img['target_type'] if temp['target_type'] == conf.TAR_TYPE_IMAGE: temp['target'] = cvtools.get_abs_url(conf.UPLOAD_DIR_TAR_IMG[1:], img['target']) elif temp['target_type'] == conf.TAR_TYPE_VIDEO: temp['target'] = cvtools.get_abs_url(conf.UPLOAD_DIR_TAR_VIDEO[1:], img['target']) else: temp['target'] = img['target'] temp['title'] = img['title'] temp['user'] = img['username'] temp['viewed_at'] = img['created_at'].isoformat() result.append(temp) res['error'] = 0 res['result'] = result else: res['error'] = 2 res['error_msg'] = 'session ended. please login' else: res['error_msg'] = 'request is not POST' response_str = json.dumps([res]) return Response(response_str, mimetype='application/json')
def on_search_image(self, request): res = {'error': 1} if request.method == 'POST': user_id = request.form.get('user_id') session_id = request.form.get('session_id') search_img = 'img' in request.files and request.files['img'] or None if search_img is not None: search_img_name = cvtools.upload_file(conf.UPLOAD_DIR_TEMP, search_img) if cvtools.resize_image(conf.UPLOAD_DIR_TEMP + '/' + search_img_name): if search_img_name is not False: timer_start = time.clock() search_result = cvtools.search_image(conf.UPLOAD_DIR_TEMP + '/' + search_img_name) res['time'] = time.clock() - timer_start if search_result is not False: search_result = search_result[0] res['error'] = 0 res['found'] = 1 res['title'] = search_result['title'] res['user'] = search_result['username'] res['src_img'] = cvtools.get_abs_url(conf.UPLOAD_DIR_SRC[1:], search_result['src_name']) res['target_type'] = search_result['target_type'] res['target_pos'] = search_result['pos'] if search_result['target_type'] == conf.TAR_TYPE_IMAGE: res['target'] = cvtools.get_abs_url(conf.UPLOAD_DIR_TAR_IMG[1:], search_result['target']) elif search_result['target_type'] == conf.TAR_TYPE_VIDEO: res['target'] = cvtools.get_abs_url(conf.UPLOAD_DIR_TAR_VIDEO[1:], search_result['target']) else: res['target'] = search_result['target'] # adding views if cvtools.user_logged_in(user_id, session_id): cvtools.insert_user_view(user_id, search_result['id']) else: res['error'] = 0 res['found'] = 0 else: res['error_msg'] = 'search image upload failed' else: res['error_msg'] = 'error while resizing image' else: res['error_msg'] = 'img not recieved' response_str = json.dumps([res]) return Response(response_str, mimetype='application/json')