Example #1
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)
Example #2
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)
def remove_photo(uid, photo_date):
    filename = photo_filename(uid, photo_date)
    old_paths = [os.path.join(_photo_path(), filename)]
    old_paths.extend([os.path.join(_thumbnail_path(format), filename) for format in _ThumbnailsFormats.keys()])
    tools.remove_silently(*old_paths)