def getSourcesIds(): sourcesService = entityFor(ISourceService) assert isinstance(sourcesService, ISourceService) sources = {} for name in SOURCES: srcs = sourcesService.getAll(q=QSource(name=name)) if srcs: sources[name] = next(iter(srcs)).Id else: src = Source() src.Name = name src.IsModifiable, src.URI, src.Type, src.Key = SOURCES[name] createSourceType(src.Type) sources[name] = sourcesService.insert(src) return sources
def insertSource(name): sourcesService = entityFor(ISourceService) assert isinstance(sourcesService, ISourceService) srcs = sourcesService.getAll(q=QSource(name=name)) if srcs: src = next(iter(srcs)).Id else: src = Source() src.Name = name src.IsModifiable, src.URI, src.Type, src.Key = False, '', '', '' src = sourcesService.insert(src) collaboratorService = entityFor(ICollaboratorService) assert isinstance(collaboratorService, ICollaboratorService) colls = collaboratorService.getAll(qs=QSource(name=name)) if not colls: coll = Collaborator() coll.User = None coll.Source = src collaboratorService.insert(coll)
def addComment(self, blogId, comment): ''' @see: IBlogCommentService.addComment ''' # checking if the blog exists # checking whether comments are allowed shall be done in gateway if not self.session().query(exists().where(BlogMapped.Id == blogId)).scalar(): raise InputError(Ref(_('Specified blog does not exist'),)) userName = comment.UserName commentText = comment.CommentText commentSource = comment.CommentSource if comment.CommentSource else self.source_name_default # checking the necessary info: user name and comment text if not userName: raise InputError(Ref(_('No value for the mandatory UserName'),)) if not commentText: raise InputError(Ref(_('No value for the mandatory CommentText'),)) # take (or make) the user (for user name) part of creator and collaborator userTypeId, = self.session().query(UserTypeMapped.id).filter(UserTypeMapped.Key == self.user_type_key).one() try: sql = self.session().query(UserMapped.userId, UserMapped.Active) sql = sql.filter(UserMapped.typeId == userTypeId) sql = sql.filter(UserMapped.FirstName == userName) userId, isActive = sql.one() if not isActive: raise InputError(Ref(_('The commentator user was inactivated'),)) except: user = User() user.FirstName = userName user.LastName = self.user_last_name user.Name = self._freeCommentUserName() user.Password = binascii.b2a_hex(os.urandom(32)).decode() user.Type = self.user_type_key userId = self.userService.insert(user) # make the source (for inlet type) part of collaborator try: sql = self.session().query(SourceMapped.Id).join(SourceTypeMapped) sql = sql.filter(SourceTypeMapped.Key == self.source_type_key).filter(SourceMapped.Name == commentSource) sourceId, = sql.one() except NoResultFound: source = Source() source.Type = self.source_type_key source.Name = commentSource source.URI = '' source.IsModifiable = True sourceId = self.sourceService.insert(source) # make the collaborator sql = self.session().query(CollaboratorMapped.Id) sql = sql.filter(CollaboratorMapped.Source == sourceId) sql = sql.filter(CollaboratorMapped.User == userId) try: collabId, = sql.one() except NoResultFound: collab = Collaborator() collab.Source = sourceId collab.User = userId collabId = self.collaboratorService.insert(collab) # create post request post = Post() post.Type = self.post_type_key post.Creator = userId post.Author = collabId post.Content = commentText post.CreatedOn = datetime.now() # insert the blog post postId = self.blogPostService.insert(blogId, post) return postId
def pushMessage(self, typeKey, phoneNumber=None, messageText=None, timeStamp=None): ''' @see: IInletService.pushMessage ''' # checking the necessary info: phone number and message text if (phoneNumber is None) or (phoneNumber == ''): raise InputError(Ref(_('No value for the mandatory phoneNumber parameter'),)) if (messageText is None) or (messageText == ''): raise InputError(Ref(_('No value for the mandatory messageText parameter'),)) # take (or make) the user (for phone number) part of creator and collaborator try: userId, = self.session().query(PersonMapped.Id).filter(PersonMapped.PhoneNumber == phoneNumber).one() except: user = User() user.PhoneNumber = phoneNumber user.Name = self._freeSMSUserName() user.Password = binascii.b2a_hex(os.urandom(32)).decode() user.Type = self.user_type_key userId = self.userService.insert(user) # make the source (for inlet type) part of collaborator try: sql = self.session().query(SourceMapped.Id).join(SourceTypeMapped) sql = sql.filter(SourceTypeMapped.Key == self.sms_source_type_key).filter(SourceMapped.Name == typeKey) sourceId, = sql.one() except NoResultFound: source = Source() source.Type = self.sms_source_type_key source.Name = typeKey source.URI = '' source.IsModifiable = True sourceId = self.sourceService.insert(source) # make the collaborator sql = self.session().query(CollaboratorMapped.Id) sql = sql.filter(CollaboratorMapped.Source == sourceId) sql = sql.filter(CollaboratorMapped.User == userId) try: collabId, = sql.one() except NoResultFound: collab = Collaborator() collab.Source = sourceId collab.User = userId collabId = self.collaboratorService.insert(collab) # take / make time stamp if timeStamp: try: timeStamp = datetime.strptime(timeStamp, '%Y-%m-%d %H:%M:%S.%f') except: timeStamp = None if not timeStamp: timeStamp = datetime.now() # create post request post = Post() post.Type = self.sms_post_type_key post.Creator = userId post.Author = collabId post.Content = messageText post.CreatedOn = timeStamp # insert the post postId = self.postService.insert(post) return (self.postService.getById(postId),)
def pushMessage(self, typeKey, phoneNumber=None, messageText=None, timeStamp=None): ''' @see: IInletService.pushMessage ''' # checking the necessary info: phone number and message text if (phoneNumber is None) or (phoneNumber == ''): raise InputError( Ref(_('No value for the mandatory phoneNumber parameter'), )) if (messageText is None) or (messageText == ''): raise InputError( Ref(_('No value for the mandatory messageText parameter'), )) # take (or make) the user (for phone number) part of creator and collaborator try: userId, = self.session().query(PersonMapped.Id).filter( PersonMapped.PhoneNumber == phoneNumber).one() except: user = User() user.PhoneNumber = phoneNumber user.Name = self._freeSMSUserName() user.Password = binascii.b2a_hex(os.urandom(32)).decode() user.Type = self.user_type_key userId = self.userService.insert(user) # make the source (for inlet type) part of collaborator try: sql = self.session().query(SourceMapped.Id).join(SourceTypeMapped) sql = sql.filter( SourceTypeMapped.Key == self.sms_source_type_key).filter( SourceMapped.Name == typeKey) sourceId, = sql.one() except NoResultFound: source = Source() source.Type = self.sms_source_type_key source.Name = typeKey source.URI = '' source.IsModifiable = True sourceId = self.sourceService.insert(source) # make the collaborator sql = self.session().query(CollaboratorMapped.Id) sql = sql.filter(CollaboratorMapped.Source == sourceId) sql = sql.filter(CollaboratorMapped.User == userId) try: collabId, = sql.one() except NoResultFound: collab = Collaborator() collab.Source = sourceId collab.User = userId collabId = self.collaboratorService.insert(collab) # take / make time stamp if timeStamp: try: timeStamp = datetime.strptime(timeStamp, '%Y-%m-%d %H:%M:%S.%f') except: timeStamp = None if not timeStamp: timeStamp = datetime.now() # create post request post = Post() post.Type = self.sms_post_type_key post.Creator = userId post.Author = collabId post.Content = messageText post.CreatedOn = timeStamp # insert the post postId = self.postService.insert(post) return (self.postService.getById(postId), )
def addComment(self, blogId, comment): ''' @see: IBlogCommentService.addComment ''' # checking if the blog exists # checking whether comments are allowed shall be done in gateway if not self.session().query( exists().where(BlogMapped.Id == blogId)).scalar(): raise InputError(Ref(_('Specified blog does not exist'), )) userName = comment.UserName commentText = comment.CommentText commentSource = comment.CommentSource if comment.CommentSource else self.source_name_default # checking the necessary info: user name and comment text if not userName: raise InputError(Ref(_('No value for the mandatory UserName'), )) if not commentText: raise InputError(Ref( _('No value for the mandatory CommentText'), )) # take (or make) the user (for user name) part of creator and collaborator userTypeId, = self.session().query(UserTypeMapped.id).filter( UserTypeMapped.Key == self.user_type_key).one() try: sql = self.session().query(UserMapped.userId, UserMapped.Active) sql = sql.filter(UserMapped.typeId == userTypeId) sql = sql.filter(UserMapped.FirstName == userName) userId, isActive = sql.one() if not isActive: raise InputError( Ref(_('The commentator user was inactivated'), )) except: user = User() user.FirstName = userName user.LastName = self.user_last_name user.Name = self._freeCommentUserName() user.Password = binascii.b2a_hex(os.urandom(32)).decode() user.Type = self.user_type_key userId = self.userService.insert(user) # make the source (for inlet type) part of collaborator try: sql = self.session().query(SourceMapped.Id).join(SourceTypeMapped) sql = sql.filter( SourceTypeMapped.Key == self.source_type_key).filter( SourceMapped.Name == commentSource) sourceId, = sql.one() except NoResultFound: source = Source() source.Type = self.source_type_key source.Name = commentSource source.URI = '' source.IsModifiable = True sourceId = self.sourceService.insert(source) # make the collaborator sql = self.session().query(CollaboratorMapped.Id) sql = sql.filter(CollaboratorMapped.Source == sourceId) sql = sql.filter(CollaboratorMapped.User == userId) try: collabId, = sql.one() except NoResultFound: collab = Collaborator() collab.Source = sourceId collab.User = userId collabId = self.collaboratorService.insert(collab) # create post request post = Post() post.Type = self.post_type_key post.Creator = userId post.Author = collabId post.Content = commentText post.CreatedOn = datetime.now() # insert the blog post postId = self.blogPostService.insert(blogId, post) return postId