Пример #1
0
    def insert(self, content):
        '''
        @see: IMetaDataService.insert
        '''
        assert isinstance(content, Content), 'Invalid content %s' % content
        if not content.getName(): raise InputError(_('No name specified for content'))

        metaData = MetaDataMapped()
        metaData.CreatedOn = datetime.now()
        metaData.Name = content.getName()
        metaData.Type = self._metaType.Key
        metaData.typeId = self._metaType.id
        metaData.thumbnailId = self._thumbnail.id
        try:
            self.session().add(metaData)
            self.session().flush((metaData,))

            path = abspath(join(self.processing_dir_path, '.'.join((str(metaData.Id), metaData.Name))))
            with open(path, 'w+b') as fobj: pipe(content, fobj)
            metaData.SizeInBytes = getsize(path)

            self.session().flush((metaData,))

            with open(path, 'rb') as fobj: self.cdmArchive.publishFromFile(self._reference(metaData), fobj)

            for handler in self.metaDataHandlers:
                assert isinstance(handler, IMetaDataHandler), 'Invalid handler %s' % handler
                if handler.process(metaData.Id, path): break
            else:
                remove(path)

        except SQLAlchemyError as e: handle(e, metaData)

        return metaData.Id
Пример #2
0
    def insert(self, source):
        '''
        @see: ISourceService.insert
        '''
        assert isinstance(source, Source), 'Invalid source %s' % source

        if source.URI:
            list = source.URI.split('//')
            source.URI = '//' + list[-1]

        if source.OriginURI:
            list = source.OriginURI.split('//')
            source.OriginURI = '//' + list[-1]

        sourceDb = SourceMapped()
        sourceDb.typeId = self._typeId(source.Type)
        copy(source, sourceDb, exclude=('Type', ))

        try:
            self.session().add(sourceDb)
            self.session().flush((sourceDb, ))
        except SQLAlchemyError as e:
            handle(e, sourceDb)
        source.Id = sourceDb.Id
        return sourceDb.Id
Пример #3
0
    def insert(self, item):
        '''
        @see: IItemService.insert
        '''
        assert self.modelType.isValid(
            item), 'Invalid item %s, expected %s' % (item, ItemMapped)
        assert isinstance(item, Item)
        assert item.ItemClass == CLASS_TEXT or item.ItemClass == CLASS_PACKAGE, \
            'Invalid item class %s, expected %s or %s' % (item.ItemClass, CLASS_TEXT, CLASS_PACKAGE)
        itemDb = copy(item, ItemMapped())
        assert isinstance(itemDb, ItemMapped)

        # TODO: generate proper guid: generate better itemId, externalise as a service
        itemDb.GUId = self.guid_scheme % {
            'prefix': self.guid_prefix,
            'provider': self.guid_provider,
            'dateId': date.today().strftime('%Y%m%d'),
            'itemId': itemDb.SlugLine
        }
        itemDb.FirstCreated = current_timestamp()
        try:
            self.session().add(itemDb)
            self.session().flush((itemDb, ))
        except SQLAlchemyError as e:
            handle(e, itemDb)
        item.GUId = itemDb.GUId
        return itemDb.GUId
Пример #4
0
    def exchange(self, firstId, secondId):
        '''
        @see: IBlogMediaService.exchange
        '''
        firstMedia = self.session().query(BlogMediaMapped).get(firstId)
        if not firstMedia: raise InputError(Ref(_('Unknown blog media id'), ref=BlogMedia.Id))
        assert isinstance(firstMedia, BlogMediaMapped), 'Invalid blog media %s' % firstMedia

        secondMedia = self.session().query(BlogMediaMapped).get(secondId)
        if not secondMedia: raise InputError(Ref(_('Unknown blog media id'), ref=BlogMedia.Id))
        assert isinstance(secondMedia, BlogMediaMapped), 'Invalid blog media %s' % secondMedia

        if firstMedia.Blog != secondMedia.Blog:
            raise InputError(Ref(_('Blog media have to be of the same blog'),))

        if firstMedia.Type != secondMedia.Type:
            raise InputError(Ref(_('Blog media have to be of the same type'),))

        firstRank, secondRank = secondMedia.Rank, firstMedia.Rank

        try:
            firstMedia.Rank = 0
            self.session().flush((firstMedia,))

            firstMedia.Rank, secondMedia.Rank = firstRank, secondRank
            self.session().flush((secondMedia,))
            self.session().flush((firstMedia,))
        except SQLAlchemyError as e: handle(e, mediaDb)
Пример #5
0
 def addMetaInfo(self, metaDataMapped, languageId):
     audioInfoMapped = AudioInfoMapped()
     audioInfoMapped.MetaData = metaDataMapped.Id
     audioInfoMapped.Language = languageId
     try:
         self.session().add(audioInfoMapped)
         self.session().flush((audioInfoMapped, ))
     except SQLAlchemyError as e:
         handle(e, audioInfoMapped)
     return audioInfoMapped
