Example #1
0
def load(json, company_id=None):
    user_can_edit = UserCompany.get(
        company_id=company_id
    ).rights['PORTAL_EDIT_PROFILE'] if company_id else None
    action = g.req('action', allowed=['load', 'validate', 'save'])
    company = Company() if company_id is None else Company.get(company_id)
    if action == 'load':
        company_dict = company.get_client_side_dict()
        image_dict = {
            'ratio': Config.IMAGE_EDITOR_RATIO,
            'coordinates': None,
            'image_file_id': company_dict.get('logo_file_id'),
            'no_image_url': g.fileUrl(FOLDER_AND_FILE.no_logo())
        }
        try:
            if company_dict.get('logo_file_id'):
                image_dict['image_file_id'], image_dict['coordinates'] = ImageCroped. \
                    get_coordinates_and_original_img(company_dict.get('logo_file_id'))
            else:
                image_dict['image_file_id'] = None
        except Exception as e:
            pass
        image = {'image': image_dict}
        company_dict.update(image)
        return company_dict
    else:
        company.attr(
            g.filter_json(json, 'about', 'address', 'country', 'email', 'name',
                          'phone', 'phone2', 'region', 'short_description',
                          'lon', 'lat'))
        if action == 'validate':
            if company_id is not None and user_can_edit:
                company.detach()
            return company.validate(company_id is None and user_can_edit)
        else:
            if json['image'].get('uploaded'):
                if company_id is None:
                    company.setup_new_company()
                company.save().get_client_side_dict()
                imgdataContent = json['image']['dataContent']
                image_data = re.sub('^data:image/.+;base64,', '',
                                    imgdataContent)
                bb = base64.b64decode(image_data)
                new_comp = db(Company, id=company.id).first()
                file_id = File.uploadForCompany(bb, json['image']['name'],
                                                json['image']['type'],
                                                new_comp)
                logo_id = crop_image(file_id, json['image']['coordinates'])
                new_comp.updates({'logo_file_id': logo_id})
            else:
                img = json['image']
                img_id = img.get('image_file_id')
                if img_id:
                    company.logo_file_id = crop_image(img_id,
                                                      img['coordinates'])
                elif not img_id:
                    company.logo_file_id = None
                if company_id is None:
                    company.setup_new_company()
                return company.save().get_client_side_dict()
Example #2
0
def load(json, company_id=None):
    user_can_edit = UserCompany.get(company_id=company_id).rights['PORTAL_EDIT_PROFILE'] if company_id else None
    action = g.req('action', allowed=['load', 'validate', 'save'])
    company = Company() if company_id is None else Company.get(company_id)
    if action == 'load':
        company_dict = company.get_client_side_dict()
        image_dict = {'ratio': Config.IMAGE_EDITOR_RATIO, 'coordinates': None,
                      'image_file_id': company_dict.get('logo_file_id'),
                      'no_image_url': g.fileUrl(FOLDER_AND_FILE.no_logo())
                      }
        try:
            if company_dict.get('logo_file_id'):
                image_dict['image_file_id'], image_dict['coordinates'] = ImageCroped. \
                    get_coordinates_and_original_img(company_dict.get('logo_file_id'))
            else:
                image_dict['image_file_id'] = None
        except Exception as e:
            pass
        image = {'image': image_dict}
        company_dict.update(image)
        return company_dict
    else:
        company.attr(g.filter_json(json, 'about', 'address', 'country', 'email', 'name', 'phone',
                                   'phone2', 'region', 'short_description', 'lon', 'lat'))
        if action == 'validate':
            if company_id is not None and user_can_edit:
                company.detach()
            return company.validate(company_id is None and user_can_edit)
        else:
            if json['image'].get('uploaded'):
                if company_id is None:
                    company.setup_new_company()
                company.save().get_client_side_dict()
                imgdataContent = json['image']['dataContent']
                image_data = re.sub('^data:image/.+;base64,', '', imgdataContent)
                bb = base64.b64decode(image_data)
                new_comp = db(Company, id=company.id).first()
                file_id = File.uploadForCompany(bb, json['image']['name'], json['image']['type'], new_comp)
                logo_id = crop_image(file_id, json['image']['coordinates'])
                new_comp.updates({'logo_file_id': logo_id})
            else:
                img = json['image']
                img_id = img.get('image_file_id')
                if img_id:
                    company.logo_file_id = crop_image(img_id, img['coordinates'])
                elif not img_id:
                    company.logo_file_id = None
                if company_id is None:
                    company.setup_new_company()
                return company.save().get_client_side_dict()
Example #3
0
def load(json, company_id=None):
    action = g.req('action', allowed=['load', 'validate', 'save'])
    company = Company() if company_id is None else Company.get(company_id)
    if action == 'load':
        company_dict = company.get_client_side_dict()
        image_dict = {'ratio': Config.IMAGE_EDITOR_RATIO, 'coordinates': None,
                      'image_file_id': company_dict.get('logo_file_id'),
                      'no_image_url': g.fileUrl(FOLDER_AND_FILE.no_logo())
                      }
        try:
            if company_dict.get('logo_file_id'):
                image_dict['image_file_id'], image_dict['coordinates'] = ImageCroped. \
                    get_coordinates_and_original_img(company_dict.get('logo_file_id'))
            else:
                image_dict['image_file_id'] = None
        except Exception as e:
            pass
        image = {'image': image_dict}
        company_dict.update(image)
        return company_dict
    else:
        company.attr(g.filter_json(json, 'about', 'address', 'country', 'email', 'name', 'phone',
                                   'phone2', 'region', 'short_description', 'lon', 'lat'))

        if action == 'validate':
            if company_id is not None:
                company.detach()
            return company.validate(company_id is None)
        else:
            img = json['image']
            img_id = img.get('image_file_id')
            if img_id:
                company.logo_file_id = crop_image(img_id, img['coordinates'])
            elif not img_id:
                company.logo_file_id = None

            if company_id is None:
                company.setup_new_company()
            return company.save().get_client_side_dict()
Example #4
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)}
Example #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)
            }
Example #6
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),
            }