def edit_project(): organization_email = get_jwt_identity() project_id = request.form.get('project_id') # check if already in database project = Project.query.filter_by(project_id=project_id).first() if project: if organization_email != project.organization_id: return jsonify("Project does not belong to this account") project.project_name = request.form.get('project_name') project.project_short_description = request.form.get('project_short_description') project.project_description = request.form.get('project_description') project.project_goal = request.form.get('project_goal') project.project_city = request.form.get('project_city') project.project_state = request.form.get('project_state') project.project_country = request.form.get('project_country') project.newspaper_id = request.form.get('newspaper_id') url = '' # print(request.files) # Setting up picture upload if 'project_picture_link' in request.files: # print("Does it come here") f = request.files['project_picture_link'] filename_split = secure_filename(f.filename).split('.') filename = filename_split[0] + str(project_id) + filename_split[1] s3.put_object(ACL='public-read', Bucket='newspark-charity-data', Key=filename, Body=f) project.project_picture_link = 'https://newspark-charity-data.s3.amazonaws.com/' + filename url = 'https://newspark-charity-data.s3.amazonaws.com/' + filename else: if 'project_picture_link' in request.form: project.project_picture_link = request.form.get('project_picture_link') url = request.form.get('project_picture_link') db.session.commit() # Remove the edited versions from the cache sql_query = '''select article_link from articles where project_id1={} or project_id2={} or project_id3={} or project_id4={} or project_id5={} or project_id6={};'''.format(project_id, project_id, project_id, project_id, project_id, project_id) conn = db.engine.connect().connection df = pd.read_sql(sql_query, conn) conn.close() unique_articles = list(df['article_link'].unique()) for i in unique_articles: if redis.exists(i): redis.delete(i) return jsonify("Success") else: return jsonify("Initiative Does Not Exist")
def auth_library_result(openid=None): """查询借书卡绑定结果""" if is_user_exists(openid): redis_prefix = 'wechat:user:auth:library:' errmsg = redis.get(redis_prefix + openid) if errmsg: redis.delete(redis_prefix + openid) return jsonify({'errmsg': errmsg}) else: abort(404) else: abort(404) """ 修改用户分组 """ if form.validate_on_submit(): groupemp = [] groupemp_list = form.group.data.split(',') for t in groupemp_list: tag = Group.query.filter_by(name=t).first() if tag is None: tag = Group() tag.name = t groupemp.append(tag) user.user_group = groupemp user.save() return redirect(url_for('wechat.user')) value = ",".join([i.name for i in user.user_group]) return render_template('wechat/editgroup.html', form=form, user=user, value=value)
def random_private_library_offset(library_type, user_id, refresh=False): if not user_id: return 1 cache_key = private_library_cache_key + str(library_type) + ":" + str( user_id) # 从集合中随机取一个值 rand_id = redis.srandmember(cache_key) # Redis随机取值失败或者需要刷新 if not rand_id or refresh: # 读取数据库数据 if library_type == 11: model_list = ImageModel.query.filter_by(user_id=user_id, type=11, status=1).all() cache_id_list = array_column(model_list, "image_id") else: model_list = VideoModel.query.filter_by(user_id=user_id, type=31, status=1).all() cache_id_list = array_column(model_list, "video_id") # 更新Redis并返回随机值 redis.delete(cache_key) if cache_id_list: redis.sadd(cache_key, *cache_id_list) return random.choice(cache_id_list) else: return 1 else: return rand_id
def redis_add(): goodsTitles = request.get_json() redis.delete(goodsTitles['title']) address_id = Postgres.return_address_and_id(goodsTitles['title']) redis.hmset(goodsTitles['title'], address_id) return '.'
def pending_payments(): k = "pending_payments" if redis.setnx(k, 1): pendings = PendingTransaction.query.order_by(asc(PendingTransaction.created_time)).all() for pending in pendings: do_payment(pending.id) # Release the redis lock after we done working redis.delete(k) else: print "pending_payments is redis locked."
def tearDown(self): redis.delete(app.config['COUNTER_KEY']) app.config.pop('REDIS_URL', None) app.config['COUNTER_KEY'] = self.old_COUNTER_KEY app.config['REDIS_HOST'] = self.old_REDIS_HOST app.config['REDIS_PORT'] = self.old_REDIS_PORT app.config['REDIS_DB'] = self.old_REDIS_DB app.config['TESTING'] = False
def delete_room(): if 'manager_id' not in session: return jsonify(api_format(status.HTTP_406_NOT_ACCEPTABLE, "you are not login")) manager_id = session['manager_id'] room_id = request.get_json()['room_id'] result_room = Room.query.filter_by(id=room_id, manager_id=manager_id).first() if result_room is None: return jsonify(api_format(status.HTTP_404_NOT_FOUND, "room is not existed")) db.session.delete(result_room) Message.query.filter_by(room_id=room_id).delete() db.session.commit() redis.delete(room_id + ':message_num') return jsonify(api_format(status.HTTP_200_OK, "delete finish", {"room_id": result_room.id}))
def server_enable(service, resource): pool_entity = service[0:2].upper() if not has_ability(flask.g, "edit_config", pool_entity): abort(make_response(jsonify(message="insufficient credentials for edit_config " "(entity %s)" % pool_entity), 403)) service_module = get_service(service) if resource not in service_module.list_resources(): abort(make_response(jsonify(message="unknown resource '%s' in '%s'" % (resource, service)), 400)) keyr = "busy:%s:%s" % (service, resource) if redis.exists(keyr): redis.delete("busy:%s:%s" % (service, resource)) return flask.jsonify("ok") else: abort(flask.make_response(flask.jsonify(message="resource was not disabled"), 400))
def delete(cls, id): """ delete any key from database """ # remove id from {model}:all set cls._field_rem('all', id) key = rkey(cls, id) return redis.delete(key)
def get_ticket(imdbId, season='', episode=''): redisRes = redis.get(imdbId) ticket = redisRes.decode("utf-8") if redisRes else None if not ticket: ticket = req(imdbId) else: ticketUsage = redis.get(ticket) ticketUsage = int(ticketUsage) if ticketUsage else 0 # ticket as 50 usage if ticketUsage >= 49: redis.delete(ticket) ticket = req(imdbId) else: redis.incr(ticket) return str(ticket)
def delete_room(): if 'manager_id' not in session: return jsonify( api_format(status.HTTP_406_NOT_ACCEPTABLE, "you are not login")) manager_id = session['manager_id'] room_id = request.get_json()['room_id'] result_room = Room.query.filter_by(id=room_id, manager_id=manager_id).first() if result_room is None: return jsonify( api_format(status.HTTP_404_NOT_FOUND, "room is not existed")) db.session.delete(result_room) Message.query.filter_by(room_id=room_id).delete() db.session.commit() redis.delete(room_id + ':message_num') return jsonify( api_format(status.HTTP_200_OK, "delete finish", {"room_id": result_room.id}))
def post_adminrequest(app, service, action, configname="base", value="1"): identifier = "%d.%d" % (os.getpid(), app._requestid) app._requestid += 1 redis.set("admin:config:%s:%s:%s:%s" % (service, action, configname, identifier), value) wait = 0 while wait < 360: configresult = redis.get("admin:configresult:%s:%s:%s:%s" % (service, action, configname, identifier)) if configresult: break wait += 1 time.sleep(1) if configresult is None: redis.delete("admin:configresult:%s:%s:%s:%s" % (service, action, configname, identifier)) abort(flask.make_response(flask.jsonify(message="request time-out"), 408)) elif configresult != "ok": abort(flask.make_response(flask.jsonify(message=configresult), 400)) return configresult
def delete(self): """ Delete a session (does not send final results) """ # Delete all data associated with this session redis.delete(f'session::{self.id}::emails') redis.delete(f'session::{self.id}::posts') redis.delete(f'session::{self.id}::ip_addresses') redis.delete(f'session::{self.id}') self.__init__() print("Session deleted")
def test_add_project_to_article(self): owner = Owner.query.all()[1] headers = auth_token_helper(owner.username) publisher_name = Owning.query.filter_by(owner_id=owner.username).first().publisher_id article = Article.query.filter_by(publisher_id=publisher_name).first() article_link = article.article_link old_ids = [article.project_id1, article.project_id2, article.project_id3, article.project_id4, article.project_id5, article.project_id6] project_id = article.project_id1 # assumes project id is not None article.project_id1 = None db.session.commit() response = test_app.post( '/add_project_to_article', headers=headers, data=dict( article_link=article_link, project_id=project_id ) ) updated_article = Article.query.filter_by(article_link=article_link).first() ids = [updated_article.project_id1, updated_article.project_id2, updated_article.project_id3, updated_article.project_id4, updated_article.project_id5, updated_article.project_id6] result = response.get_json() self.assertEqual(response.status_code, 200) self.assertEqual(result, 'Success') self.assertTrue(project_id in ids) self.assertTrue(redis.exists(article_link)) # Revert back to original state updated_article.project_id1 = old_ids[0] updated_article.project_id2 = old_ids[1] updated_article.project_id3 = old_ids[2] updated_article.project_id4 = old_ids[3] updated_article.project_id5 = old_ids[4] updated_article.project_id6 = old_ids[5] redis.delete(article_link) db.session.commit()
def test_getCharities1(self): """ log into existing profile """ article = 'https://www.idsnews.com/article/2020/06/wildlife-to-look-for-in-monroe-county' curr_article = Article.query.filter_by(article_link=article).first() if curr_article is not None: db.session.delete(curr_article) db.session.commit() response1 = test_app.post('/get_charities', data=dict( article_link=article, article_title='', article_date='', articel_text='', )) result1 = response1.get_json() self.assertEqual(response1.status_code, 200) self.assertEqual(len(result1), num_choices) self.assertTrue(redis.exists(article)) response2 = test_app.post('/get_charities', data=dict( article_link=article, article_title='', article_date='', article_text='', )) result2 = response2.get_json() self.assertEqual(response1.status_code, 200) self.assertEqual(len(result1), num_choices) self.assertEqual(result1, result2) self.assertTrue(redis.exists(article)) # Delete the article and then make sure the results are the same again curr_article = Article.query.filter_by(article_link=article).first() redis.delete(curr_article.article_link) db.session.delete(curr_article) db.session.commit()
def test_getCharities2(self): article = 'https://www.idsnews.com/article/2020/06/former-iu-mens-soccer-all-american-ken-snow-dies-at-50' curr_article = Article.query.filter_by(article_link=article).first() if curr_article is not None: db.session.delete(curr_article) db.session.commit() response1 = test_app.post('/get_charities', data=dict( article_link=article, article_title='blah blah', article_date='Jun 29 2020', article_text='here is some text', )) result1 = response1.get_json() self.assertEqual(response1.status_code, 200) self.assertEqual(len(result1), num_choices) self.assertTrue(redis.exists(article)) response2 = test_app.post('/get_charities', data=dict( article_link=article, article_title='blah bah', article_date='Jun 23 2020', article_text='here is some text', )) result2 = response2.get_json() self.assertEqual(response1.status_code, 200) self.assertEqual(len(result1), num_choices) self.assertEqual(result1, result2) self.assertTrue(redis.exists(article)) # Delete the article and then make sure the results are the same again curr_article = Article.query.filter_by(article_link=article).first() redis.delete(curr_article.article_link) db.session.delete(curr_article) db.session.commit()
def change_widget_status(): """ Change the status of the widget for an article :param: JWT token for a owner, article link :return: Success if widget status changed, Article Not Found if article not found in database, or Publisher Not Found if owner not found in database """ username = get_jwt_identity() exists = Owner.query.filter_by(username=username).first() if exists: article_link = request.form.get('article_link') article = Article.query.filter_by(article_link=article_link).first() if article: # Check if the value is in the cache if not article.widget_status and redis.exists(article.article_link): redis.delete(article.article_link) # Checks admin privileges for account if username == 'admin': article.edited_by_newspark = True else: article.edited_by_publisher = True article.widget_status = not article.widget_status db.session.commit() return jsonify("Success") else: return jsonify("Article Not Found") else: return jsonify("Account Not Found")
def edit_organization(): current_org = get_jwt_identity() # check if already in database exists = Organization.query.filter_by(email=current_org).first() if exists: password = request.form.get('password') if password: salt, hashed_password = secure_password(password) exists.password = hashed_password exists.salt = salt exists.phone_number = request.form.get('phone_number') exists.organization_website = request.form.get('organization_website') exists.organization_name = request.form.get('organization_name') exists.organization_description = request.form.get('organization_description') exists.organization_street_address = request.form.get('organization_street_address') exists.organization_city = request.form.get('organization_city') exists.organization_state = request.form.get('organization_state') exists.organization_country = request.form.get('organization_country') exists.organization_type = request.form.get('organization_type') if request.form.get('year_established'): exists.year_established = request.form.get('year_established') db.session.commit() for i in redis.keys(): data = pickle.loads(redis.get(i)) for j in data: if j['organization_id'] == current_org: redis.delete(i) break return jsonify("Success") else: return jsonify("Organization Does Not Exist")
def bi(): app = create_app('test') app_context = app.app_context() app_context.push() target_date = '20190626' name = os.path.join(os.getcwd(), current_app.config['STATISTICS_TAG'], 'bi', target_date, 'report_d.json') if os.path.exists(name): print('read_report_d file exist') else: print('read_report_d file not exist') return True # if BiReport.query.filter_by(record_time=target_date).first() is not None: # print('read_report_d already exist') # return True dbreports = BiReportProtocol.query.filter_by(status=1).all() for re in dbreports: cache.set(str(re.we_id), '1', timeout=60 * 10) redis.lpush("BiReportProtocol", str(re.we_id)) bi_default = BiReport() with open(os.path.join(os.getcwd(), current_app.config['STATISTICS_TAG'], 'bi', target_date, 'report_d.json'), 'r') as file: data = file.readlines() for report in data: try: report = json.loads(report) if report['we_id'] != 'default': print(report['we_id']) data = cache.get(report['we_id']) if data: bi_report = BiReport() bi_report.record_time = report['record_time'] bi_report.we_id = report['we_id'] bi_report.rank_index = report['rank_index'] bi_report.latent_consumer_index = report['latent_consumer_index'] bi_report.activite_consumer_index = report['activite_consumer_index'] bi_report.extend_work_heat = report['extend_work_heat'] bi_report.sale_work_heat = report['sale_work_heat'] bi_report.income_index = report['income_index'] bi_report.pay_index = report['pay_index'] bi_report.v_webusiness_index = report['v_webusiness_index'] # db.session.add(bi_report) print('add bi_report') cache.set(str(report['we_id']), '2', timeout=60 * 10) else: bi_default.record_time = report['record_time'] bi_default.we_id = report['we_id'] bi_default.rank_index = report['rank_index'] bi_default.latent_consumer_index = report['latent_consumer_index'] bi_default.activite_consumer_index = report['activite_consumer_index'] bi_default.extend_work_heat = report['extend_work_heat'] bi_default.sale_work_heat = report['sale_work_heat'] bi_default.income_index = report['income_index'] bi_default.pay_index = report['pay_index'] bi_default.v_webusiness_index = report['v_webusiness_index'] except Exception as e: print('read_report_d: ', e) db.session.commit() all_data = redis.lrange('BiReportProtocol', 0, -1) for key in all_data: print(key) data = cache.get(key.decode()) print(data) if data == '1': print('add') bi_report = BiReport() bi_report.record_time = bi_default.record_time bi_report.we_id = key.decode() bi_report.rank_index = bi_default.rank_index bi_report.latent_consumer_index = bi_default.latent_consumer_index bi_report.activite_consumer_index = bi_default.activite_consumer_index bi_report.extend_work_heat = bi_default.extend_work_heat bi_report.sale_work_heat = bi_default.sale_work_heat bi_report.income_index = bi_default.income_index bi_report.pay_index = bi_default.pay_index bi_report.v_webusiness_index = bi_default.v_webusiness_index db.session.add(bi_report) db.session.commit() redis.delete('BiReportProtocol') print('read_report_d finish')
def remove_token(password): redis.delete(_PASSWORD_PREFIX + password)
def delete_token_redis(user, mode): name = f'{user.id}{mode.lower()}' return redis.delete(name)
def clear(self): redis.delete(self.cache_key)
def delete_msg(key): redis.delete(key) return Response(status=204)
def delete(key): redis.delete(key) return redirect(url_for('blog.index'))
from app.models import * from app import redis from config import STELLAR_ADDRESS # Make sure to start from a completely empty database # Inside psql, use command "DROP DATABASE <DATABASE NAME>;" to completely remove the database # Then use command "CREATE DATABASE <DATABASE NAME>;" to recreate a fresh one # Create the initial database tables db.drop_all() db.create_all() # Create the user and admin roles user_role = Role(name='user', description='user') admin_role = Role(name='admin', description='admin') db.session.add(user_role) db.session.add(admin_role) db.session.commit() # Create the first admin user admin_user = User(email='your-email', username='******', password='******') # Give admin user admin rights admin_user.roles.append(admin_role) db.session.add(admin_user) db.session.commit() # Reset the redis locks redis.delete('pending_payments')
def remove_project_from_article(): """ Removes project from this article :param: JWT token for a owner, article link, project id :return: Success if project removed from this article """ username = get_jwt_identity() exists = Owner.query.filter_by(username=username).first() if exists: article_link = request.form.get('article_link') project_id = int(request.form.get('project_id')) article = Article.query.filter_by(article_link=article_link).first() if article: ids = article.get_project_ids() for i in range(len(ids)): if ids[i] == project_id: ids[i] = None ids = sorted(ids, key=lambda x: x is None) # move None's to back article.project_id1 = ids[0] article.project_id2 = ids[1] article.project_id3 = ids[2] article.project_id4 = ids[3] article.project_id5 = ids[4] article.project_id6 = ids[5] if username == 'admin': article.edited_by_newspark = True else: article.edited_by_publisher = True db.session.commit() sql_query = '''select project_id from projects, organizations where projects.organization_id=organizations.email and projects.removed=FALSE and organizations.verified=TRUE;''' conn = db.engine.connect().connection df = pd.read_sql(sql_query, conn) other_ids = list(filter(lambda x: not (x in ids), list(df['project_id']))) # store recommendations in a json file and upload to a s3 bucket update_recommendations(s3_client=s3, article_link=article_link, ids=ids, other_ids=other_ids) # Re-run the commands to get the right data for the articles in the cache # TODO: set up a celery worker to do all of this if redis.exists(article_link): redis.delete(article_link) project_info_list = get_projects_from_article(article_url=article_link, num_ids=application.config['NUM_CHOICES']) # Save to redis cache redis.set(article_link, pickle.dumps(project_info_list)) return jsonify("Success") else: return jsonify("Article does not exist.") else: return jsonify("Publisher does not exist.")
def add_order(): orders = request.get_json() shopToStocks = Neo.get_all(orders['id_shop']) new_order = [] shopToStocks.sort(key=lambda x: x[1]) for goods in orders['goods']: redisdata = redis.hgetall(goods) if redisdata == {}: update_redis() all_stocks = {k: int(v) for k, v in redisdata.items()} for i in range(len(shopToStocks)): id_stock = shopToStocks[i][0] if id_stock in all_stocks.values(): try: cur_order = next(item for item in new_order if item['id_stock'] == id_stock) except StopIteration: cur_order = {} cur_quantity = Postgres.return_quantity(id_stock, goods) new_quantity = cur_quantity[0] - orders['goods'][goods] if new_quantity >= 0: if cur_order == {}: cur_order.update({'id_shop': orders['id_shop']}) cur_order.update({'id_stock': id_stock}) cur_order.update({'goods': {}}) cur_order['goods'].update( {goods: orders['goods'][goods]}) new_order.append(cur_order) else: new_order.remove(cur_order) cur_order['goods'].update( {goods: orders['goods'][goods]}) new_order.append(cur_order) if new_quantity == 0: Postgres.delete_state_by_id(id_stock, goods) redis.delete(goods) address_id = Postgres.return_address_and_id(goods) redis.hmset(goods, address_id) else: Postgres.update_quantity(goods, id_stock, new_quantity) break else: continue for order in new_order: id_order = Mongo.insert('orders', order, returned=True) body = { "date": f"{datetime.now():%Y-%m-%d}", "id_order": str(id_order) } es.index(index='orders', body=body) return '.'
def delete_from_redis(user, mode): name = f'{user.id}_{mode.lower()}' redis.delete(name)
def release(lock_key): if redis.get(lock_key): redis.delete(lock_key)
def clear_cache(): for i in redis.keys(): redis.delete(i)
def do_payment(pending_id): # Create locked celery job (using redis) to send payment k = "do_payment:%s" % pending_id if redis.setnx(k, 1): redis.expire(k, LOCK_EXPIRE) pending = PendingTransaction.query.get(pending_id) # Only do the task if signed is not completed already if pending.tx_signed is False: r = tx_sign(pending.destination, pending.amount, pending.sequence) if r['result']['status'] == 'success' and 'tx_blob' in r['result']: # Signing was successful pending.tx_blob = r['result']['tx_blob'] pending.tx_hash = r['result']['tx_json']['hash'] pending.tx_signed = True db.session.add(pending) db.session.commit() # Also save info to Payable payable = Payable.query.get(pending.payable) payable.tx_blob = pending.tx_blob payable.tx_hash = pending.tx_hash payable.tx_signed = pending.tx_signed db.session.add(payable) db.session.commit() if pending.tx_signed is True and pending.tx_submitted is False: # Check if tx has already been submitted successfully verify = verify_tx(pending.tx_hash) if verify['result']['status'] == 'success' and 'validated' in r['result']: if verify['result']['validated'] is True: pending.tx_submitted = True db.session.add(pending) db.session.commit() else: r = submit_tx_blob(pending.tx_blob) if r['result']['status'] == 'success': if r['result']['engine_result_code'] == 0: # payment was successful tx = r['result']['tx_json'] pending.tx_submitted = True db.session.add(pending) db.session.commit() payable = Payable.query.get(pending.payable) payable.tx_submitted = True db.session.add(payable) db.session.commit() if pending.tx_signed is True and pending.tx_submitted is True and pending.tx_validated is False: # Verify tx was confirmed and validated r = verify_tx(pending.tx_hash) if r['result']['status'] == 'success' and 'validated' in r['result']: if r['result']['validated'] is True: # tx has been confirmed tx = r['result'] payable = Payable.query.get(pending.payable) payable.tx_validated = True payable.account_fulfilled = tx['Account'] payable.amount_fulfilled = tx['Amount'] payable.destination_fulfilled = tx['Destination'] payable.fee_fulfilled = tx['Fee'] payable.flags_fulfilled = tx['Flags'] payable.sequence_fulfilled = tx['Sequence'] payable.signing_pub_key_fulfilled = tx['SigningPubKey'] payable.transaction_type_fulfilled = tx['TransactionType'] payable.tx_signature_fulfilled = tx['TxnSignature'] payable.tx_hash_fulfilled = tx['hash'] payable.ledger_index_fulfilled = tx['ledger_index'] db.session.add(payable) db.session.commit() db.session.delete(pending) db.session.commit() else: # Validation typically takes 5 seconds to confirm # celery retry is bugging out the redis lock release pass if pending.tx_signed is True and pending.tx_submitted is True and pending.tx_validated is True: # If all 3 conditions are true, the pending entry should be deleted db.session.delete(pending) db.session.commit() # Release the redis lock after we're done working on it redis.delete(k) else: # The payment is redis locked print "Aborting: the payment is currently locked."
def reset_game(game_id): redis.delete(game_id) return "OK"
def _verification(uuid): if not int(rd.hget(uuid, 'success').decode('utf8')): raise Forbidden() rd.delete(uuid)