Пример #6
0
 def addMetaInfo(self, metaDataMapped, languageId):
     audioInfoMapped = AudioInfoMapped()
     audioInfoMapped.MetaData = metaDataMapped.Id
     audioInfoMapped.Language = languageId
     try:
         self.session().add(audioInfoMapped)
         self.session().flush((audioInfoMapped,))
     except SQLAlchemyError as e:
         handle(e, audioInfoMapped)
     return audioInfoMapped
Пример #7
0
 def addMetaInfo(self, metaDataMapped, languageId):
     imageInfoMapped = ImageInfoMapped()
     imageInfoMapped.MetaData = metaDataMapped.Id
     imageInfoMapped.Language = languageId
     try:
         self.session().add(imageInfoMapped)
         self.session().flush((imageInfoMapped, ))
     except SQLAlchemyError as e:
         handle(e, imageInfoMapped)
     return imageInfoMapped
Пример #8
0
 def update(self, entity):
     '''
     @see: IEntityCRUDService.update
     '''
     assert self.modelType.isValid(entity), 'Invalid entity %s, expected %s' % (entity, self.Entity)
     assert isinstance(entity.Id, int), 'Invalid entity %s, with id %s' % (entity, entity.Id)
     entityDb = self.session().query(self.Entity).get(entity.Id)
     if not entityDb: raise InputError(Ref(_('Unknown id'), ref=self.Entity.Id))
     try: self.session().flush((copy(entity, entityDb),))
     except SQLAlchemyError as e: handle(e, self.Entity)
Пример #9
0
 def addMetaInfo(self, metaDataMapped, languageId):
     videoInfoMapped = VideoInfoMapped()
     videoInfoMapped.MetaData = metaDataMapped.Id
     videoInfoMapped.Language = languageId
     try:
         self.session().add(videoInfoMapped)
         self.session().flush((videoInfoMapped,))
     except SQLAlchemyError as e:
         handle(e, videoInfoMapped)
     return videoInfoMapped
Пример #10
0
 def addMetaInfo(self, metaDataMapped, languageId):
     videoInfoMapped = VideoInfoMapped()
     videoInfoMapped.MetaData = metaDataMapped.Id
     videoInfoMapped.Language = languageId
     try:
         self.session().add(videoInfoMapped)
         self.session().flush((videoInfoMapped, ))
     except SQLAlchemyError as e:
         handle(e, videoInfoMapped)
     return videoInfoMapped
Пример #11
0
 def addMetaInfo(self, metaDataMapped, languageId):
     imageInfoMapped = ImageInfoMapped()
     imageInfoMapped.MetaData = metaDataMapped.Id
     imageInfoMapped.Language = languageId
     try:
         self.session().add(imageInfoMapped)
         self.session().flush((imageInfoMapped,))
     except SQLAlchemyError as e:
         handle(e, imageInfoMapped)
     return imageInfoMapped
Пример #12
0
    def process(self, metaDataMapped, contentPath):
        '''
        @see: IMetaDataHandler.process
        '''
        assert isinstance(metaDataMapped, MetaDataMapped), 'Invalid meta data mapped %s' % metaDataMapped

        p = Popen([self.metadata_extractor_path, contentPath], stdin=PIPE, stdout=PIPE, stderr=STDOUT)
        result = p.wait()
        # 253 is the exiv2 code for error: No Exif data found in the file
        if result != 0 and result != 253: return False

        imageDataEntry = ImageDataEntry()
        imageDataEntry.Id = metaDataMapped.Id

        while True:
            line = p.stdout.readline()
            if not line: break
            line = str(line, "utf-8")

            property = self.extractProperty(line)

            if property is None:
                continue
            try:
                if property == 'Image size':
                    size = self.extractSize(line)
                    imageDataEntry.Width = size[0]
                    imageDataEntry.Height = size[1]
                elif property == 'Image timestamp':
                    imageDataEntry.CreationDate = self.extractDateTime(line)
                elif property == 'Camera make':
                    imageDataEntry.CameraMake = self.extractString(line)
                elif property == 'Camera model':
                    imageDataEntry.CameraModel = self.extractString(line)
            except: 
                #skip if not able to extract data
                pass        

        path = self.format_file_name % {'id': metaDataMapped.Id, 'file': metaDataMapped.Name}
        path = ''.join((META_TYPE_KEY, '/', self.generateIdPath(metaDataMapped.Id), '/', path))

        metaDataMapped.content = path
        metaDataMapped.typeId = self.metaTypeId()
        metaDataMapped.Type = META_TYPE_KEY
        metaDataMapped.thumbnailFormatId = self.thumbnailFormatId()
        metaDataMapped.IsAvailable = True

        self.thumbnailManager.putThumbnail(self.thumbnailFormatId(), contentPath, metaDataMapped)

        try: self.session().add(imageDataEntry)
        except SQLAlchemyError as e:
            metaDataMapped.IsAvailable = False
            handle(e, ImageDataEntry)

        return True
Пример #13
0
    def update(self, user):
        '''
        @see: IUserService.update
        '''
        assert isinstance(user, User), 'Invalid user %s' % user

        userDb = self.session().query(UserMapped).get(user.Id)
        if not userDb: raise InputError(Ref(_('Unknown id'), ref=UserMapped.Id))
        try:
            self.session().flush((copy(user, userDb),))
        except SQLAlchemyError as e: handle(e, userDb)
