def post(self): config = get_config() subvoat_utils = SubvoatUtils(self.db) user_utils = UserUtils(self.db) parser = reqparse.RequestParser() return_data = [] parser.add_argument('subvoat_name') args = parser.parse_args() schema = Schema({ Required('subvoat_name'): All(str, Length(min=config['min_length_subvoat_name']))}) try: schema({'subvoat_name':args.get('subvoat_name')}) except MultipleInvalid as e: return {'error':'%s %s' % (e.msg, e.path)} threads = subvoat_utils.get_all_threads(args['subvoat_name']) # see the thread schema in voat_sql/schemas/subvoat_schema.py # I convert the user_id to username for t in threads: user_status, user_result = user_utils.get_user_by_id(t.user_id) if user_status: username = user_result.username else: # NEED TO LOG THIS continue c = 0 for v in t.votes: c += v.direction return_data.append({'uuid':t.uuid, 'title':t.title, 'body':t.body, 'username':username, 'creation_date':t.creation_date.isoformat(), 'votes':c}) return {'result':return_data}
def post(self): user_utils = UserUtils() parser.add_argument('username') parser.add_argument('api_token') args = parser.parse_args() user = user_utils.authenticate_by_token(args.get('username'), args.get('api_token')) if not user: return {'error': 'incorrect login'} config = get_config() return {'result': config}
def post(self): user_utils = UserUtils(self.db) parser = reqparse.RequestParser() # prob need some sort of max length limit parser.add_argument('username') parser.add_argument('password') args = parser.parse_args() result, user = user_utils.authenticate_by_password(args.get('username'), args.get('password')) if result == False: return {'error':'incorrect login'} return {'result':{'api_token':user.api_token, 'username':user.username}}
def post(self): user_utils = UserUtils(self.db) db = get_db() parser = reqparse.RequestParser() parser.add_argument('username') parser.add_argument('password') args = parser.parse_args() status, result = user_utils.add_user(password=args.get('password'), username=args.get('username')) if status == True: return {'success': result} return {'error': result}
def post(self): parser = reqparse.RequestParser() parser.add_argument('thread_uuid') parser.add_argument('direction') parser.add_argument('username') parser.add_argument('api_token') args = parser.parse_args() user_utils = UserUtils(self.db) subvoat_utils = SubvoatUtils(self.db) uuid_ = args.get('thread_uuid') user = user_utils.authenticate_by_token(args.get('username'), args.get('api_token')) if not user: return {'error': 'incorrect login'} try: direction = int(args.get('direction')) except Exception as e: return {'error': 'incorrect direction'} status, result = subvoat_utils.vote_thread(thread_uuid=uuid_, direction=direction, user_id=user.id) if status == True: return {'result': result} return {'error': result}
class Admin(): def __init__(self): self.user_utils = UserUtils() self.db = get_db() self.classes = self.db.base.classes def add_admin(self, username, password): q = self.db.session.query(self.classes.user).filter( self.classes.user.username == username).first() if q: c = input( 'User already exists, this will delete the existing user. Continue?: [y/n] ' ) if c != 'y' and c != 'yes': return else: self.db.session.delete(q) self.db.session.commit() one_month = datetime.datetime.utcnow() + relativedelta(months=1) new_admin = self.user_utils.create_user_object( password_hash=pwd_context.encrypt(password), api_token=str(uuid.uuid4()), registration_date=datetime.datetime.utcnow(), username=username, verified=True, site_admin=True) self.db.session.add(new_admin) if not self.db.session.commit(): print('Admin added') return print('Unable to add admin')
def __init__(self): self.user_utils = UserUtils() self.db = get_db() self.classes = self.db.base.classes
def __init__(self, db): self.db = db self.config = get_config() self.user_utils = UserUtils(self.db) self.session = db.session()
class SubvoatUtils(): def __init__(self, db): self.db = db self.config = get_config() self.user_utils = UserUtils(self.db) self.session = db.session() # Returns a user object (see the schemas) def create_subvoat_object(self, **kwargs): return SubVoat(**kwargs) def create_thread_object(self, **kwargs): return Thread(**kwargs) def create_comment_object(self, **kwargs): return Comment(**kwargs) # Returns a user object def get_subvoat(self, subvoat_name): return self.session.query(SubVoat).filter( SubVoat.name == subvoat_name).first() #return self.db.session.query(self.classes.subvoat).filter(self.classes.subvoat.name == subvoat_name).first() def get_comments(self, thread_uuid): return self.session.query(Thread).filter( Thread.uuid == thread_uuid).first().comments #return self.db.session.query(self.classes.thread).filter(self.classes.thread.uuid == thread_uuid).first().comment_collection def get_all_subvoats(self): return self.session.query(SubVoat).all() #return self.db.session.query(self.classes.subvoat).all() def add_subvoat(self, new_subvoat): self.session.add(new_subvoat) result = transaction.commit() return result def add_comment(self, thread_uuid, body, user_obj, reply_uuid=None): thread = self.get_thread_by_uuid(thread_uuid) if not thread: return [False, 'no such thread'] if reply_uuid: new_comment = self.create_comment_object( body=body, user_id=user_obj.id, uuid=str(uuid.uuid4()), creation_date=datetime.datetime.utcnow(), reply_uuid=reply_uuid) else: new_comment = self.create_comment_object( body=body, user_id=user_obj.id, uuid=str(uuid.uuid4()), creation_date=datetime.datetime.utcnow()) thread.comments.append(new_comment) if not transaction.commit(): return [True, 'added'] return [False, 'unable to add comment'] # Returns [result, message] def add_thread(self, subvoat_name, title, body, username): schema = Schema({ Required('subvoat_name'): All(str, Length(min=self.config['min_length_subvoat_name'])), Required('title'): All(str, Length(min=self.config['min_length_thread_title'])), Required('body'): All(str, Length(min=self.config['min_length_thread_body'])), }) try: schema({ 'subvoat_name': subvoat_name, 'title': title, 'body': body }) except MultipleInvalid as e: return [False, '%s %s' % (e.msg, e.path)] subvoat = self.get_subvoat(subvoat_name) if not subvoat: return [False, 'subvoat does not exist'] # We need to use the user.id status, result = self.user_utils.get_user(username) if not status: return [False, result] # Should this even be here? elif not result: return [False, 'user does not exist'] now = datetime.datetime.utcnow() new_thread = self.create_thread_object(uuid=str(uuid.uuid4()), title=title, body=body, user_id=result.id, creation_date=now) subvoat.threads.append(new_thread) transaction.commit() # JUST TESTING, FIX THIS (MAKE IT ASYNC, OTHERWISE IT WILL BLOCK) send_thread.delay() return [True, 'thread added'] # Make one that orders by date, with a limit def get_all_threads(self, subvoat_name): threads = [] subvoat = self.session.query(SubVoat).filter( SubVoat.name == subvoat_name).first() #subvoat = self.db.session.query(self.classes.subvoat).filter(self.classes.subvoat.name == subvoat_name).first() # probably want to limit this #if subvoat: # for thread in subvoat.thread_collection: # Need to convert the user_id to username #u_result, u_obj = self.user_utils.get_user_by_id(thread.user_id) #f u_result == False: # LOG ERROR HERE # error message should be in u_obj # continue return subvoat.threads def get_thread_by_uuid(self, uuid): thread = self.session.query(Thread).filter(Thread.uuid == uuid).first() #thread = self.db.session.query(self.classes.thread).filter(self.classes.thread.uuid == uuid).first() return thread def get_comment_by_uuid(self, uuid): comment = self.session.query(Comment).filter( Comment.uuid == uuid).first() #comment = self.db.session.query(self.classes.comment).filter(self.classes.comment.uuid == uuid).first() return comment def vote_thread(self, thread_uuid, direction, user_id): schema = Schema({ Required('direction'): All(int, Range(min=-1, max=1)), Required('thread_uuid'): All(str, Length(min=36, max=36)) }) try: schema({'direction': direction, 'thread_uuid': thread_uuid}) except MultipleInvalid as e: return [False, '%s %s' % (e.msg, e.path)] thread = self.get_thread_by_uuid(thread_uuid) if not thread: return [False, 'no such thread'] # see if the user already voted, if so change the vote direction if its different sq = self.session.query(Thread).filter( Thread.uuid == thread_uuid).subquery() #sq = self.db.session.query(self.classes.thread).filter(self.classes.thread.uuid == thread_uuid).subquery() q = self.session.query( ThreadVote, sq).filter(ThreadVote.user_id == user_id).first() #q = self.db.session.query(self.classes.thread_vote, sq).filter(self.classes.thread_vote.user_id == user_id).first() # if the vote doesn't exist, create it and commit it if not q: new_vote = ThreadVote(user_id=user_id, direction=direction) thread.votes.append(new_vote) self.db.session.add(thread) if not transaction.commit(): return [True, 'vote added'] return [False, 'unable to commit vote'] # If the vote is the same if q.ThreadVote.direction == int(direction): return [True, 'vote unchanged'] # Otherwise update the vote direction else: q.ThreadVote.direction = int(direction) self.db.session.add(q.vote) if not transaction.commit(): return [True, 'vote changed'] return [False, 'unable to commit vote change'] def vote_comment(self, comment_uuid, direction, user_id): schema = Schema({ Required('direction'): All(int, Range(min=-1, max=1)), Required('comment_uuid'): All(str, Length(min=36, max=36)) }) try: schema({'direction': direction, 'comment_uuid': comment_uuid}) except MultipleInvalid as e: return [False, '%s %s' % (e.msg, e.path)] # FIX: calling comment 2x (sq = ) comment = self.get_comment_by_uuid(comment_uuid) if not comment: return [False, 'no such comment'] # get the comments sq = self.session.query(Comment).filter( Comment.uuid == comment_uuid).subquery() q = self.session.query( CommentVote, sq).filter(CommentVote.user_id == user_id).first() #sq = self.db.session.query(self.classes.comment).filter(self.classes.comment.uuid == comment_uuid).subquery() #q = self.db.session.query(self.classes.comment_vote, sq).filter(self.classes.comment_vote.user_id == user_id).first() if not q: new_vote = CommentVote(user_id=user_id, direction=direction) comment.votes.append(new_vote) self.db.session.add(comment) if not transaction.commit(): return [True, 'vote added'] return [False, 'unable to commit vote'] if q.CommentVote.direction == int(direction): return [True, 'vote unchanged'] else: q.CommentVote.direction = int(direction) self.db.session.add(q.vote) if not transaction.commit(): return [True, 'vote changed'] return [False, 'unable to commit vote change']