Esempio n. 1
0
def load_form_create(json, company_id=None, material_id=None, publication_id=None):
    action = g.req('action', allowed=['load', 'validate', 'save'])

    def portal_division_dict(article, tags=None):
        if (not hasattr(article, 'portal_division_id')) or (article.portal_division_id is None):
            return {'positioned_articles': []}
        else:
            filter = article.position_unique_filter()
            return {'positioned_articles':
                        [pda.get_client_side_dict(fields='id|position|title') for pda in
                         db(ArticlePortalDivision).filter(filter).
                             order_by(expression.desc(ArticlePortalDivision.position)).all()],
                    'availableTags': tags
                    }

    available_tag_names = None

    if company_id:  # creating material version
        articleVersion = ArticleCompany(company_id=company_id, editor=g.user, article=Article(author_user_id=g.user.id))
    elif material_id:  # companys version. always updating existing
        articleVersion = ArticleCompany.get(material_id)
    elif publication_id:  # updating portal version
        articleVersion = ArticlePortalDivision.get(publication_id)
        portal_division_id = articleVersion.portal_division_id
        article_tag_names = articleVersion.tags
        available_tags = PortalDivision.get(portal_division_id).portal_division_tags
        available_tag_names = list(map(lambda x: getattr(x, 'name', ''), available_tags))

    if action == 'load':
        article_dict = articleVersion.get_client_side_dict(more_fields='long|company')
        article_dict['image'] = articleVersion.get_image_client_side_dict()
        if publication_id:
            article_dict = dict(list(article_dict.items()) + [('tags', article_tag_names)])
        return {'article': article_dict,
                'portal_division': portal_division_dict(articleVersion, available_tag_names)}
    else:
        parameters = g.filter_json(json, 'article.title|subtitle|short|long|keywords')
        articleVersion.attr(parameters['article'])
        if action == 'validate':
            articleVersion.detach()
            return articleVersion.validate(articleVersion.id is not None)
        else:
            if type(articleVersion) == ArticlePortalDivision:
                tag_names = json['article']['tags']
                articleVersion.manage_article_tags(tag_names)
            article_dict = articleVersion.set_image_client_side_dict(
                json['article']['image']).save().get_client_side_dict(more_fields='long|company')
            if publication_id:
                articleVersion.insert_after(json['portal_division']['insert_after'],
                                            articleVersion.position_unique_filter())
            article_dict['image'] = articleVersion.get_image_client_side_dict()
            return {'article': article_dict,
                    'portal_division': portal_division_dict(articleVersion)}
Esempio n. 2
0
def load_form_create(json,
                     company_id=None,
                     material_id=None,
                     publication_id=None):
    action = g.req('action', allowed=['load', 'validate', 'save'])

    def portal_division_dict(article, tags=None):
        if (not hasattr(article, 'portal_division_id')) or (
                article.portal_division_id is None):
            return {'positioned_articles': []}
        else:
            filter = article.position_unique_filter()
            return {
                'positioned_articles': [
                    pda.get_client_side_dict(fields='id|position|title') for
                    pda in db(ArticlePortalDivision).filter(filter).order_by(
                        expression.desc(ArticlePortalDivision.position)).all()
                ],
                'availableTags':
                tags
            }

    available_tag_names = None

    if company_id:  # creating material version
        articleVersion = ArticleCompany(
            company_id=company_id,
            editor=g.user,
            article=Article(author_user_id=g.user.id))
    elif material_id:  # companys version. always updating existing
        articleVersion = ArticleCompany.get(material_id)
    elif publication_id:  # updating portal version
        articleVersion = ArticlePortalDivision.get(publication_id)
        portal_division_id = articleVersion.portal_division_id
        article_tag_names = articleVersion.tags
        available_tags = PortalDivision.get(
            portal_division_id).portal_division_tags
        available_tag_names = list(
            map(lambda x: getattr(x, 'name', ''), available_tags))

    if action == 'load':
        article_dict = articleVersion.get_client_side_dict(
            more_fields='long|company')
        article_dict['image'] = articleVersion.get_image_client_side_dict()
        if publication_id:
            article_dict = dict(
                list(article_dict.items()) + [('tags', article_tag_names)])
        return {
            'article':
            article_dict,
            'portal_division':
            portal_division_dict(articleVersion, available_tag_names)
        }
    else:
        parameters = g.filter_json(
            json, 'article.title|subtitle|short|long|keywords')
        articleVersion.attr(parameters['article'])
        if action == 'validate':
            articleVersion.detach()
            return articleVersion.validate(articleVersion.id is not None)
        else:
            if type(articleVersion) == ArticlePortalDivision:
                tag_names = json['article']['tags']
                articleVersion.manage_article_tags(tag_names)
            article_dict = articleVersion.set_image_client_side_dict(
                json['article']['image']).save().get_client_side_dict(
                    more_fields='long|company')
            if publication_id:
                articleVersion.insert_after(
                    json['portal_division']['insert_after'],
                    articleVersion.position_unique_filter())
            article_dict['image'] = articleVersion.get_image_client_side_dict()
            return {
                'article': article_dict,
                'portal_division': portal_division_dict(articleVersion)
            }