Пример #14
0
    def update(self, item):
        '''
        @see: IItemService.update
        '''
        assert self.modelType.isValid(item), 'Invalid item %s, expected %s' % (item, ItemMapped)
        assert isinstance(item.GUId, str), 'Invalid item %s, with id %s' % (item, item.GUId)

        itemDb = self.session().query(ItemMapped).get(item.GUId)
        if not itemDb: raise InputError(Ref(_('Unknown id'), ref=ItemMapped.GUId))
        try: self.session().flush((copy(item, itemDb),))
        except SQLAlchemyError as e: handle(e, ItemMapped)
Пример #15
0
 def insert(self, entity):
     '''
     @see: IEntityCRUDService.insert
     '''
     assert self.modelType.isValid(entity), 'Invalid entity %s, expected %s' % (entity, self.Entity)
     entityDb = copy(entity, self.Entity())
     try:
         self.session().add(entityDb)
         self.session().flush((entityDb,))
     except SQLAlchemyError as e: handle(e, entityDb)
     return entity.Key
Пример #16
0
 def setIcon(self, personId, metaDataId):
     '''
     @see: IPersonIconService.setIcon
     '''
     entityDb = PersonIconMapped()
     entityDb.Id, entityDb.MetaData = personId, metaDataId
     try:
         self.session().merge(entityDb)
         self.session().flush((entityDb,))
     except SQLAlchemyError as e: handle(e, entityDb)
     return entityDb.Id
Пример #17
0
    def update(self, entity):
        '''
        @see: IEntityCRUDService.update
        '''
        assert self.modelType.isValid(entity), 'Invalid entity %s, expected %s' % (entity, self.Entity)
        assert isinstance(entity.Key, str), 'Invalid entity %s, with key %s' % (entity, entity.Key)

        try: entityDb = self.session().query(self.Entity).filter(self.Entity.Key == entity.Key)
        except NoResultFound: raise InputError(Ref(_('Unknown key'), ref=self.Entity.Key))
        try:
            self.session().flush((copy(entity, entityDb),))
        except SQLAlchemyError as e: handle(e, self.Entity)
Пример #18
0
 def update(self, entity):
     '''
     @see: IEntityCRUDService.update
     '''
     assert self.modelType.isValid(entity), 'Invalid entity %s, expected %s' % (entity, self.Entity)
     entityDb = self.session().query(self.Entity).get(entity.Id)
     if not entityDb: raise InputError(Ref(_('Unknown id'), ref=self.Entity.Id))
     try:
         for prop in self.model.properties:
             if getattr(entity.__class__, prop) in entity: setattr(entityDb, prop, getattr(entity, prop))
         self.session().flush((entityDb,))
     except SQLAlchemyError as e: handle(e, self.Entity)
Пример #19
0
    def update(self, source):
        '''
        @see: ISourceService.update
        '''
        assert isinstance(source, Source), 'Invalid source %s' % source
        sourceDb = self.session().query(SourceMapped).get(source.Id)
        if not sourceDb: raise InputError(Ref(_('Unknown source id'), ref=Source.Id))
        if Source.Type in source: sourceDb.typeId = self._typeId(source.Type)

        try:
            self.session().flush((copy(source, sourceDb, exclude=('Type',)),))
        except SQLAlchemyError as e: handle(e, SourceMapped)
Пример #20
0
 def insert(self, entity):
     '''
     @see: IEntityCRUDService.insert
     '''
     assert self.modelType.isValid(
         entity), 'Invalid entity %s, expected %s' % (entity, self.Entity)
     entityDb = copy(entity, self.Entity())
     try:
         self.session().add(entityDb)
         self.session().flush((entityDb, ))
     except SQLAlchemyError as e:
         handle(e, entityDb)
     return entity.Key
Пример #21
0
 def changePassword(self, adminId, user):
     """
     @see: IUserService.changePassword
     """
     userDb = self.session().query(UserMapped).get(user.Id)
     if not userDb or userDb.DeletedOn is not None:
         assert isinstance(userDb, UserMapped), "Invalid user %s" % userDb
         raise InputError(Ref(_("Unknown user id"), ref=User.Id))
     try:
         userDb.password = user.Password
         self.session().flush((userDb,))
     except SQLAlchemyError as e:
         handle(e, userDb)
Пример #22
0
 def changePassword(self, adminId, user):
     '''
     @see: IUserService.changePassword
     '''
     userDb = self.session().query(UserMapped).get(user.Id)
     if not userDb or userDb.DeletedOn is not None:
         assert isinstance(userDb, UserMapped), 'Invalid user %s' % userDb
         raise InputError(Ref(_('Unknown user id'), ref=User.Id))
     try:
         userDb.password = user.Password
         self.session().flush((userDb, ))
     except SQLAlchemyError as e:
         handle(e, userDb)
