def read(self, nbytes=None): ''' @see: Content.read ''' if not self._response: try: req = Request(self._url, headers={'User-Agent': 'Magic Browser'}) self._response = urlopen(req) except (HTTPError, socket.error) as e: log.error('Can not read icon image data %s' % e) raise InputError(Ref(_('Can not open icon URL'), )) if not self._response: log.error('Can not read icon image data %s' % e) raise InputError(Ref(_('Can not open icon URL'), )) if str(self._response.status) != '200': raise InputError(Ref(_('Can not open icon URL'), )) self.type = self._response.getheader('Content-Type') if not self.type: self.type = 'image' self.length = self._response.getheader('Content-Length') if not self.length: self.length = 0 if (not self._response) or self._response.closed: return '' try: if nbytes: return self._response.read(nbytes) return self._response.read() except (HTTPError, socket.error) as e: log.error('Can not read icon image data %s' % e) raise InputError(Ref(_('Can not read from icon URL'), ))
def addCollaborator(self, blogId, collaboratorId, typeName): ''' @see: IBlogCollaboratorService.addCollaborator ''' typeId = self.collaboratorTypeIds()[typeName] if typeId is None: raise InputError( Ref(_('Invalid collaborator type'), ref=BlogCollaborator.Type)) sql = self.session().query(BlogCollaboratorEntry) sql = sql.filter(BlogCollaboratorEntry.Blog == blogId) sql = sql.filter( BlogCollaboratorEntry.blogCollaboratorId == collaboratorId) if sql.update({BlogCollaboratorEntry.typeId: typeId}) > 0: return sql = self.session().query(BlogCollaboratorMapped.Id) sql = sql.join(BlogMapped) sql = sql.filter(BlogCollaboratorMapped.User == BlogMapped.Creator) sql = sql.filter(BlogMapped.Id == blogId) sql = sql.filter(BlogCollaboratorMapped.Id == collaboratorId) if sql.count() > 0: raise InputError( _('The blog creator cannot be assigned as a collaborator')) bgc = BlogCollaboratorEntry() bgc.Blog = blogId bgc.blogCollaboratorId = collaboratorId bgc.typeId = typeId self.session().add(bgc) self.session().flush((bgc, ))
def handle(e, entity): ''' Handles the SQL alchemy exception while inserting or updating. ''' if isinstance(e, IntegrityError): raise InputError( Ref(_('Cannot persist, failed unique constraints on entity'), model=typeFor(entity).container)) if isinstance(e, OperationalError): raise InputError( Ref(_('A foreign key is not valid'), model=typeFor(entity).container)) raise e
def getURLInfo(self, url=None): ''' @see: IURLInfoService.getURLInfo ''' if not url: raise InputError('Invalid URL %s' % url) assert isinstance(url, str), 'Invalid URL %s' % url url = unquote(url) try: with urlopen(url) as conn: urlInfo = URLInfo() urlInfo.URL = url urlInfo.Date = datetime.now() contentType = None charset = 'utf_8' for tag, val in conn.info().items(): if tag == 'Content-Type': contentTypeInfo = val.split(';') contentType = contentTypeInfo[0].strip().lower() if 2 == len(contentTypeInfo): charset = contentTypeInfo[1].split('=')[1] break if not contentType or contentType != 'text/html': req = Request(url) selector = req.get_selector().strip('/') if selector: parts = selector.split('/') if parts: urlInfo.Title = parts[len(parts) - 1] else: urlInfo.Title = req.get_host() return urlInfo elif contentType == 'text/html': urlInfo.ContentType = contentType extr = HTMLInfoExtractor(urlInfo) try: readData = conn.read() decodedData = '' try: decodedData = readData.decode(charset, 'ignore') except Exception as e: decodedData = readData.decode('utf_8', 'ignore') for onePair in self.html_fixes: decodedData = re.sub(onePair['from'], onePair['to'], decodedData) extr.feed(decodedData) except (AssertionError, HTMLParseError, UnicodeDecodeError): pass return extr.urlInfo except (URLError, ValueError): raise InputError('Invalid URL %s' % url)
def reorder(self, blogId, postId, refPostId, before=True): ''' @see: IBlogPostService.reorder ''' sql = self.session().query(BlogPostMapped.Order) sql = sql.filter(BlogPostMapped.Blog == blogId) sql = sql.filter(BlogPostMapped.Id == refPostId) order = sql.scalar() if not order: raise InputError(Ref(_('Invalid before post'))) sql = self.session().query(BlogPostMapped.Order) sql = sql.filter(BlogPostMapped.Blog == blogId) sql = sql.filter(BlogPostMapped.Id != postId) if before: sql = sql.filter(BlogPostMapped.Order > order) sql = sql.order_by(BlogPostMapped.Order) else: sql = sql.filter(BlogPostMapped.Order < order) sql = sql.order_by(desc_op(BlogPostMapped.Order)) sql = sql.limit(1) orderPrev = sql.scalar() if orderPrev: order = (order + orderPrev) / 2 elif before: order += 1 else: order -= 1 post = self.getById(blogId, postId) assert isinstance(post, BlogPostMapped) post.Order = order post.CId = self._nextCId() self.session().merge(post)
def getPluginPOFile(self, plugin, locale, scheme): ''' @see: IPOFileService.getPluginPOFile ''' pluginObj = self.pluginService.getById(plugin) assert isinstance(pluginObj, Plugin) if pluginObj.Component: return self.getComponentPOFile(pluginObj.Component, locale, scheme) path = self._cdmPath(locale, plugin=plugin) try: try: cdmFileTimestamp = self.cdmLocale.getTimestamp(path) except PathNotFound: republish = True else: mngFileTimestamp = max( self.poFileManager.getGlobalPOTimestamp(locale) or datetime.min, self.poFileManager.getPluginPOTimestamp(plugin, locale) or datetime.min) republish = False if mngFileTimestamp is None else cdmFileTimestamp < mngFileTimestamp if republish: self.cdmLocale.publishFromFile( path, self.poFileManager.getPluginPOFile(plugin, locale)) except InvalidLocaleError: raise InputError( _('Invalid locale %(locale)s') % dict(locale=locale)) return self.cdmLocale.getURI(path, scheme)
def reorder(self, blogTypeId, postId, refPostId, before=True): ''' @see: IBlogPostService.reorder ''' sql = self.session().query(BlogTypePostMapped.Order) sql = sql.filter(BlogTypePostMapped.BlogType == blogTypeId) sql = sql.filter(BlogTypePostMapped.Id == refPostId) order = sql.scalar() if order is None: raise InputError(Ref(_('Invalid before post'))) sql = self.session().query(BlogTypePostMapped.Order) sql = sql.filter(BlogTypePostMapped.BlogType == blogTypeId) sql = sql.filter(BlogTypePostMapped.Id != postId) if before: sql = sql.filter(BlogTypePostMapped.Order < order) sql = sql.order_by(desc_op(BlogTypePostMapped.Order)) else: sql = sql.filter(BlogTypePostMapped.Order > order) sql = sql.order_by(BlogTypePostMapped.Order) sql = sql.limit(1) orderPrev = sql.scalar() if orderPrev is not None: order = (order + orderPrev) / 2 else: order = order - 1 if before else order + 1 post = self.getById(blogTypeId, postId) assert isinstance(post, BlogTypePostMapped) post.Order = order self.session().merge(post) self.session().flush((post, ))
def getByCode(self, code, locales): ''' @see: ILanguageService.getByCode ''' locale = self._localeOf(code) if not locale: raise InputError(Ref(_('Unknown language code'), ref=Language.Code)) return self._populate(Language(code), self._translator(locale, self._localesOf(locales)))
def getMethod(self, id): ''' @see: IRequestService.getMethod ''' self._refresh() if id not in self._methods: raise InputError(Ref(_('Invalid method id'), ref=Method.Id)) return self._methods[id]
def getById(self, id, scheme, thumbSize=None): ''' @see: IMetaDataService.getById ''' metaData = self.session().query(self.MetaData).get(id) if metaData is None: raise InputError(Ref(_('Unknown meta data'), ref=self.MetaData.Id)) return self.referencer.populate(metaData, scheme, thumbSize)
def getRequest(self, id): ''' @see: IRequestService.getRequest ''' self._refresh() if id not in self._requests: raise InputError(Ref(_('Invalid request id'), ref=Request.Id)) return self._requests[id]
def getByCode(self, code, locales): ''' @see: ICountryService.getByCode ''' if code not in self.countries: raise InputError(Ref(_('Unknown country code'), ref=Country.Code)) return Country(code, self._translate(code, self._localesOf(locales)))
def deleteSource(self, blogId, sourceId): ''' @see: IBlogSourceService.deleteSource ''' assert isinstance(blogId, int), 'Invalid blog identifier %s' % blogId assert isinstance(sourceId, int), 'Invalid source identifier %s' % sourceId try: res = self.session().query(BlogSourceDB).filter( BlogSourceDB.blog == blogId).filter( BlogSourceDB.source == sourceId).delete() > 0 if res: sourceTypeKey, = self.session().query( SourceTypeMapped.Key).join( SourceMapped, SourceTypeMapped.id == SourceMapped.typeId).filter( SourceMapped.Id == sourceId).one() if sourceTypeKey in self.sources_auto_delete: self.sourceService.delete(sourceId) return res except OperationalError: assert log.debug( 'Could not delete blog source with blog id \'%s\' and source id \'%s\'', blogId, sourceId, exc_info=True) or True raise InputError(Ref(_('Cannot delete because is in use'), ))
def getComponentPOFile(self, component, locale, scheme): ''' @see: IPOFileService.getComponentPOFile ''' self.componentService.getById(component) path = self._cdmPath(locale, component=component) try: try: cdmFileTimestamp = self.cdmLocale.getTimestamp(path) except PathNotFound: republish = True else: mngFileTimestamp = max( self.poFileManager.getGlobalPOTimestamp(locale) or datetime.min, self.poFileManager.getComponentPOTimestamp( component, locale) or datetime.min) republish = False if mngFileTimestamp is None else cdmFileTimestamp < mngFileTimestamp if republish: self.cdmLocale.publishFromFile( path, self.poFileManager.getComponentPOFile(component, locale)) except InvalidLocaleError: raise InputError( _('Invalid locale %(locale)s') % dict(locale=locale)) return self.cdmLocale.getURI(path, scheme)
def getComponentJSONFile(self, component, locale, scheme): ''' @see: IPOService.getComponentPOFile ''' self.componentService.getById(component) path = self._cdmPath(locale, component=component) try: try: cdmFileTimestamp = self.cdmLocale.getTimestamp(path) except PathNotFound: republish = True else: mngFileTimestamp = max( self.poFileManager.getGlobalPOTimestamp(locale) or datetime.min, self.poFileManager.getComponentPOTimestamp( component, locale) or datetime.min) republish = False if mngFileTimestamp is None else cdmFileTimestamp < mngFileTimestamp if republish: jsonString = JSONEncoder(ensure_ascii=False).encode( self.poFileManager.getComponentAsDict(component, locale)) self.cdmLocale.publishContent( path, BytesIO(bytes(jsonString, getdefaultencoding()))) except InvalidLocaleError: raise InputError( _('Invalid locale %(locale)s') % dict(locale=locale)) return self.cdmLocale.getURI(path, scheme)
def getById(self, id, locales): ''' @see: ILanguageService.getById ''' locales = self._localesOf(locales) language = self.session().query(LanguageEntity).get(id) if not language: raise InputError(Ref(_('Unknown language id'), ref=LanguageEntity.Id)) return self._populate(language, self._translator(self._localeOf(language.Code), locales))
def getById(self, id): ''' @see: IUserService.getById ''' user = self.session().query(UserMapped).get(id) if not user: raise InputError(Ref(_('Unknown user id'), ref=User.Id)) assert isinstance(user, UserMapped), 'Invalid user %s' % user return user
def detachIcon(self, personIconId): ''' @see: IPersonIconService.detachIcon ''' try: return self.session().query(PersonIconMapped).filter(PersonIconMapped.Id == personIconId).delete() > 0 except (OperationalError, IntegrityError): raise InputError(Ref(_('Can not detach person icon because in use'),))
def getAllInputs(self, id): ''' @see: IRequestService.getAllInputs ''' self._refresh() if not id: return self._inputs.values() if id not in self._patternInputs: raise InputError(Ref(_('Invalid request id'), ref=Request.Id)) return (self._inputs[inpId] for inpId in self._patternInputs[id])
def getById(self, id): ''' @see: IEntityGetService.getById ''' entity = self.session().query(self.Entity).get(id) if not entity: raise InputError(Ref(_('Unknown id'), ref=self.Entity.Id)) return entity
def getById(self, guid): ''' Implementation for @see: IItemService.getById ''' item = self.session().query(ItemMapped).get(guid) if not item: raise InputError(Ref(_('Unknown id'), ref=ItemMapped.GUId)) return item
def getByName(self, name): ''' @see: IRoleService.getByName ''' try: return self.session().query(RoleMapped).filter( RoleMapped.Name == name).one() except NoResultFound: raise InputError(Ref(_('Unknown name'), ref=Role.Name))
def getById(self, id): ''' @see: IPluginService.getById ''' assert isinstance(id, str), 'Invalid id %s' % id modules = modulesIn('%s.%s' % ('__plugin__', id)).asList() if len(modules) != 1: raise InputError(Ref(_('Invalid plugin id'), ref=Plugin.Id)) return self.pluginFor(modules[0])
def getById(self, id): ''' @see: IComponentService.getById ''' assert isinstance(id, str), 'Invalid id %s' % id modules = modulesIn('%s.%s' % (self.package, id)).asList() if len(modules) != 1: raise InputError(Ref(_('Invalid component id'), ref=Component.Id)) return self.componentFor(modules[0])
def getByKey(self, key): ''' @see: IEntityGetService.getByKey ''' try: return self.session().query( self.Entity).filter(self.Entity.Key == key).one() except NoResultFound: raise InputError(Ref(_('Unknown key'), ref=self.Entity.Key))
def putLive(self, blogId): ''' @see: IBlogService.putLive ''' blog = self.session().query(BlogMapped).get(blogId) if not blog: raise InputError(_('Invalid blog or credentials')) assert isinstance(blog, Blog), 'Invalid blog %s' % blog blog.LiveOn = current_timestamp() if blog.LiveOn is None else None self.session().merge(blog)
def unhide(self, blogId): ''' @see: IBlogService.unhide ''' blog = self.session().query(BlogMapped).get(blogId) if not blog: raise InputError(_('Invalid blog or credentials')) assert isinstance(blog, Blog), 'Invalid blog %s' % blog blog.DeletedOn = None self.session().merge(blog)
def _typeId(self, key): ''' Provides the source type id that has the provided key. ''' try: sql = self.session().query(SourceTypeMapped.id).filter(SourceTypeMapped.Key == key) return sql.one()[0] except NoResultFound: raise InputError(Ref(_('Invalid source type %(type)s') % dict(type=key), ref=Source.Type))
def delete(self, id): ''' @see: IEntityCRUDService.delete ''' try: return self.session().query( self.Entity).filter(self.Entity.Id == id).delete() > 0 except OperationalError: raise InputError( Ref(_('Cannot delete because is in use'), model=self.model))
def getById(self, id): ''' @see: IMetaTypeService.getById ''' try: return self.session().query(MetaTypeMapped).filter( MetaTypeMapped.Id == id).one() except NoResultFound: raise InputError( Ref(_('Unknown meta type id'), ref=MetaTypeMapped.Id))