Esempio n. 3
0
def load_form_create(json, article_company_id=None, mine_version_article_company_id=None,
                     article_portal_division_id=None):
    action = g.req('action', allowed=['load', 'validate', 'save'])

    def portal_division_dict(article, tags=None):
        if (not hasattr(article, 'portal_division_id')) or (article.portal_division_id is None):
            return {'positioned_articles': []}
        else:
            filter = article.position_unique_filter()
            return {'positioned_articles':
                        [pda.get_client_side_dict(fields='id|position|title') for pda in
                         db(ArticlePortalDivision).filter(filter).
                             order_by(expression.desc(ArticlePortalDivision.position)).all()],
                    'availableTags': tags
                    }

    available_tag_names = None

    if article_company_id:  # companys version. always updating existing
        articleVersion = ArticleCompany.get(article_company_id)
    elif mine_version_article_company_id:  # updating personal version
        articleVersion = ArticleCompany.get(mine_version_article_company_id)
    elif article_portal_division_id:  # updating portal version
        articleVersion = ArticlePortalDivision.get(article_portal_division_id)
        portal_division_id = articleVersion.portal_division_id

        article_tag_names = articleVersion.tags
        available_tags = PortalDivision.get(portal_division_id).portal_division_tags
        available_tag_names = list(map(lambda x: getattr(x, 'name', ''), available_tags))

    else:  # creating personal version
        articleVersion = ArticleCompany(editor=g.user, article=Article(author_user_id=g.user.id))

    if action == 'load':
        article_dict = articleVersion.get_client_side_dict(more_fields='long|company')

        if article_portal_division_id:
            article_dict = dict(list(article_dict.items()) + [('tags', article_tag_names)])

        image_dict = {'ratio': Config.IMAGE_EDITOR_RATIO, 'coordinates': None,
                      'image_file_id': article_dict['image_file_id'],
                      'no_image_url': g.fileUrl(FOLDER_AND_FILE.no_article_image())
                      }
        # article_dict['long'] = '<table><tr><td><em>cell</em> 1</td><td><strong>cell<strong> 2</td></tr></table>'
        # TODO: VK by OZ: this code should be moved to model
        try:
            if article_dict.get('image_file_id'):
                image_dict['image_file_id'], image_dict['coordinates'] = ImageCroped. \
                    get_coordinates_and_original_img(article_dict.get('image_file_id'))
            else:
                image_dict['image_file_id'] = None
        except Exception as e:
            pass

        return {'article': article_dict,
                'image': image_dict,
                'portal_division': portal_division_dict(articleVersion, available_tag_names)}
    else:
        parameters = g.filter_json(json, 'article.title|short|long|keywords, image.*')

        articleVersion.attr(parameters['article'])
        if action == 'validate':
            articleVersion.detach()
            return articleVersion.validate(article_company_id is None)
        else:
            image_id = parameters['image'].get('image_file_id')
            # TODO: VK by OZ: this code dont work if ArticlePortalDivision updated
            if image_id:
                articleVersion.image_file_id = crop_image(image_id, parameters['image']['coordinates'])
            else:
                articleVersion.image_file_id = None

            if type(articleVersion) == ArticlePortalDivision:
                tag_names = json['article']['tags']
                articleVersion.manage_article_tags(tag_names)

            a = articleVersion.save()
            if article_portal_division_id:
                articleVersion.insert_after(json['portal_division']['insert_after'], articleVersion.position_unique_filter())
            return {'article': articleVersion.save().get_client_side_dict(more_fields='long'), 'image': json['image'],
                    'portal_division': portal_division_dict(articleVersion)}