Пример #23
0
    def insert(self, userId, content):
        '''
        @see: IMetaDataService.insert
        '''
        assert isinstance(content, Content), 'Invalid content %s' % content
        if not content.name:
            raise InputError(_('No name specified for content'))

        metaData = MetaDataMapped()
        metaData.CreatedOn = current_timestamp()
        metaData.Creator = userId
        metaData.Name = content.name

        metaData.typeId = self._metaType.Id
        metaData.thumbnailFormatId = self._thumbnailFormat.id

        try:
            self.session().add(metaData)
            self.session().flush((metaData, ))

            path = self.format_file_name % {
                'id': metaData.Id,
                'name': metaData.Name
            }
            path = ''.join((META_TYPE_KEY, '/',
                            self.generateIdPath(metaData.Id), '/', path))
            contentPath = self.cdmArchive.getURI(path, 'file')

            self.cdmArchive.publishContent(path, content)
            metaData.content = path
            metaData.SizeInBytes = getsize(contentPath)

            for handler in self.metaDataHandlers:
                assert isinstance(
                    handler, IMetaDataHandler), 'Invalid handler %s' % handler
                if handler.processByInfo(metaData, contentPath, content.type):
                    break
            else:
                for handler in self.metaDataHandlers:
                    if handler.process(metaData, contentPath): break

            self.session().merge(metaData)
            self.session().flush((metaData, ))
        except SQLAlchemyError as e:
            handle(e, metaData)

        if metaData.content != path:
            self.cdmArchive.republish(path, metaData.content)

        return metaData.Id
Пример #24
0
    def insert(self, postVerification):
        '''
        @see: IPostVerificationService.insert
        '''
        assert isinstance(postVerification, PostVerification), 'Invalid post verification %s' % postVerification

        postVerificationDb = PostVerificationMapped()
        postVerificationDb.statusId = self._verificationStatusId(postVerification.Status)
        try:
            self.session().add(copy(postVerification, postVerificationDb, exclude=('Status',)))
            self.session().flush((postVerificationDb,))
        except SQLAlchemyError as e: handle(e, postVerificationDb)
        postVerification.Id = postVerificationDb.Id
        return postVerification.Id
Пример #25
0
 def insert(self, entity):
     '''
     @see: IEntityCRUDService.insert
     '''
     assert self.modelType.isValid(entity), 'Invalid entity %s, expected %s' % (entity, self.Entity)
     mentity = self.Entity()
     for prop in self.model.properties:
         if getattr(entity.__class__, prop) in entity: setattr(mentity, prop, getattr(entity, prop))
     try:
         self.session().add(mentity)
         self.session().flush((mentity,))
     except SQLAlchemyError as e: handle(e, mentity)
     entity.Id = mentity.Id
     return entity
Пример #26
0
 def unpublish(self, id):
     '''
     Implementation of @see: IArticleService.unpublish
     '''
     article = self.getById(id)
     assert isinstance(article, Article)
     # unset article publish timestamp
     article.PublishedOn = None
     try:
         self.session().flush((article, ))
     except SQLAlchemyError as e:
         handle(e, article)
     # publish the article content through the content publisher service
     self.contentPublisherService.unpublish(article.Item)
Пример #27
0
    def update(self, user):
        '''
        @see: IUserService.update
        Should not this be handeled automatically via entity service?
        '''
        assert isinstance(user, User), 'Invalid user %s' % user

        userDb = self.session().query(UserMapped).get(user.Id)
        if not userDb:
            assert isinstance(userDb, UserMapped), 'Invalid user %s' % userDb
            raise InputError(Ref(_('Unknown user id'), ref=User.Id))
        try:
            userDb.typeId = self._userTypeId(user.Type)
            self.session().flush((copy(user, userDb, exclude=('Type',)),))
        except SQLAlchemyError as e: handle(e, userDb)
Пример #28
0
    def insert(self, source):
        '''
        @see: ISourceService.insert
        '''
        assert isinstance(source, Source), 'Invalid source %s' % source
        sourceDb = SourceMapped()
        sourceDb.typeId = self._typeId(source.Type)
        copy(source, sourceDb, exclude=('Type',))

        try:
            self.session().add(sourceDb)
            self.session().flush((sourceDb,))
        except SQLAlchemyError as e: handle(e, sourceDb)
        source.Id = sourceDb.Id
        return sourceDb.Id
Пример #29
0
    def requestLogin(self):
        '''
        @see: IAuthenticationService.requestLogin
        '''
        hash = hashlib.sha512()
        hash.update(urandom(self.authentication_token_size))

        token = TokenMapped()
        token.Token = hash.hexdigest()
        token.requestedOn = current_timestamp()

        try: self.session().add(token)
        except SQLAlchemyError as e: handle(e, token)

        return token
Пример #30
0
    def update(self, user):
        '''
        @see: IUserService.update
        Should not this be handeled automatically via entity service?
        '''
        assert isinstance(user, User), 'Invalid user %s' % user

        userDb = self.session().query(UserMapped).get(user.Id)
        if not userDb:
            assert isinstance(userDb, UserMapped), 'Invalid user %s' % userDb
            raise InputError(Ref(_('Unknown user id'), ref=User.Id))
        try:
            userDb.typeId = self._userTypeId(user.Type)
            self.session().flush((copy(user, userDb, exclude=('Type', )), ))
        except SQLAlchemyError as e:
            handle(e, userDb)
