Пример #1
0
class ArticlesTab(object):
    def __init__(self, parent, type):
        self.parent = parent
        self.type = type
        self.article_repository = ArticleRepository()

    @property
    def articles(self):
        return self.article_repository.get_by_type(self.type)

    def edit_article(self, id):
        self.parent.edit_article(id=id)

    def preview_article(self, id):
        self.parent.preview_article(id=id)

    def create_article(self, type):
        self.parent.edit_article(type=type)

    def has_single_published_article(self, type):
        return type == ArticleType.Headline

    def set_publish(self, id, published):
        article = self.article_repository.get_by_id(id)
        type = article.type
        if self.has_single_published_article(type):  # unpublish other articles
            for a in self.article_repository.get_by_type(type):
                a.published = False
        article.published = published

    def delete_article(self, id):
        self.article_repository.get_by_id(id).delete()
Пример #2
0
class ArticlesTab(object):
    def __init__(self, parent, type):
        self.parent = parent
        self.type = type
        self.article_repository = ArticleRepository()

    @property
    def articles(self):
        return self.article_repository.get_by_type(self.type)

    def edit_article(self, id):
        self.parent.edit_article(id=id)

    def preview_article(self, id):
        self.parent.preview_article(id=id)

    def create_article(self, type):
        self.parent.edit_article(type=type)

    def has_single_published_article(self, type):
        return type == ArticleType.Headline

    def set_publish(self, id, published):
        article = self.article_repository.get_by_id(id)
        type = article.type
        if self.has_single_published_article(type):  # unpublish other articles
            for a in self.article_repository.get_by_type(type):
                a.published = False
        article.published = published

    def delete_article(self, id):
        self.article_repository.get_by_id(id).delete()
Пример #3
0
    def __init__(self, page, article_id, type=None, mobile_access=False):
        super(ArticleEditor, self).__init__(None)
        self.mobile_access = mobile_access
        article = ArticleRepository().get_by_id(article_id)

        self.page = page
        self.creation = article is None

        self.DEFAULT_TOPIC = ArticleTopicData.get_by(default=True).label
        # gets the initial values
        type = article.type if article else (type or self.DEFAULT_TYPE)
        topic = article.topic.label if article else self.DEFAULT_TOPIC
        title = article.title if article else u''
        content = article.content if article else u''
        mobile_content = article.mobile_content if article else u''
        tags = u', '.join(article.tags) if article else u''

        # creates the properties
        self.type = editor.Property(type)
        self.topic = editor.Property(topic)
        self.title = editor.Property(title).validate(
            validators.non_empty_string)
        self.thumbnail = editor.Property().validate(self._validate_thumbnail)
        self.thumbnail_filename = None
        self.content = editor.Property(content).validate(validators.string)
        self.mobile_content = editor.Property(mobile_content).validate(
            validators.string)
        self.tags = editor.Property(tags).validate(
            lambda t: validators.word_list(t, duplicates='remove'))
