def getPosts(): current_user = get_jwt_identity() current_user_claims = get_jwt_claims() data = [] queueId = request.args.get("queue_id", None) userId = current_user_claims.get('id', None) page_offset = request.args.get("page", None) page_limit = 10 with db_session() as s: data_posts = s.query(Post) \ .join(Queue) \ .filter( #Post.queue_id == queueId, Queue.user_id == userId ).limit(page_limit).offset(page_offset) for post in data_posts: print(post.id) data = [{ "id": x.id, "title": x.title, "message": x.message, "queue": x.queue_id } for x in data_posts] return jsonify(data)
def session(request, db): session = db_session() def teardown(): session.rollback() request.addfinalizer(teardown) return session
def db_update_artist_unfollow(self, user): db = db_session() db.execute(""" UPDATE public.artists SET follows = False, updated_on = now() WHERE artist_full_path = '{}' """.format(user)) db.commit()
def db_update_artist_follow(self, user, watch): db = db_session() db.execute(f""" UPDATE {watch['table']} SET {watch['follows']} = True, updated_on = now() WHERE artist_full_path = '{user}' """) db.commit()
def test_session_is_close(app): """ GIVEN a Flask application WHEN when the app context if closed THEN check if the session is clean """ with app.app_context(): from app.database import db_session from app.model.models import User assert db_session()._is_clean() db_session.add(User()) assert not db_session()._is_clean() # the session returned to pool assert db_session()._is_clean()
def test_encode_auth_token(self): user = User( email='*****@*****.**', password='******' ) with db_session() as s: s.add(user) s.commit() auth_token = user.encode_auth_token(user.id)
def db_i_follow_follow_me(self): db = db_session() i_follow = db.execute(""" SELECT * FROM watched_artists WHERE follows_me = FALSE""") follows = db.execute(""" SELECT * FROM artists WHERE follows = TRUE""") if_df = pd.DataFrame(i_follow) f_df = pd.DataFrame(follows) results = set(if_df[5]).intersection(set(f_df[3])) return results
def deleteUsers(userId): if userId: with db_session() as s: s.query(User).filter( User.id == userId, ).delete() s.commit() return { "msg": f"User '{userId}' deleted." }, status.HTTP_200_OK return { f"msg": "An error occured on deleting user." }, status.HTTP_401_BAD_REQUEST
def db_get_followers(self, watch): """ returns artists following me or i am following :param watch: :return: """ db = db_session() artist_list = db.execute(f""" SELECT artist_name, artist_full_path FROM {watch['table']} WHERE {watch['follows']} = True """) return artist_list
def db_update_artist_info(self, user_dict): db = db_session() db.execute(""" UPDATE public.artists SET artist_active = {}, artist_telegram = '{}', artist_twitter = '{}', updated_on = now() WHERE artist_full_path = '{}' """.format(user_dict['active'], user_dict['telegram'], user_dict['twitter'], user_dict['full_path'])) db.commit()
def db_update_stats(self, stats_dict): db = db_session() prev_watchers = db.execute(""" SELECT current_watchers from profile_stats where id = (select MAX(id) from profile_stats)""" ) db.execute( """INSERT INTO profile_stats (current_watchers, previous_watchers, watching, views, faves, updated_on, created_on) VALUES ( {}, {}, {}, {}, {}, now(), now())""".format( stats_dict['watchers'], prev_watchers.fetchone()['current_watchers'], stats_dict['watching'], stats_dict['views'], stats_dict['faves'])) db.commit()
def deletePosts(postId): #queueId = request.json.get("queue_id", type=int) #userId = request.json.get("user_id", default=1, type=int) if postId: with db_session() as s: s.query(Post).filter(Post.id == postId).delete() s.commit() return {"msg": f"Post '{postId}' deleted."}, status.HTTP_200_OK return { f"msg": "An error occured on deleting post." }, status.HTTP_400_BAD_REQUEST
def db_set_follow_me(self, results): db = db_session() if len(results) > 0: for user in list(results): _log.info(f"[FOLLOW HARMONY] {user} follows me back!") user = str(user) db.execute(f""" UPDATE watched_artists SET follows_me = TRUE, follows_me_id = (SELECT id FROM artists WHERE artist_full_path = '{user}'), updated_on = now() WHERE artist_full_path = '{user}'""") _log.info(f"[FOLLOW HARMONY] {user} updated!") db.commit() else: _log.info("[FOLLOW HARMONY] No new follow backs.")
def postUsers(): userName = request.form.get("user_name", type=str) if userName: with db_session() as s: newUser = User( username = userName, ) s.add(newUser) s.commit() return { "msg": f"User '{userName}' created." }, status.HTTP_201_CREATED return { f"msg": "An error occured on creating user." }, status.HTTP_401_BAD_REQUEST
def putUsers(userId): userName = request.form.get("user_name", type=str) if userName: with db_session() as s: s.query(User).filter( User.id == userId, ).update({ User.username: userName, }) s.commit() return { "msg": f"User '{userId}' updated." }, status.HTTP_200_OK return { f"msg": "An error occured on updating user." }, status.HTTP_401_BAD_REQUEST
def db_update_watching_info(self, user_dict, user_stats): db = db_session() db.execute(f""" UPDATE watched_artists SET artist_active = {user_dict['active']}, artist_telegram = '{user_dict['telegram']}', artist_twitter = '{user_dict['twitter']}', watchers = {user_stats['watchers']}, watching = {user_stats['watching']}, views = {user_stats['views']}, faves = {user_stats['faves']}, updated_on = now() WHERE artist_full_path = '{user_dict['full_path']}' """) db.commit()
def test_session_shutdown(app, mocker): """ GIVEN the shutdown_session() function WHEN shutdown_session() is call THEN check if the session is clean """ from app.database import init, shutdown_session, db_session from app.database import shutdown_session init(app) db_session = mocker.MagicMock() shutdown_session() assert db_session()._is_clean()
def deleteQueues(queueId): current_user = get_jwt_identity() current_user_claims = get_jwt_claims() userId = current_user_claims.get('id') if queueId: with db_session() as s: s.query(Queue).filter(Queue.id == queueId, Queue.user_id == userId).delete() return {"msg": f"Queue '{queueId}' deleted."}, status.HTTP_200_OK return { f"msg": "An error occured on deleting queue." }, status.HTTP_400_BAD_REQUEST
def db_add_artists_names(self, names_dict, watch): db = db_session() heart_beat_counter = 0 db_tools = self.db_watch_toolbox(watch=watch) for art_entry in names_dict: user_name = art_entry['user_name'] artist_full_path = art_entry['user_path'] db.execute(f""" INSERT INTO {db_tools['table']} (artist_name, {db_tools['follows']}, artist_full_path) VALUES ( '{user_name}',{True},'{artist_full_path}' )""") db.commit() heart_beat_counter += 1 heartbeat = (heart_beat_counter / 100) % 2 if heartbeat == 0 or heartbeat == 1: _log.info(f"[DATABASE][HEARBEAT] {heart_beat_counter} added!") _log.info(f"[DATABASE] {heart_beat_counter} artists added!!")
def getUsers(): data = [] page_offset = request.args.get("page", default=0, type=int) page_limit = 10 with db_session() as s: data_users = s.query(User).filter() \ .limit(page_limit) \ .offset(page_offset) for user in data_users: print(user.id) data = [ { "id": x.id, "email": x.email } for x in data_users ] return jsonify(data)
def queueQueues(): current_user = get_jwt_identity() current_user_claims = get_jwt_claims() queueName = request.json.get("name", None) queueCron = request.json.get("cron", None) userId = current_user_claims.get('id', None) if queueName and queueCron and userId: with db_session() as s: newQueue = Queue(name=queueName, cron=queueCron, user_id=userId) s.add(newQueue) return { "msg": f"Queue '{queueName}' created." }, status.HTTP_201_CREATED return { f"msg": "An error occured on creating queue." }, status.HTTP_400_BAD_REQUEST
def getPostsByQueueId(queueId): current_user = get_jwt_identity() current_user_claims = get_jwt_claims() data = [] page_offset = request.args.get("page", 0) page_limit = 10 with db_session() as s: data_posts = s.query(Post).filter( Post.queue_id == queueId).limit(page_limit).offset(page_offset) for post in data_posts: print(post.id) data = [{ "id": x.id, "title": x.title, "message": x.message, "queue": x.queue_id } for x in data_posts] return jsonify(data)
def getQueuesById(queueId): current_user = get_jwt_identity() current_user_claims = get_jwt_claims() data = [] userId = current_user_claims.get('id', None) page_offset = request.args.get("page", default=0, type=int) page_limit = 10 with db_session() as s: data_queues = s.query(Queue).filter( Queue.user_id == userId).limit(page_limit).offset(page_offset) #for queue in data_queues: # print(queue.id) data = [{ "id": x.id, "title": x.name, "message": x.cron, "user": x.user_id } for x in data_queues] return jsonify(data)
def postPostByQueueId(queueId): current_user = get_jwt_identity() current_user_claims = get_jwt_claims() postTitle = request.json.get("title", None) postMessage = request.json.get("message", None) #queueId = request.json.get("queue_id", None) userId = current_user_claims.get('id', None) if postTitle and postMessage and queueId: with db_session() as s: newPost = Post(title=postTitle, message=postMessage, queue_id=queueId) s.add(newPost) return { "msg": f"Post '{postTitle}' created." }, status.HTTP_201_CREATED return { f"msg": "An error occured on creating post." }, status.HTTP_400_BAD_REQUEST
def putPosts(postId): current_user = get_jwt_identity() current_user_claims = get_jwt_claims() postTitle = request.json.get("title", None) postMessage = request.json.get("message", None) queueId = request.json.get("queue_id", None) userId = request.json.get("user_id", None) if postId and postTitle and postMessage: with db_session() as s: s.query(Post).filter(Post.id == postId).update({ Post.title: postTitle, Post.message: postMessage }) return {"msg": f"Post '{postId}' updated."}, status.HTTP_200_OK return { f"msg": "An error occured on updating post." }, status.HTTP_400_BAD_REQUEST
def putQueues(queueId): current_user = get_jwt_identity() current_user_claims = get_jwt_claims() queueName = request.json.get("name", None) queueCron = request.json.get("cron", None) userId = current_user_claims.get('id', None) if (queueName or queueCron) and userId: with db_session() as s: if queueName: s.query(Queue).filter(Queue.id == queueId, Queue.user_id == userId).update( {Queue.name: queueName}) if queueCron: s.query(Queue).filter(Queue.id == queueId, Queue.user_id == userId).update( {Queue.cron: queueCron}) return {"msg": f"Queue '{queueId}' updated."}, status.HTTP_200_OK return { f"msg": "An error occured on updating queue." }, status.HTTP_400_BAD_REQUEST
def get_twitter_list(self): db = db_session() return db.execute(""" SELECT artist_twitter from public.artists where artist_twitter not like 'None' """)
def db_get_artists(self): db = db_session() results = db.execute(""" SELECT artist_full_path FROM public.artists """) return results
def db_get_artists_social_update(self, watch): db_tools = self.db_watch_toolbox(watch=watch) db = db_session() return db.execute(f""" SELECT artist_full_path FROM {db_tools['table']} where artist_twitter is null """)
import time from settings import ConfigApp from http.client import HTTPException from app import create_app, make_celery, setdate from sqlalchemy import and_ from celery.utils.log import get_task_logger from app.event.models import EventModel, MemberModel from app.database import db_session from flask_mail import Mail, Message from collections import defaultdict # Creating App app = create_app(ConfigApp) Session = db_session() # Email initialization mail = Mail(app) # Creating celery app logger = get_task_logger(__name__) celery = make_celery(app) # Async Task @celery.task(name="async_task", auto_retry=[HTTPException], max_retries=5) def send_email(data): # Sending more than one email in one connection according to setting with mail.connect() as conn: # iterating email address for email in data['email']: msg = Message(data['subject'], sender="*****@*****.**",