Пример #31
0
 def update(self, entity):
     '''
     @see: IEntityCRUDService.update
     '''
     assert self.modelType.isValid(
         entity), 'Invalid entity %s, expected %s' % (entity, self.Entity)
     assert isinstance(
         entity.Id,
         int), 'Invalid entity %s, with id %s' % (entity, entity.Id)
     entityDb = self.session().query(self.Entity).get(entity.Id)
     if not entityDb:
         raise InputError(Ref(_('Unknown id'), ref=self.Entity.Id))
     try:
         self.session().flush((copy(entity, entityDb), ))
     except SQLAlchemyError as e:
         handle(e, self.Entity)
Пример #32
0
    def update(self, item):
        '''
        @see: IItemService.update
        '''
        assert self.modelType.isValid(
            item), 'Invalid item %s, expected %s' % (item, ItemMapped)
        assert isinstance(
            item.GUId, str), 'Invalid item %s, with id %s' % (item, item.GUId)

        itemDb = self.session().query(ItemMapped).get(item.GUId)
        if not itemDb:
            raise InputError(Ref(_('Unknown id'), ref=ItemMapped.GUId))
        try:
            self.session().flush((copy(item, itemDb), ))
        except SQLAlchemyError as e:
            handle(e, ItemMapped)
Пример #33
0
    def insert(self, user):
        '''
        @see: IUserService.insert
        '''
        assert isinstance(user, User), 'Invalid user %s' % user

        userDb = UserMapped()
        userDb.password = user.Password
        userDb.CreatedOn = current_timestamp()
        userDb.typeId = self._userTypeId(user.Type)
        try:
            self.session().add(copy(user, userDb, exclude=('Type',)))
            self.session().flush((userDb,))
        except SQLAlchemyError as e: handle(e, userDb)
        user.Id = userDb.Id
        return user.Id
Пример #34
0
 def update(self, entity):
     '''
     @see: IEntityCRUDService.update
     '''
     assert self.modelType.isValid(
         entity), 'Invalid entity %s, expected %s' % (entity, self.Entity)
     entityDb = self.session().query(self.Entity).get(entity.Id)
     if not entityDb:
         raise InputError(Ref(_('Unknown id'), ref=self.Entity.Id))
     try:
         for prop in self.model.properties:
             if getattr(entity.__class__, prop) in entity:
                 setattr(entityDb, prop, getattr(entity, prop))
         self.session().flush((entityDb, ))
     except SQLAlchemyError as e:
         handle(e, self.Entity)
Пример #35
0
 def changePassword(self, id, password):
     '''
     @see: IUserService.changePassword
     '''
     assert isinstance(password, Password), 'Invalid password change %s' % password
     try: userDb = self.session().query(UserMapped).filter(UserMapped.Id == id).one() #.filter(UserMapped.password == password.OldPassword).one()
     except NoResultFound: userDb = None
     
     if not userDb:
         assert isinstance(userDb, UserMapped), 'Invalid user %s' % userDb
         raise InputError(Ref(_('Invalid user id or old password'), ref=User.Id))
     
     try:
         userDb.password = password.NewPassword
         self.session().flush((userDb,))
     except SQLAlchemyError as e: handle(e, userDb)
Пример #36
0
    def performLogin(self, authentication):
        '''
        @see: IAuthenticationService.performLogin
        '''
        assert isinstance(authentication, Authentication), 'Invalid authentication %s' % authentication

        if authentication.Token is None:
            raise InputError(Ref(_('The login token is required'), ref=Authentication.Token))
        if authentication.HashedToken is None:
            raise InputError(Ref(_('The hashed login token is required'), ref=Authentication.HashedToken))
        if authentication.UserName is None:
            raise InputError(Ref(_('A user name is required for authentication'), ref=Authentication.UserName))

        olderThan = self.session().query(current_timestamp()).scalar()
        olderThan -= self._authenticationTimeOut
        sql = self.session().query(TokenMapped)
        sql = sql.filter(TokenMapped.Token == authentication.Token)
        sql = sql.filter(TokenMapped.requestedOn > olderThan)
        if sql.delete() > 0:
            commitNow()  # We make sure that the delete has been performed

            try: user = self.session().query(UserMapped).filter(UserMapped.Name == authentication.UserName).filter(UserMapped.DeletedOn == None).one()
            except NoResultFound: user = None

            if user is not None:
                assert isinstance(user, UserMapped), 'Invalid user %s' % user

                hashedToken = hmac.new(bytes(user.Name, 'utf8'),
                                       bytes(user.password, 'utf8'), hashlib.sha512).hexdigest()
                hashedToken = hmac.new(bytes(hashedToken, 'utf8'),
                                       bytes(authentication.Token, 'utf8'), hashlib.sha512).hexdigest()

                if authentication.HashedToken == hashedToken:
                    hash = hashlib.sha512()
                    hash.update(urandom(self.authentication_token_size))

                    login = LoginMapped()
                    login.Session = hash.hexdigest()
                    login.User = user.Id
                    login.CreatedOn = login.AccessedOn = current_timestamp()

                    try: self.session().add(login)
                    except SQLAlchemyError as e: handle(e, login)

                    return login

        raise InputError(_('Invalid credentials'))
