def addBlog(self, *args, **kwargs): uModel = UserModel() # check authentication and get data of current user profileDict = uModel.getUserByToken(**kwargs) blogItem = BlogItem( title=kwargs['title'], text=kwargs['text'], userId=profileDict['id'], public=kwargs['public'], ) blogItem.validate() sql = '''insert into "{0}" ("title", "text", "user_id", "public", "date") values (%s, %s, %s, %s, %s) returning "id" ;'''.format(self.TABLE) cursor = self.connection.cursor() cursor.execute(sql, [ blogItem.title, blogItem.text, blogItem.userId, blogItem.public, blogItem.date ]) ids = cursor.fetchone()[0] self.connection.commit() blogItem.id = ids return blogItem.__dict__
def resolve_friendRemove (self, info, *args, **kwargs): __u = UserModel() __res = __u.friendRemove(**kwargs) return FriendRequest( success = __res['success'], friendId = __res['friendId'], userId = __res['userId'] )
def resolve_registration (self, info, email, password, confirmPassword, device): _u = UserModel() _reg_user = _u.registration( email = email, password = password, confirmPassword=confirmPassword, device = device ) return RegGraph(**_reg_user)
def resolve_getFriendList (self, info, *args, **kwargs): __u = UserModel() __fList = __u.getFriendList(**kwargs) __firends = [] for v in __fList['friends']: __firends.append(UserDetailGraph(**v)) return FriendListGraph( count = __fList['count'], friends = __firends )
def resolve_getUserList (self, info, *args, **kwargs): __u = UserModel() __user = __u.getUserList(**kwargs) __users = [] for v in __user['users']: __users.append(UserDetailGraph(**v)) return UserListGraph( count = __user['count'], users = __users )
def editPost (self, *args, **kwargs): uModel = UserModel() # check authentication and get data of current user profileDict = uModel.getUserByToken(**kwargs) postItem = PostItem( id = kwargs.get('id', None), blogId = kwargs.get('blogId', None), userId = profileDict.get('id', None), title = kwargs.get('title', None), description = kwargs.get('descript', None), text = kwargs.get('text', None), public = kwargs.get('public', None), ) postItem.validate() if postItem.id == -1: return self.addPost(**postItem.__dict__) postRows = ('id', 'title', 'description', 'text', 'public', 'date', self.TABLE) postSql = '''select "{0}", "{1}", "{2}", "{3}", "{4}", "{5}" from "{6}" where id = %s and blog_id = %s and user_id = %s'''.format(*postRows) cursor = self.connection.cursor() cursor.execute(postSql, ( postItem.id, postItem.blogId, postItem.userId )) postData = cursor.fetchone() if (postData is None): raise Exception('Blog not found') selectDict = self.list_to_dict(postRows)(postData) if ( postItem.title == selectDict['title'] and postItem.description == selectDict['description'] and postItem.text == selectDict['text'] and postItem.public == selectDict['public'] ): raise Exception('Nothing to update') updateSql = '''update "{0}" set "title" = %s, "description" = %s, "text" = %s, "public" = %s, "date" = %s where id = %s ;'''.format(self.TABLE) cursor.execute(updateSql, [postItem.title, postItem.description, postItem.text, postItem.public, postItem.date, postItem.id]) self.connection.commit() return postItem.__dict__
def getMyPost(self, *args, **kwargs): if (not 'blogId' in kwargs): raise Exception('Blog not found') if (not 'id' in kwargs): raise Exception('Post not found') uModel = UserModel() # check authentication and get data of current user profileDict = uModel.getUserByToken(**kwargs) postSql = '''select blog.id as blogId, blog.user_id as userId, post.id as id, post.title as title, post.description as description, post.text as "text", post.public as "public", post.date as "date" from blog left join post on blog.id = post.blog_id where blog.id = %s and blog.user_id = %s and post.id = %s and post.user_id = %s ;'''.format(BlogModel.TABLE, self.TABLE) cursor = self.connection.cursor() cursor.execute(postSql, [ kwargs["blogId"], profileDict["id"], kwargs["id"], profileDict["id"] ]) postData = cursor.fetchone() if (postData is None): raise Exception('Blog not found') postRows = [ "blogId", "userId", "id", "title", "description", "text", "public", "date" ] postDict = self.list_to_dict(postRows)(postData) if ( postDict is None or postDict["blogId"] is None or postDict["id"] is None ): raise Exception('Post not found') return postDict
def editBlog(self, *args, **kwargs): uModel = UserModel() # check authentication and get data of current user profileDict = uModel.getUserByToken(**kwargs) blogItem = BlogItem( id=kwargs['id'], title=kwargs['title'], text=kwargs['text'], userId=profileDict['id'], public=kwargs['public'], ) blogItem.validate() if blogItem.id == -1: return self.addBlog(**kwargs) selectRows = ['id', 'user_id', 'text', 'title', 'public', self.TABLE] selectSql = 'select "{0}", "{1}", "{2}", "{3}", "{4}" from "{5}" where id = %s'.format( *selectRows) cursor = self.connection.cursor() cursor.execute(selectSql, [blogItem.id]) selectItem = cursor.fetchone() selectDict = self.list_to_dict(selectRows)(selectItem) if (selectDict is None or selectDict["user_id"] != profileDict["id"]): raise Exception('Blog not found') if (blogItem.title == selectDict['title'] and blogItem.text == selectDict['text'] and blogItem.public == selectDict['public']): raise Exception('Nothing to update') updateSql = '''update "{0}" set "title" = %s, "text" = %s, "public" = %s, "date" = %s where id = %s ;'''.format(self.TABLE) cursor.execute(updateSql, [ blogItem.title, blogItem.text, blogItem.public, blogItem.date, blogItem.id ]) self.connection.commit() return blogItem.__dict__
def getMyBlogList(self, *args, **kwargs): start = int(kwargs['start']) if ('start' in kwargs) else 0 perpage = int(kwargs['perpage']) if ('perpage' in kwargs) else 20 uModel = UserModel() # check authentication and get data of current user profileDict = uModel.getUserByToken(**kwargs) blogParam = [profileDict.get('id'), perpage, start] countParam = [profileDict.get('id')] blogRows = [ 'id', 'user_id', 'title', 'text', 'date', 'public', 'login', 'email', self.TABLE, UserModel.TABLE ] blogSql = '''select bTable.{0}, bTable.{1}, bTable.{2}, bTable.{3}, bTable.{4}, bTable.{5}, uTable.{6}, uTable.{7} from "{8}" as bTable left join "{9}" as uTable on bTable.user_id = uTable.id where bTable.{1} = %s order by bTable.{0} desc limit %s offset %s; '''.format(*blogRows) countSql = 'select count(id) from {0} where user_id = %s;'.format( self.TABLE) blogKeys = [ 'id', 'userId', 'title', 'text', 'date', 'public', 'userName', 'userEmail' ] cursor = self.connection.cursor() cursor.execute(blogSql, blogParam) blogData = cursor.fetchall() cursor.execute(countSql, countParam) countData = cursor.fetchone() listResult = {'count': 0, 'blogs': []} if (countData != None): listResult['count'] = countData[0] if (blogData != None): listResult['blogs'] = map(self.list_to_dict(blogKeys), blogData) return listResult
def getBlog(self, *args, **kwargs): if (not 'id' in kwargs): raise Exception('Blog not found') uModel = UserModel() # check authentication and get data of current user profileDict = uModel.getUserByToken(**kwargs) cursor = self.connection.cursor() blogRows = [ 'id', 'user_id', 'title', 'text', 'date', 'public', 'login', 'email', self.TABLE, UserModel.TABLE ] blogSql = '''select bTable.{0}, bTable.{1}, bTable.{2}, bTable.{3}, bTable.{4}, bTable.{5}, uTable.{6}, uTable.{7} from "{8}" as bTable left join "{9}" as uTable on bTable.user_id = uTable.id where bTable.{0} = %s ;'''.format(*blogRows) cursor.execute(blogSql, [kwargs["id"]]) blogData = cursor.fetchone() if (blogData is None): raise Exception('Blog not found') blogDict = self.list_to_dict(blogRows)(blogData) if (profileDict["id"] != blogDict["user_id"] and not blogDict["public"]): raise Exception('Blog not found') return dict( id=blogDict["id"], userId=blogDict["user_id"], userName=blogDict["login"], userEmail=blogDict["email"], title=blogDict["title"], text=blogDict["text"], date=blogDict["date"], public=blogDict["public"], )
def publicMyBlog(self, *args, **kwargs): id = kwargs.get('id') public = kwargs.get('public') if (id is None or math.isnan(int(id))): raise Exception('Blog not found') uModel = UserModel() # check authentication and get data of current user profileDict = uModel.getUserByToken(**kwargs) selectRows = ['id', 'user_id', 'public', self.TABLE] selectSql = 'select "{0}", "{1}", "{2}" from "{3}" where id = %s and user_id = %s;'.format( *selectRows) cursor = self.connection.cursor() cursor.execute(selectSql, [id, profileDict.get('id')]) selectItem = cursor.fetchone() selectDict = self.list_to_dict(selectRows)(selectItem) if (selectDict is None): raise Exception('Blog not found') if (selectDict.get("public") == public): return {'id': id, 'public': public} updateSql = '''update "{0}" set "public" = %s where id = %s ;'''.format(self.TABLE) cursor.execute(updateSql, [public, id]) self.connection.commit() return {'id': id, 'public': public}
def resolve_editProfile (self, info, *args, **kwargs): _u = UserModel() _userProfile = _u.editUserProfile(**kwargs) return EditProfileGraph(**_userProfile)
def resolve_profile (self, info, token, device): blog = BlogModel() _u = UserModel() _userProfile = _u.getUserProfile(token = token, device = device) return ProfileGraph(**_userProfile)
def resolve_auth (self, info, login, password, device): _u = UserModel() _auth_user = _u.authenticate(login = login, password = password, device = device) return AuthGraph(**_auth_user)
def resolve_logout (self, info, *args, **kwargs): __u = UserModel() __log = __u.logout(**kwargs) return LogoutGraph(**__log)
def getMyBlogDetail(self, *args, **kwargs): if (not 'blogId' in kwargs): raise Exception('Blog not found') start = int(kwargs['start']) if ('start' in kwargs) else 0 perpage = int(kwargs['perpage']) if ('perpage' in kwargs) else 20 listResult = { 'count': 0, 'blog': None, 'posts': None } uModel = UserModel() # check authentication and get data of current user profileDict = uModel.getUserByToken(**kwargs) blogRows = ('id', 'title', 'text', 'public', 'date', 'userId', 'userName') blogSql = '''select blog.id as "{0}", blog.title as "{1}", blog.text as "{2}", blog.public as "{3}", blog.date as "{4}", uTable.id as "{5}", uTable.login as "{6}" from blog left join "user" as uTable on blog.user_id = uTable.id where blog.id = %s and blog.user_id = %s ;'''.format(*blogRows) cursor = self.connection.cursor() cursor.execute(blogSql, (kwargs['blogId'], profileDict['id'])) blogData = cursor.fetchone() if (blogData is None): return listResult listResult['blog'] = self.list_to_dict(blogRows)(blogData) postRows = ( 'id', 'blogId', 'title', 'description', 'public', 'date', 'userId', 'userName' ) postSql = '''select post.id as {0}, post.blog_id as {1}, post.title as {2}, post.description as {3}, post.public as {4}, post.date as {5}, uTable.id as {6}, uTable.login as {7} from post left join "user" as uTable on post.user_id = uTable.id where post.blog_id = %s and post.user_id = %s order by post.{0} desc limit %s offset %s; ;'''.format(*postRows) cursor.execute(postSql, ( kwargs['blogId'], profileDict['id'], perpage, start )) postData = cursor.fetchall() countSql = 'select count(id) from post where blog_id = %s and user_id = %s;' cursor.execute(countSql, ( kwargs['blogId'], profileDict['id'] )) countData = cursor.fetchone() if (countData != None): listResult['count'] = countData[0] if (postData != None): listResult['posts'] = map(self.list_to_dict(postRows), postData) return listResult