Esempio n. 4
0
def load_form_create(json,
                     company_id=None,
                     material_id=None,
                     publication_id=None):
    action = g.req('action', allowed=['load', 'validate', 'save'])

    def portal_division_dict(article, tags=None):
        if (not hasattr(article, 'portal_division_id')) or (
                article.portal_division_id is None):
            return {'positioned_articles': []}
        else:
            filter = article.position_unique_filter()
            return {
                'positioned_articles': [
                    pda.get_client_side_dict(fields='id|position|title') for
                    pda in db(ArticlePortalDivision).filter(filter).order_by(
                        expression.desc(ArticlePortalDivision.position)).all()
                ],
                'availableTags':
                tags
            }

    available_tag_names = None

    if company_id:  # creating material version
        articleVersion = ArticleCompany(
            company_id=company_id,
            editor=g.user,
            article=Article(author_user_id=g.user.id))
    elif material_id:  # companys version. always updating existing
        articleVersion = ArticleCompany.get(material_id)
    elif publication_id:  # updating portal version
        articleVersion = ArticlePortalDivision.get(publication_id)
        portal_division_id = articleVersion.portal_division_id

        article_tag_names = articleVersion.tags
        available_tags = PortalDivision.get(
            portal_division_id).portal_division_tags
        available_tag_names = list(
            map(lambda x: getattr(x, 'name', ''), available_tags))

    if action == 'load':
        article_dict = articleVersion.get_client_side_dict(
            more_fields='long|company')

        if publication_id:
            article_dict = dict(
                list(article_dict.items()) + [('tags', article_tag_names)])

        image_dict = {
            'ratio': Config.IMAGE_EDITOR_RATIO,
            'coordinates': None,
            'image_file_id': article_dict['image_file_id'],
            'no_image_url': g.fileUrl(FOLDER_AND_FILE.no_article_image())
        }
        # article_dict['long'] = '<table><tr><td><em>cell</em> 1</td><td><strong>cell<strong> 2</td></tr></table>'
        # TODO: VK by OZ: this code should be moved to model
        try:
            if article_dict.get('image_file_id'):
                image_dict['image_file_id'], image_dict['coordinates'] = ImageCroped. \
                    get_coordinates_and_original_img(article_dict.get('image_file_id'))
            else:
                image_dict['image_file_id'] = None
        except Exception as e:
            pass

        return {
            'article':
            article_dict,
            'image':
            image_dict,
            'portal_division':
            portal_division_dict(articleVersion, available_tag_names)
        }
    else:
        parameters = g.filter_json(
            json, 'article.title|subtitle|short|long|keywords, image.*')
        articleVersion.attr(parameters['article'])

        if action == 'validate':
            articleVersion.detach()
            return articleVersion.validate(articleVersion.id is not None)
        else:
            image_id = parameters['image'].get('image_file_id')
            # TODO: VK by OZ: this code dont work if ArticlePortalDivision updated
            if image_id:
                articleVersion.image_file_id = crop_image(
                    image_id, parameters['image']['coordinates'])
            else:
                articleVersion.image_file_id = None

            if type(articleVersion) == ArticlePortalDivision:
                tag_names = json['article']['tags']
                articleVersion.manage_article_tags(tag_names)

            articleVersion.save()
            if publication_id:
                articleVersion.insert_after(
                    json['portal_division']['insert_after'],
                    articleVersion.position_unique_filter())
            return {
                'article':
                articleVersion.save().get_client_side_dict(more_fields='long'),
                'image':
                json['image'],
                'portal_division':
                portal_division_dict(articleVersion)
            }