Пример #37
0
    def requestLogin(self):
        '''
        @see: IAuthenticationService.requestLogin
        '''
        hash = hashlib.sha512()
        hash.update(urandom(self.authentication_token_size))

        token = TokenMapped()
        token.Token = hash.hexdigest()
        token.requestedOn = current_timestamp()

        try:
            self.session().add(token)
        except SQLAlchemyError as e:
            handle(e, token)

        return token
Пример #38
0
    def insert(self, user):
        '''
        @see: IUserService.insert
        '''
        assert isinstance(user, User), 'Invalid user %s' % user

        userDb = UserMapped()
        userDb.password = user.Password
        try:
            self.session().add(copy(user, userDb))
            self.session().flush((userDb,))
        except SQLAlchemyError as e: handle(e, userDb)
        user.Id = userDb.Id
        return user.Id

        user.password = user.Password # We set the password value.
        return super().insert(user)
Пример #39
0
 def insert(self, entity):
     '''
     @see: IEntityCRUDService.insert
     '''
     assert self.modelType.isValid(
         entity), 'Invalid entity %s, expected %s' % (entity, self.Entity)
     mentity = self.Entity()
     for prop in self.model.properties:
         if getattr(entity.__class__, prop) in entity:
             setattr(mentity, prop, getattr(entity, prop))
     try:
         self.session().add(mentity)
         self.session().flush((mentity, ))
     except SQLAlchemyError as e:
         handle(e, mentity)
     entity.Id = mentity.Id
     return entity
Пример #40
0
    def insert(self, user):
        '''
        @see: IUserService.insert
        '''
        assert isinstance(user, User), 'Invalid user %s' % user

        userDb = UserMapped()
        userDb.password = user.Password
        userDb.CreatedOn = current_timestamp()
        userDb.typeId = self._userTypeId(user.Type)
        try:
            self.session().add(copy(user, userDb, exclude=('Type', )))
            self.session().flush((userDb, ))
        except SQLAlchemyError as e:
            handle(e, userDb)
        user.Id = userDb.Id
        return user.Id
Пример #41
0
    def update(self, media:BlogMedia):
        '''
        @see: IBlogMediaService.update
        '''
        assert isinstance(media, BlogMedia), 'Invalid blog media %s' % media
        mediaDb = self.session().query(BlogMediaMapped).get(media.Id)
        if not mediaDb: raise InputError(Ref(_('Unknown blog media id'), ref=BlogMedia.Id))

        copy(media, mediaDb, exclude=('Type',))
        if BlogMedia.Type in media: mediaDb.typeId = self._typeId(media.Type)

        if (BlogMedia.Rank in media) and (media.Rank < 1):
            mediaDb.Rank = self._nextRank(mediaDb.Id, mediaDb.Blog, mediaDb.typeId)
    
        try:
            self.session().flush((mediaDb,))
        except SQLAlchemyError as e: handle(e, mediaDb)
Пример #42
0
    def insert(self, parentId, configuration):
        '''
        @see: IConfigurationService.insert
        '''
        assert isinstance(parentId, int), 'Invalid parentId'
        assert isinstance(configuration, Configuration), 'Invalid configuration'

        if not configuration.Name:
            raise InputError(Ref(_('No configuration name'),))

        configurationDb = copy(configuration, self.ConfigurationMapped())
        configurationDb.parent = parentId
        try:
            self.session().add(configurationDb)
            self.session().flush((configurationDb,))
        except SQLAlchemyError as e: handle(e, configurationDb)
        return configurationDb.Name
Пример #43
0
 def setIcon(self, personId, metaDataId, update):
     '''
     @see: IPersonIconService.setIcon
     '''
  
     if update:   
         user = User()
         user.Id = personId
         self.userService.update(user)
     
     entityDb = PersonIconMapped()
     entityDb.Id, entityDb.MetaData = personId, metaDataId
     
     try:
         self.session().merge(entityDb)
         self.session().flush((entityDb,))
     except SQLAlchemyError as e: handle(e, entityDb)
     return entityDb.Id
Пример #44
0
    def update(self, parentId, configuration):
        '''
        @see: IConfigurationService.update
        '''
        assert isinstance(parentId, int), 'Invalid parentId'
        assert isinstance(configuration, Configuration), 'Invalid configuration'

        sql = self.session().query(self.ConfigurationMapped)
        sql = sql.filter(self.ConfigurationMapped.parent == parentId)
        sql = sql.filter(self.ConfigurationMapped.Name == configuration.Name)
        try:
            configurationDb = sql.one()
        except NoResultFound: raise InputError(Ref(_('Unknown configuration'),))

        configurationDb.Value = configuration.Value
        configurationDb.parent = parentId
        try: self.session().flush((configurationDb,))
        except SQLAlchemyError as e: handle(e, self.ConfigurationMapped)
