Esempio n. 1
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'))
Esempio n. 2
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'))
Esempio n. 3
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)
Esempio n. 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)