Esempio n. 5
0
def load_form_create(json, company_id=None, material_id=None, publication_id=None):
    action = g.req("action", allowed=["load", "validate", "save"])

    def portal_division_dict(article, tags=None):
        if (not hasattr(article, "portal_division_id")) or (article.portal_division_id is None):
            return {"positioned_articles": []}
        else:
            filter = article.position_unique_filter()
            return {
                "positioned_articles": [
                    pda.get_client_side_dict(fields="id|position|title")
                    for pda in db(ArticlePortalDivision)
                    .filter(filter)
                    .order_by(expression.desc(ArticlePortalDivision.position))
                    .all()
                ],
                "availableTags": tags,
            }

    available_tag_names = None

    if company_id:  # creating material version
        articleVersion = ArticleCompany(company_id=company_id, editor=g.user, article=Article(author_user_id=g.user.id))
    elif material_id:  # companys version. always updating existing
        articleVersion = ArticleCompany.get(material_id)
    elif publication_id:  # updating portal version
        articleVersion = ArticlePortalDivision.get(publication_id)
        portal_division_id = articleVersion.portal_division_id

        article_tag_names = articleVersion.tags
        available_tags = PortalDivision.get(portal_division_id).portal_division_tags
        available_tag_names = list(map(lambda x: getattr(x, "name", ""), available_tags))

    if action == "load":
        article_dict = articleVersion.get_client_side_dict(more_fields="long|company")

        if publication_id:
            article_dict = dict(list(article_dict.items()) + [("tags", article_tag_names)])

        image_dict = {
            "ratio": Config.IMAGE_EDITOR_RATIO,
            "coordinates": None,
            "image_file_id": article_dict["image_file_id"],
            "no_image_url": g.fileUrl(FOLDER_AND_FILE.no_article_image()),
        }
        # article_dict['long'] = '<table><tr><td><em>cell</em> 1</td><td><strong>cell<strong> 2</td></tr></table>'
        # TODO: VK by OZ: this code should be moved to model
        try:
            if article_dict.get("image_file_id"):
                image_dict["image_file_id"], image_dict["coordinates"] = ImageCroped.get_coordinates_and_original_img(
                    article_dict.get("image_file_id")
                )
            else:
                image_dict["image_file_id"] = None
        except Exception as e:
            pass

        return {
            "article": article_dict,
            "image": image_dict,
            "portal_division": portal_division_dict(articleVersion, available_tag_names),
        }
    else:
        parameters = g.filter_json(json, "article.title|subtitle|short|long|keywords, image.*")
        articleVersion.attr(parameters["article"])

        if action == "validate":
            articleVersion.detach()
            return articleVersion.validate(articleVersion.id is not None)
        else:
            image_id = parameters["image"].get("image_file_id")
            # TODO: VK by OZ: this code dont work if ArticlePortalDivision updated
            if image_id:
                articleVersion.image_file_id = crop_image(image_id, parameters["image"]["coordinates"])
            else:
                articleVersion.image_file_id = None

            if type(articleVersion) == ArticlePortalDivision:
                tag_names = json["article"]["tags"]
                articleVersion.manage_article_tags(tag_names)

            articleVersion.save()
            if publication_id:
                articleVersion.insert_after(
                    json["portal_division"]["insert_after"], articleVersion.position_unique_filter()
                )
            return {
                "article": articleVersion.save().get_client_side_dict(more_fields="long"),
                "image": json["image"],
                "portal_division": portal_division_dict(articleVersion),
            }