Пример #45
0
    def insert(self, parentId, configuration):
        '''
        @see: IConfigurationService.insert
        '''
        assert isinstance(parentId, int), 'Invalid parentId'
        assert isinstance(configuration,
                          Configuration), 'Invalid configuration'

        if not configuration.Name:
            raise InputError(Ref(_('No configuration name'), ))

        configurationDb = copy(configuration, self.ConfigurationMapped())
        configurationDb.parent = parentId
        try:
            self.session().add(configurationDb)
            self.session().flush((configurationDb, ))
        except SQLAlchemyError as e:
            handle(e, configurationDb)
        return configurationDb.Name
Пример #46
0
    def update(self, entity):
        '''
        @see: IEntityCRUDService.update
        '''
        assert self.modelType.isValid(
            entity), 'Invalid entity %s, expected %s' % (entity, self.Entity)
        assert isinstance(
            entity.Key,
            str), 'Invalid entity %s, with key %s' % (entity, entity.Key)

        try:
            entityDb = self.session().query(
                self.Entity).filter(self.Entity.Key == entity.Key)
        except NoResultFound:
            raise InputError(Ref(_('Unknown key'), ref=self.Entity.Key))
        try:
            self.session().flush((copy(entity, entityDb), ))
        except SQLAlchemyError as e:
            handle(e, self.Entity)
Пример #47
0
    def insert(self, userId, content):
        '''
        @see: IMetaDataService.insert
        '''
        assert isinstance(content, Content), 'Invalid content %s' % content
        if not content.name: raise InputError(_('No name specified for content'))

        metaData = MetaDataMapped()
        metaData.CreatedOn = current_timestamp()
        metaData.Creator = userId
        metaData.Name = content.name

        metaData.typeId = self._metaType.Id
        metaData.thumbnailFormatId = self._thumbnailFormat.id

        try:
            self.session().add(metaData)
            self.session().flush((metaData,))

            path = self.format_file_name % {'id': metaData.Id, 'name': metaData.Name}
            path = ''.join((META_TYPE_KEY, '/', self.generateIdPath(metaData.Id), '/', path))
            contentPath = self.cdmArchive.getURI(path, 'file')

            self.cdmArchive.publishContent(path, content)
            metaData.content = path
            metaData.SizeInBytes = getsize(contentPath)

            for handler in self.metaDataHandlers:
                assert isinstance(handler, IMetaDataHandler), 'Invalid handler %s' % handler
                if handler.processByInfo(metaData, contentPath, content.type): break
            else:
                for handler in self.metaDataHandlers:
                    if handler.process(metaData, contentPath): break

            self.session().merge(metaData)
            self.session().flush((metaData,))
        except SQLAlchemyError as e: handle(e, metaData)


        if metaData.content != path:
            self.cdmArchive.republish(path, metaData.content)

        return metaData.Id
Пример #48
0
    def insert(self, media:BlogMedia):
        '''
        @see: IBlogMediaService.insert
        '''
        assert isinstance(media, BlogMedia), 'Invalid blog media %s' % media
        mediaDb = BlogMediaMapped()
        copy(media, mediaDb, exclude=('Type',))
        mediaDb.typeId = self._typeId(media.Type)

        if (not BlogMedia.Rank in media) or (media.Rank < 1):
            mediaDb.Rank = self._nextRank(0, mediaDb.Blog, mediaDb.typeId)

        try:
            self.session().add(mediaDb)
            self.session().flush((mediaDb,))
        except SQLAlchemyError as e: handle(e, mediaDb)

        media.Id = mediaDb.Id
        return media.Id
Пример #49
0
    def update(self, media: BlogMedia):
        '''
        @see: IBlogMediaService.update
        '''
        assert isinstance(media, BlogMedia), 'Invalid blog media %s' % media
        mediaDb = self.session().query(BlogMediaMapped).get(media.Id)
        if not mediaDb:
            raise InputError(Ref(_('Unknown blog media id'), ref=BlogMedia.Id))

        copy(media, mediaDb, exclude=('Type', ))
        if BlogMedia.Type in media: mediaDb.typeId = self._typeId(media.Type)

        if (BlogMedia.Rank in media) and (media.Rank < 1):
            mediaDb.Rank = self._nextRank(mediaDb.Id, mediaDb.Blog,
                                          mediaDb.typeId)

        try:
            self.session().flush((mediaDb, ))
        except SQLAlchemyError as e:
            handle(e, mediaDb)
Пример #50
0
    def insert(self, media: BlogMedia):
        '''
        @see: IBlogMediaService.insert
        '''
        assert isinstance(media, BlogMedia), 'Invalid blog media %s' % media
        mediaDb = BlogMediaMapped()
        copy(media, mediaDb, exclude=('Type', ))
        mediaDb.typeId = self._typeId(media.Type)

        if (not BlogMedia.Rank in media) or (media.Rank < 1):
            mediaDb.Rank = self._nextRank(0, mediaDb.Blog, mediaDb.typeId)

        try:
            self.session().add(mediaDb)
            self.session().flush((mediaDb, ))
        except SQLAlchemyError as e:
            handle(e, mediaDb)

        media.Id = mediaDb.Id
        return media.Id