Пример #4
0
    def edit_article(self, id=None, type=None):
        # handles None being passed as a id for convenience
        id = -1 if id is None else id

        # creates the editor
        editor = ArticleEditor(self, id, type=type, mobile_access=self.mobile_access)

        # starts edition
        if self.content.call(editor):
            # gets the article type
            article_type = editor.get_type()
            article_topic = ArticleTopicData.get_by(label=editor.get_topic())

            # write the thumbnail down
            thumbnails_dir = get_fs_service().expand_path(
                ['articles-thumbnails'])
            thumbnail = editor.get_thumbnail()
            thumbnail_filename = None
            thumbnail_path = None

            if thumbnail:
                thumbnail_extension = os.path.splitext(
                    editor.get_thumbnail_filename())[1].lower()
                os.path.splitext(editor.get_thumbnail_filename())[1].lower()
                os.path.splitext(editor.get_thumbnail_filename())[1].lower()
                thumbnail_filename = uuid.uuid4().hex + thumbnail_extension  # random filename
                thumbnail_path = os.path.join(thumbnails_dir,
                                              thumbnail_filename)

                with open(thumbnail_path, 'wb') as target:
                    shutil.copyfileobj(StringIO(thumbnail), target)

            # creates the article if it does not exist yet
            article_repository = ArticleRepository()
            article = article_repository.get_by_id(id)

            # FIXME: we should save the article in the ArticleEditor, not here
            if article:
                if article.thumbnail_filename:
                    if thumbnail:
                        # removes the old thumbnail if a new thumbnail has been uploaded
                        tools.remove_silently(os.path.join(thumbnails_dir,
                                                           article.thumbnail_filename))
                    else:
                        # otherwise, keep the old thumbnail
                        thumbnail_filename = article.thumbnail_filename

                article.type = article_type
                article.topic = article_topic
                article.title = editor.get_title()
                article.thumbnail_filename = thumbnail_filename
                article.content = editor.get_content()
                article.mobile_content = editor.get_mobile_content()
                article.tags = editor.get_tags()
            else:
                # FIXME: this algorithm is not scalable. Ranks should be reversed in that case
                for n in article_repository.get_by_type(article_type):
                    n.rank += 1

                article_repository.create(type=article_type,
                                          topic=article_topic,
                                          title=editor.get_title(),
                                          creation_date=datetime.now(),
                                          thumbnail_filename=thumbnail_filename,
                                          content=editor.get_content(),
                                          mobile_content=editor.get_mobile_content(),
                                          tags=editor.get_tags(),
                                          rank=1, published=False)
Пример #5
0
 def __init__(self, parent, type):
     self.parent = parent
     self.type = type
     self.article_repository = ArticleRepository()
Пример #6
0
    def edit_article(self, id=None, type=None):
        # handles None being passed as a id for convenience
        id = -1 if id is None else id

        # creates the editor
        editor = ArticleEditor(self,
                               id,
                               type=type,
                               mobile_access=self.mobile_access)

        # starts edition
        if self.content.call(editor):
            # gets the article type
            article_type = editor.get_type()
            article_topic = ArticleTopicData.get_by(label=editor.get_topic())

            # write the thumbnail down
            thumbnails_dir = get_fs_service().expand_path(
                ['articles-thumbnails'])
            thumbnail = editor.get_thumbnail()
            thumbnail_filename = None
            thumbnail_path = None

            if thumbnail:
                thumbnail_extension = os.path.splitext(
                    editor.get_thumbnail_filename())[1].lower()
                os.path.splitext(editor.get_thumbnail_filename())[1].lower()
                os.path.splitext(editor.get_thumbnail_filename())[1].lower()
                thumbnail_filename = uuid.uuid4(
                ).hex + thumbnail_extension  # random filename
                thumbnail_path = os.path.join(thumbnails_dir,
                                              thumbnail_filename)

                with open(thumbnail_path, 'wb') as target:
                    shutil.copyfileobj(StringIO(thumbnail), target)

            # creates the article if it does not exist yet
            article_repository = ArticleRepository()
            article = article_repository.get_by_id(id)

            # FIXME: we should save the article in the ArticleEditor, not here
            if article:
                if article.thumbnail_filename:
                    if thumbnail:
                        # removes the old thumbnail if a new thumbnail has been uploaded
                        tools.remove_silently(
                            os.path.join(thumbnails_dir,
                                         article.thumbnail_filename))
                    else:
                        # otherwise, keep the old thumbnail
                        thumbnail_filename = article.thumbnail_filename

                article.type = article_type
                article.topic = article_topic
                article.title = editor.get_title()
                article.thumbnail_filename = thumbnail_filename
                article.content = editor.get_content()
                article.mobile_content = editor.get_mobile_content()
                article.tags = editor.get_tags()
            else:
                # FIXME: this algorithm is not scalable. Ranks should be reversed in that case
                for n in article_repository.get_by_type(article_type):
                    n.rank += 1

                article_repository.create(
                    type=article_type,
                    topic=article_topic,
                    title=editor.get_title(),
                    creation_date=datetime.now(),
                    thumbnail_filename=thumbnail_filename,
                    content=editor.get_content(),
                    mobile_content=editor.get_mobile_content(),
                    tags=editor.get_tags(),
                    rank=1,
                    published=False)
Пример #7
0
 def __init__(self, parent, type):
     self.parent = parent
     self.type = type
     self.article_repository = ArticleRepository()