Пример #51
0
    def insert(self, user):
        '''
        @see: IUserService.insert
        '''
        assert isinstance(user, User), 'Invalid user %s' % user

        sql = self.session().query(UserMapped)
        sql = sql.filter(UserMapped.Name == user.Name)
        sql = sql.filter(UserMapped.DeletedOn == None)
        #        if sql.count() > 0: raise InputError(Ref(_('There is already a user with this name'), ref=User.Name))

        userDb = UserMapped()
        userDb.password = user.Password
        userDb.CreatedOn = current_timestamp()
        try:
            self.session().add(copy(user, userDb))
            self.session().flush((userDb, ))
        except SQLAlchemyError as e:
            handle(e, userDb)
        user.Id = userDb.Id
        return user.Id
Пример #52
0
    def update(self, postVerification):
        '''
        @see: IPostVerificationService.update
        '''
        assert isinstance(postVerification, PostVerification), 'Invalid post verification %s' % postVerification

        postVerificationDb = self.session().query(PostVerificationMapped).get(postVerification.Id)
        if not postVerificationDb:
            assert isinstance(postVerificationDb, PostVerificationMapped), 'Invalid post verification %s' % postVerificationDb
            raise InputError(Ref(_('Unknown post verification id'), ref=PostVerification.Id))
        try:
            if postVerification.Status:
                postVerificationDb.statusId = self._verificationStatusId(postVerification.Status)
            if postVerification.Checker:
                postVerificationDb.Checker = postVerification.Checker    
            self.session().flush((postVerificationDb,))
        except SQLAlchemyError as e: handle(e, postVerificationDb)
        
        #increase the CId for blog post
        sql = self.session().query(BlogPostMapped).filter(BlogPostMapped.Id == postVerification.Id)
        blogPost = sql.one()
        self.blogPostService.updateCid(blogPost.Blog, blogPost.Id)
Пример #53
0
    def update(self, parentId, configuration):
        '''
        @see: IConfigurationService.update
        '''
        assert isinstance(parentId, int), 'Invalid parentId'
        assert isinstance(configuration,
                          Configuration), 'Invalid configuration'

        sql = self.session().query(self.ConfigurationMapped)
        sql = sql.filter(self.ConfigurationMapped.parent == parentId)
        sql = sql.filter(self.ConfigurationMapped.Name == configuration.Name)
        try:
            configurationDb = sql.one()
        except NoResultFound:
            raise InputError(Ref(_('Unknown configuration'), ))

        configurationDb.Value = configuration.Value
        configurationDb.parent = parentId
        try:
            self.session().flush((configurationDb, ))
        except SQLAlchemyError as e:
            handle(e, self.ConfigurationMapped)
Пример #54
0
    def update(self, user):
        '''
        @see: IUserService.update
        '''
        assert isinstance(user, User), 'Invalid user %s' % user

        userDb = self.session().query(UserMapped).get(user.Id)
        if not userDb or userDb.DeletedOn is not None:
            assert isinstance(userDb, UserMapped), 'Invalid user %s' % userDb
            raise InputError(Ref(_('Unknown user id'), ref=User.Id))
        try:
            sql = self.session().query(UserMapped)
            sql = sql.filter(UserMapped.Id != user.Id)
            sql = sql.filter(UserMapped.Name == user.Name)
            sql = sql.filter(UserMapped.DeletedOn == None)
            if sql.count() > 0:
                raise InputError(
                    Ref(_('There is already a user with this name'),
                        ref=User.Name))

            self.session().flush((copy(user, userDb), ))
        except SQLAlchemyError as e:
            handle(e, userDb)
Пример #55
0
    def changePassword(self, id, password):
        '''
        @see: IUserService.changePassword
        '''
        assert isinstance(password,
                          Password), 'Invalid password change %s' % password
        try:
            userDb = self.session().query(UserMapped).filter(
                UserMapped.Id == id).one(
                )  #.filter(UserMapped.password == password.OldPassword).one()
        except NoResultFound:
            userDb = None

        if not userDb or userDb.DeletedOn is not None:
            assert isinstance(userDb, UserMapped), 'Invalid user %s' % userDb
            raise InputError(
                Ref(_('Invalid user id or old password'), ref=User.Id))

        try:
            userDb.password = password.NewPassword
            self.session().flush((userDb, ))
        except SQLAlchemyError as e:
            handle(e, userDb)
Пример #56
0
    def addSource(self, blogId, source):
        '''
        @see: IBlogSourceService.addSource
        NB: The source must have the correct type set in.
            This way, we can reuse it for other purposes, apart from the chained blogs.
        '''
        assert isinstance(blogId, int), 'Invalid blog identifier %s' % blogId
        assert isinstance(source, Source), 'Invalid source %s' % source

        # insert source if it didn't exist yet
        q = QSource(name=source.Name)
        sources = self.sourceService.getAll(typeKey=source.Type, q=q)
        if not sources: sourceId = self.sourceService.insert(source)
        else: sourceId = sources[0].Id

        ent = BlogSourceDB()
        ent.blog = blogId
        ent.source = sourceId
        try:
            self.session().add(ent)
            self.session().flush((ent, ))
        except SQLAlchemyError as e:
            handle(e, ent)
        return sourceId