def set_after_logination_params(): back_to = g.req('back_to') if back_to: session['back_to'] = back_to portal_id = g.req('portal_id') if portal_id: session['portal_id'] = portal_id
def profile_load_validate_save(json, company_id=None): # if not user_can_edit: # raise Exception('no PORTAL_EDIT_PROFILE') 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() # company_dict['logo'] = company.get_logo_client_side_dict() user_company = UserCompany.get(company_id=company_id) if user_company: company_dict['actions'] = {'edit_company_profile': EditCompanyRight(company=company).is_allowed(), 'edit_portal_profile': EditPortalRight(company=company_id).is_allowed()} return company_dict else: company.attr(g.filter_json(json, 'about', 'address', 'country', 'email', 'name', 'phone', 'city', 'postcode', '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: if company_id and EditCompanyRight(company=company_id).is_allowed() != True: return abort(403) if company_id is None: company.setup_new_company().save() company.logo = json['logo'] return utils.dict_merge(company.save().get_client_side_dict(), actions={'edit': True if company_id else False})
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()
def edit_profile_load(json, user_id): action = g.req('action', allowed=['load', 'validate', 'save']) if action == 'load': ret = {'user': g.user.get_client_side_dict(), 'languages': Config.LANGUAGES, 'countries': Country.get_countries()} ret['user']['avatar'] = g.user.avatar return ret else: user_data = utils.filter_json(json['user'], 'first_name, last_name, birth_tm, lang, country_id, location, gender, address_url, address_phone, password, password_confirmation') user_data['country_id'] = user_data['country_id'] if user_data[ 'country_id'] else '56f52e6b-1273-4001-b15d-d5471ebfc075' user_data['full_name'] = user_data['first_name'] + ' ' + user_data['last_name'] user_data['birth_tm'] = user_data['birth_tm'] if user_data['birth_tm'] else None avatar = json['user']['avatar'] g.user.attr(user_data) if g.user.password == '': g.user.password = None if action == 'validate': g.user.detach() validate = g.user.validate(False) return validate else: g.user.avatar = avatar g.user.set_password_hash() ret = {'user': g.user.save().get_client_side_dict()} return ret
def tags_load(json, company_id): action = g.req('action', allowed=['load', 'save', 'validate']) company = Company.get(company_id) portal = company.own_portal def get_client_model(aportal): portal_dict = aportal.get_client_side_dict() portal_dict['divisions'] = [division for division in portal_dict['divisions'] if division['portal_division_type_id'] in ['news', 'events', 'catalog']] ret = {'company': company.get_client_side_dict(), 'portal': portal_dict} for division in ret['portal']['divisions']: division['tags'] = {tagdict['id']: True for tagdict in division['tags']} return ret if action == 'load': return get_client_model(portal) else: new_tags = {t['id']: {'text': t.get('text', ''), 'description': t.get('description', '')} for t in json['portal']['tags']} validated = portal.validate_tags(new_tags) if action == 'save': if validated['errors']: raise ValueError portal.set_tags(new_tags, json['portal']['divisions']).save() return get_client_model(portal) else: return validated
def profile_load_validate_save(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() user_company = UserCompany.get_by_user_and_company_ids(company_id=company_id) if user_company: company_dict['actions'] = { 'edit_company_profile': EmployeeHasRightAtCompany(RIGHT_AT_COMPANY.COMPANY_EDIT_PROFILE).check( company_id=company.id), 'edit_portal_profile': EmployeeHasRightAtCompany(RIGHT_AT_COMPANY.PORTAL_EDIT_PROFILE).check( company_id=company.id) } return company_dict else: company.attr( utils.filter_json(json, 'about', 'address', 'country', 'email', 'web', 'name', 'phone', 'city', 'postcode', '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: if company_id is None: company.setup_new_company().save() company.logo = json['logo'] return utils.dict_merge(company.save().get_client_side_dict(), actions={'edit': True if company_id else False})
def publication_delete_unpublish(json): action = g.req('action', allowed=['delete', 'unpublish', 'undelete']) publication = ArticlePortalDivision.get(json['publication_id']) publication.status = ArticlePortalDivision.STATUSES['DELETED' if action == 'delete' else 'UNPUBLISHED'] return get_publication_dict(publication.save())
def tags_load(json, company_id): action = g.req('action', allowed=['load', 'save', 'validate']) company = Company.get(company_id) portal = company.own_portal def get_client_model(aportal): portal_dict = aportal.get_client_side_dict() portal_dict['divisions'] = [division for division in portal_dict['divisions'] if division['portal_division_type_id'] == 'news' or division[ 'portal_division_type_id'] == 'events'] ret = {'company': company.get_client_side_dict(), 'portal': portal_dict} for division in ret['portal']['divisions']: division['tags'] = {tagdict['id']: True for tagdict in division['tags']} return ret if action == 'load': return get_client_model(portal) else: new_tags = {t['id']: {'text': t.get('text', ''), 'description': t.get('description', '')} for t in json['portal']['tags']} validated = portal.validate_tags(new_tags) if action == 'save': if validated['errors']: raise ValueError portal.set_tags(new_tags, json['portal']['divisions']).save() return get_client_model(portal) else: return validated
def load_form_update(json, article_company_id): action = g.req('action', allowed=['load', 'save', 'validate']) article = ArticleCompany.get(article_company_id) if action == 'load': article = article.get_client_side_dict() article.update(ratio=Config.IMAGE_EDITOR_RATIO) image_id = article.get('image_file_id') if image_id: try: article['image_file_id'], coordinates = ImageCroped. \ get_coordinates_and_original_img(image_id) article.update(coordinates) except NoResultFound: pass return article else: article.attr({key: val for key, val in json.items() if key in ['keywords', 'title', 'short', 'long']}) if action == 'save': image_id = json.get('image_file_id') coordinates = json.get('coordinates') if image_id: if db(ImageCroped, original_image_id=image_id).count(): update_croped_image(image_id, coordinates) else: article.image_file_id = crop_image(image_id, coordinates) article.save() article = article.get_client_side_dict() # print(article['image_file_id']) return article else: # return {'errors': {}, 'warnings': {}, 'notices': {}} article.detach() return article.validate('update')
def submit_or_publish_material(json, material_id): action = g.req('action', allowed=['load', 'validate', 'save']) publication = Publication(material=Material.get(material_id)) publisher_membership = MemberCompanyPortal.get(json['publisher_membership']['id']) employment = UserCompany.get_by_user_and_company_ids(company_id=publication.material.company_id) also_publish = json.get('also_publish', None) if action == 'load': return utils.dict_merge( publish_dialog_load(publication, publisher_membership), {'can_material_also_be_published': employment.rights[RIGHT_AT_COMPANY.ARTICLES_SUBMIT_OR_PUBLISH] and \ publisher_membership.rights[RIGHT_AT_PORTAL.PUBLICATION_PUBLISH] }) else: publish_dialog_save( publication, json['publication'], status=Publication.STATUSES['PUBLISHED'] if also_publish else Publication.STATUSES['SUBMITTED']) if action == 'validate': publication.detach() return publication.validate(True) else: publication.save() utils.db.execute_function("tag_publication_set_position('%s', ARRAY ['%s']);" % (publication.id, "', '".join([t.id for t in publication.tags]))) publisher_membership.NOTIFY_MATERIAL_ACTION_BY_COMPANY_OR_PORTAL( action='PUBLISH' if also_publish else 'SUBMIT', company_or_portal='company', material_title=publication.material.title) return publisher_membership.material_or_publication_grid_row(publication.material)
def edit_material_load(json_data, company_id=None, material_id=None): action = g.req('action', allowed=['load', 'validate', 'save']) company = Company.get(company_id) if material_id: material = Material.get(material_id) else: material = Material(company=company, company_id=company_id, editor=g.user, source_type ='profireader', source_id = '') if action == 'load': # return {'material': material.get_client_side_dict(fields='id,cr_tm,md_tm,external_url,company_id,illustration.url,title,subtitle,author,short,long,keywords')} # return {'material': material.get_client_side_dict(fields='id,cr_tm,md_tm,external_url,company_id,illustration.url,title,subtitle,author,short,long,keywords,company.id|name')} return {'material': material.get_client_side_dict(more_fields='long|company|illustration,image_galleries.items,image_galleries.id')} else: parameters = utils.filter_json(json_data, 'material.title|subtitle|short|long|keywords|author|external_url') material.attr(parameters['material']) if action == 'validate': material.detach() return material.validate(material.id is not None) else: (material.long, material.image_galleries) = MaterialImageGallery.check_html( material.long, json_data['material']['image_galleries'], material.image_galleries, company) material.illustration = json_data['material']['illustration'] material.save() return {'material': material.get_client_side_dict(more_fields='long|company|illustration')}
def publish(json, publication_id, actor_membership_id, request_from): action = g.req('action', allowed=['load', 'validate', 'save']) publication = Publication.get(publication_id) actor_membership = MemberCompanyPortal.get(actor_membership_id) publisher_membership = MemberCompanyPortal.get_by_portal_id_company_id( publication.portal_division.portal_id, publication.material.company_id) if action == 'load': return publish_dialog_load(publication, publisher_membership) else: publish_dialog_save(publication, json['publication'], status=Publication.STATUSES['PUBLISHED']) if action == 'validate': publication.detach() return publication.validate(True) else: publication.save() utils.db.execute_function("tag_publication_set_position('%s', ARRAY ['%s']);" % (publication.id, "', '".join([t.id for t in publication.tags]))) actor_membership.NOTIFY_MATERIAL_ACTION_BY_COMPANY_OR_PORTAL( action='PUBLISH', material_title=publication.material.title, company_or_portal='company' if request_from == 'company_material_details' else 'portal') if request_from == 'company_material_details': return actor_membership.material_or_publication_grid_row(publication.material) elif request_from == 'portal_publications': return publication.portal_publication_grid_row(actor_membership)
def profile_load_validate_save(json, company_id=None): user_can_edit = UserCompany.get( company_id=company_id ).rights['PORTAL_EDIT_PROFILE'] if company_id else None # if not user_can_edit: # raise Exception('no PORTAL_EDIT_PROFILE') 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() company_dict['logo'] = company.get_logo_client_side_dict() company_dict['actions'] = { 'edit': True if company_id and UserCompany.get( company_id=company_id).rights['PORTAL_EDIT_PROFILE'] else False } return company_dict else: company.attr( g.filter_json(json, 'about', 'address', 'country', 'email', 'name', 'phone', 'city', 'postcode', '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: if company_id is None: company.setup_new_company() company_dict = company.set_logo_client_side_dict( json['logo']).save().get_client_side_dict() company_dict['logo'] = company.get_logo_client_side_dict() company_dict['actions'] = {'edit': True if company_id else False} return company_dict
def edit_profile_load(json, user_id): action = g.req('action', allowed=['load', 'validate', 'save']) if action == 'load': ret = { 'user': g.user.get_client_side_dict(), 'languages': Config.LANGUAGES, 'countries': Country.get_countries(), 'change_password': { 'password1': '', 'password2': '' } } ret['user']['avatar'] = g.user.get_avatar_client_side_dict() return ret else: g.user.updates(json['user']) if action == 'validate': g.user.detach() validate = g.user.validate(False) if json['change_password']['password1'] != json['change_password'][ 'password2']: validate['errors'][ 'password2'] = 'pls enter the same passwords' return validate else: if json['change_password']['password1']: g.user.password = json['change_password']['password1'] g.user.set_avatar_client_side_dict(json['user']['avatar']).save() ret = {'user': g.user.get_client_side_dict()} ret['user']['avatar'] = g.user.get_avatar_client_side_dict() return ret
def profile_load_validate_save(json, company_id=None): user_can_edit = UserCompany.get(company_id=company_id).rights['PORTAL_EDIT_PROFILE'] if company_id else None # if not user_can_edit: # raise Exception('no PORTAL_EDIT_PROFILE') 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() company_dict['logo'] = company.get_logo_client_side_dict() company_dict['actions'] = {'edit': True if company_id and UserCompany.get( company_id=company_id).rights['PORTAL_EDIT_PROFILE'] else False} return company_dict else: company.attr(g.filter_json(json, 'about', 'address', 'country', 'email', 'name', 'phone', 'city', 'postcode', '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: if company_id is None: company.setup_new_company() company_dict = company.set_logo_client_side_dict(json['logo']).save().get_client_side_dict() company_dict['logo'] = company.get_logo_client_side_dict() company_dict['actions'] = {'edit': True if company_id else False} return company_dict
def create_save(json, create_or_update, company_id): action = g.req('action', allowed=['load', 'save', 'validate']) layouts = [x.get_client_side_dict() for x in db(PortalLayout).all()] types = {x.id: x.get_client_side_dict() for x in PortalDivisionType.get_division_types()} company = Company.get(company_id) member_companies = {company_id: company.get_client_side_dict()} company_logo = company.logo_file_relationship.url() if company.logo_file_id else '/static/images/company_no_logo.png' if action == 'load': ret = {'company_id': company_id, 'company_logo': company_logo, 'portal_company_members': member_companies, 'portal': {'company_owner_id': company_id, 'name': '', 'host': '', 'logo_file_id': company.logo_file_id, 'portal_layout_id': layouts[0]['id'], 'divisions': [ {'name': 'index page', 'portal_division_type_id': 'index', 'page_size': ''}, {'name': 'news', 'portal_division_type_id': 'news', 'page_size': ''}, {'name': 'events', 'portal_division_type_id': 'events', 'page_size': ''}, {'name': 'catalog', 'portal_division_type_id': 'catalog', 'page_size': ''}, {'name': 'our subportal', 'portal_division_type_id': 'company_subportal', 'page_size': '', 'settings': {'company_id': company_id}}]}, 'layouts': layouts, 'division_types': types} if create_or_update == 'update': pass # ret['portal'] = return ret else: json_portal = json['portal'] if create_or_update == 'update': pass elif create_or_update == 'create': page_size_for_config = dict() for a in json_portal['divisions']: page_size_for_config[a.get('name')] = a.get('page_size') \ if type(a.get('page_size')) is int and a.get('page_size') != 0 \ else Config.ITEMS_PER_PAGE portal = Portal(company_owner=company, **g.filter_json(json_portal, 'name', 'portal_layout_id', 'host')) divisions = [] for division_json in json['portal']['divisions']: division = PortalDivision(portal, portal_division_type_id=division_json['portal_division_type_id'], position=len(json['portal']['divisions']) - len(divisions), name=division_json['name']) if division_json['portal_division_type_id'] == 'company_subportal': division.settings = {'member_company_portal': portal.company_members[0]} divisions.append(division) # self, portal=portal, portal_division_type=portal_division_type, name='', settings={} portal.divisions = divisions PortalConfig(portal=portal, page_size_for_divisions=page_size_for_config) if action == 'save': return portal.setup_created_portal(g.filter_json(json_portal, 'logo_file_id')).save().get_client_side_dict() else: return portal.validate(create_or_update == 'insert')
def check(self, json, *args, **kwargs): action = g.req('action', allowed=['load', 'validate', 'save']) right = getattr(self, action + '_right', None) if right is True or right is False: return right EmployeeHasRightAtCompany(right).check(*args, **kwargs) return True
def submit_publish(json, article_action): action = g.req('action', allowed=['load', 'validate', 'save']) company = Company.get(json['company']['id']) if article_action == 'SUBMIT': material = ArticleCompany.get(json['material']['id']) publication = ArticlePortalDivision(title=material.title, subtitle=material.subtitle, keywords=material.keywords, short=material.short, long=material.long, article_company_id=material.id) more_data_to_ret = { 'material': {'id': material.id}, 'can_material_also_be_published': MemberCompanyPortal.get(portal_id=json['portal']['id'], company_id=json['company']['id']) .has_rights(MemberCompanyPortal.RIGHT_AT_PORTAL.PUBLICATION_PUBLISH) } else: publication = ArticlePortalDivision.get(json['publication']['id']) more_data_to_ret = {} if action == 'load': ret = { 'publication': publication.get_client_side_dict(), 'company': company.get_client_side_dict(), 'portal': Portal.get(json['portal']['id']).get_client_side_dict() } ret['portal']['divisions'] = PRBase.get_ordered_dict([d for d in ret['portal']['divisions'] if (d['portal_division_type_id'] in ['events', 'news'])]) return PRBase.merge_dicts(ret, more_data_to_ret) else: publication.attr(g.filter_json(json['publication'], 'portal_division_id')) publication.publishing_tm = PRBase.parse_timestamp(json['publication'].get('publishing_tm')) publication.event_tm = PRBase.parse_timestamp(json['publication'].get('event_tm')) if 'also_publish' in json and json['also_publish']: publication.status = ArticlePortalDivision.STATUSES['PUBLISHED'] else: if article_action in [ArticlePortalDivision.ACTIONS['PUBLISH'], ArticlePortalDivision.ACTIONS['REPUBLISH']]: publication.status = ArticlePortalDivision.STATUSES['PUBLISHED'] elif article_action in [ArticlePortalDivision.ACTIONS['UNPUBLISH'], ArticlePortalDivision.ACTIONS[ 'UNDELETE']]: publication.status = ArticlePortalDivision.STATUSES['UNPUBLISHED'] elif article_action in [ArticlePortalDivision.ACTIONS['DELETE']]: publication.status = ArticlePortalDivision.STATUSES['DELETED'] if action == 'validate': publication.detach() return publication.validate(True if article_action == 'SUBMIT' else False) else: if article_action == 'SUBMIT': publication.long = material.clone_for_portal_images_and_replace_urls(publication.portal_division_id, publication) publication.save() return get_portal_dict_for_material(publication.portal, company, publication=publication)
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)}
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()
def membership_set_tags(json, company_id, portal_id): membership = MemberCompanyPortal.get(portal_id=portal_id, company_id=company_id) action = g.req('action', allowed=['load', 'validate', 'save']) if action == 'load': return membership.get_client_side_dict(fields='id,portal,portal.tags,company,tags') else: membership.tags = [Tag.get(t['id']) for t in json['tags']] if action == 'validate': membership.detach() return PRBase.DEFAULT_VALIDATION_ANSWER() else: membership.save().set_tags_positions() return {'membership': membership_grid_row(membership)}
def set_membership_plan(json, membership_id): action = g.req('action', allowed=['load', 'save', 'validate']) membership = MemberCompanyPortal.get(membership_id) if action == 'load': return membership.get_client_side_dict_for_plan() else: immediately = True if json['membership']['request_membership_plan_issued_immediately'] else False requested_plan_id = json.get('selected_by_user_plan_id', None) if action == 'validate': ret = PRBase.DEFAULT_VALIDATION_ANSWER() if not requested_plan_id: ret['errors']['general'] = 'pls select membership plan' return ret else: return membership.set_new_plan_issued(requested_plan_id, immediately).company_member_grid_row()
def company_update_load(json, employeer_id, member_id): action = g.req('action', allowed=['load', 'validate', 'save']) member = MemberCompanyPortal.get(Company.get(employeer_id).own_portal.id, member_id) if action == 'load': return {'member': member.get_client_side_dict(more_fields='company'), 'statuses_available': MemberCompanyPortal.get_avaliable_statuses(), 'employeer': Company.get(employeer_id).get_client_side_dict()} else: member.set_client_side_dict(status=json['member']['status'], rights=json['member']['rights']) if action == 'validate': member.detach() return member.validate(False) else: member.save() return member.get_client_side_dict()
def membership_set_tags(json, company_id, portal_id): membership = MemberCompanyPortal.get_by_portal_id_company_id(portal_id=portal_id, company_id=company_id) action = g.req('action', allowed=['load', 'validate', 'save']) if action == 'load': # catalog_division = g.db.query(PortalDivision).filter(and_(PortalDivision.portal_id == portal_id, # PortalDivision.portal_division_type_id == 'catalog')).first() return membership.get_client_side_dict(fields='id,portal,company,tags') else: membership.tags = [Tag.get(t['id']) for t in json['tags']] if action == 'validate': membership.detach() return PRBase.DEFAULT_VALIDATION_ANSWER() else: membership.save().set_tags_positions() return {'membership': membership_grid_row(membership)}
def set_membership_plan(json, membership_id): action = g.req('action', allowed=['load', 'save', 'validate']) membership = MemberCompanyPortal.get(membership_id) if action == 'load': return membership.get_client_side_dict_for_plan() else: immediately = True if json['membership'][ 'request_membership_plan_issued_immediately'] else False requested_plan_id = json.get('selected_by_user_plan_id', None) if action == 'validate': ret = PRBase.DEFAULT_VALIDATION_ANSWER() if not requested_plan_id: ret['errors']['general'] = 'pls select membership plan' return ret else: return membership.set_new_plan_issued( requested_plan_id, immediately).company_member_grid_row()
def load_form_create(json): action = g.req('action', allowed=['load', 'validate', 'save']) if action == 'load': return {'id': '', 'title': '', 'short': '', 'long': '', 'coordinates': '', 'ratio': Config.IMAGE_EDITOR_RATIO} if action == 'validate': del json['coordinates'], json['ratio'] return Article.save_new_article(g.user_dict['id'], **g.filter_json(json, 'title,short,long,keywords')).mine_version.validate('insert') else: image_id = json.get('image_file_id') if image_id: json['image_file_id'] = crop_image(image_id, json.get('coordinates')) del json['coordinates'], json['ratio'] article = Article.save_new_article(g.user_dict['id'], **json) g.db.add(article) return article.get_client_side_dict()
def edit_material_load(json_data, company_id=None, material_id=None): action = g.req('action', allowed=['load', 'validate', 'save']) company = Company.get(company_id) if material_id: material = Material.get(material_id) else: material = Material(company=company, company_id=company_id, editor=g.user, source_type='profireader', source_id='') if action == 'load': # return {'material': material.get_client_side_dict(fields='id,cr_tm,md_tm,external_url,company_id,illustration.url,title,subtitle,author,short,long,keywords')} # return {'material': material.get_client_side_dict(fields='id,cr_tm,md_tm,external_url,company_id,illustration.url,title,subtitle,author,short,long,keywords,company.id|name')} return { 'material': material.get_client_side_dict( more_fields= 'long|company|illustration,image_galleries.items,image_galleries.id' ) } else: parameters = utils.filter_json( json_data, 'material.title|subtitle|short|long|keywords|author|external_url') material.attr(parameters['material']) if action == 'validate': material.detach() return material.validate(material.id is not None) else: (material.long, material.image_galleries) = MaterialImageGallery.check_html( material.long, json_data['material']['image_galleries'], material.image_galleries, company) material.illustration = json_data['material']['illustration'] material.save() return { 'material': material.get_client_side_dict( more_fields='long|company|illustration') }
def request_membership_plan(json, membership_id): action = g.req('action', allowed=['load', 'save', 'validate']) membership = MemberCompanyPortal.get(membership_id) if action == 'load': return membership.get_client_side_dict_for_plan() else: immediately = True if json['membership']['request_membership_plan_issued_immediately'] else False requested_plan_id = json.get('selected_by_user_plan_id', None) if action == 'validate': ret = PRBase.DEFAULT_VALIDATION_ANSWER() # if we can apply it immediately and it have to be confirmed by portal company owner if (requested_plan_id is True and not membership.requested_membership_plan_issued.auto_apply) \ or (requested_plan_id is not False and requested_plan_id is not True and not MembershipPlan.get(requested_plan_id).auto_apply and requested_plan_id != membership.portal.default_membership_plan_id): ret['warnings']['general'] = 'plan must be confirmed by company owner before activation' return ret else: return membership.requested_new_plan_issued(requested_plan_id, immediately).portal_memberee_grid_row()
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": return company.get_client_side_dict() else: company.attr( g.filter_json( json, "about", "address", "country", "email", "name", "phone", "phone2", "region", "short_description" ) ) if action == "save": if company_id is None: company.setup_new_company() return company.save().get_client_side_dict() else: if company_id is not None: company.detach() return company.validate("insert" if company_id is None else "update")
def employee_update_load(json, company_id, user_id): action = g.req('action', allowed=['load', 'validate', 'save']) employment = UserCompany.get(user_id=user_id, company_id=company_id) if action == 'load': return {'employment': employment.get_client_side_dict(), 'employee': employment.employee.get_client_side_dict(), 'employer': employment.employer.get_client_side_dict(fields='id|name, logo.url'), # 'statuses_available': UserCompany.get_statuses_avaible(company_id), # 'rights_available': employment.get_rights_avaible() } else: employment.set_client_side_dict(json['employment']) if action == 'validate': employment.detach() return employment.validate(False) else: employment.save() return employment.get_client_side_dict()
def signup(json_data): action = g.req('action', allowed=['validate', 'save']) params = utils.filter_json(json_data, 'first_name,last_name,email,password,password_confirmation') params['address_email'] = params['email'] del params['email'] new_user = User(**params) if action == 'validate': return new_user.validate(True) else: new_user.set_password_hash() new_user.save() g.db.commit() new_user.generate_confirmation_token(get_after_logination_params()).save() Socket.send_greeting([new_user]) g.db.commit() return {}
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': return company.get_client_side_dict() else: company.attr(g.filter_json(json, 'about', 'address', 'country', 'email', 'name', 'phone', 'phone2', 'region', 'short_description')) if json['logo_file_id']: company.logo_file_id = json['logo_file_id'] if action == 'save': if company_id is None: company.setup_new_company() return company.save().get_client_side_dict() else: if company_id is not None: company.detach() return company.validate('insert' if company_id is None else 'update')
def signup(json_data): action = g.req('action', allowed=['validate', 'save']) params = g.filter_json(json_data, 'first_name,last_name,email,password,password_confirmation') params['address_email'] = params['email'] del params['email'] new_user = User(**params) if action == 'validate': return new_user.validate(True) else: # try: if session.get('portal_id'): addtourl = {'subscribe_to_portal': session['portal_id']} session.pop('portal_id') else: addtourl = {} new_user.set_password_hash() new_user.generate_confirmation_token(addtourl).save() g.db.commit() return {}
def company_update_load(json, employeer_id, member_id): action = g.req('action', allowed=['load', 'validate', 'save']) member = MemberCompanyPortal.get(Company.get(employeer_id).own_portal.id, member_id) current_user_right = UserCompany.get(company_id=employeer_id).has_rights(UserCompany.RIGHT_AT_COMPANY.PORTAL_MANAGE_MEMBERS_COMPANIES) if action == 'load': if member.can_update_company_partner(current_user_right) != True: return {'errors': member.can_update_company_partner(current_user_right)} else: return {'member': member.get_client_side_dict(more_fields='company'), 'statuses_available': MemberCompanyPortal.get_avaliable_statuses(), 'employeer': Company.get(employeer_id).get_client_side_dict()} else: if member.can_update_company_partner(current_user_right) == True: member.set_client_side_dict(status=json['member']['status'], rights=json['member']['rights']) if action == 'validate': member.detach() validate = member.validate(False) return validate else: member.save() return member.get_client_side_dict()
def signup(json_data): action = g.req('action', allowed=['validate', 'save']) params = utils.filter_json( json_data, 'first_name,last_name,email,password,password_confirmation') params['address_email'] = params['email'] del params['email'] new_user = User(**params) if action == 'validate': return new_user.validate(True) else: new_user.set_password_hash() new_user.save() g.db.commit() new_user.generate_confirmation_token( get_after_logination_params()).save() new_user.NOTIFY_WELCOME() g.db.commit() return {}
def load_form_create(json_data, company_id=None, material_id=None): action = g.req('action', allowed=['load', 'validate', 'save']) if material_id: material = Material.get(material_id) else: material = Material(company=Company.get(company_id), company_id=company_id, editor=g.user) if action == 'load': return {'material': material.get_client_side_dict(more_fields='long|company|illustration')} else: parameters = g.filter_json(json_data, 'material.title|subtitle|short|long|keywords|author') material.attr(parameters['material']) if action == 'validate': material.detach() return material.validate(material.id is not None) else: material.save() material.illustration = json_data['material']['illustration'] return {'material': material.save().get_client_side_dict(more_fields='long|company|illustration')}
def submit_or_publish_material(json, material_id): action = g.req('action', allowed=['load', 'validate', 'save']) publication = Publication(material=Material.get(material_id)) publisher_membership = MemberCompanyPortal.get( json['publisher_membership']['id']) employment = UserCompany.get_by_user_and_company_ids( company_id=publication.material.company_id) also_publish = json.get('also_publish', None) if action == 'load': return utils.dict_merge( publish_dialog_load(publication, publisher_membership), {'can_material_also_be_published': employment.rights[RIGHT_AT_COMPANY.ARTICLES_SUBMIT_OR_PUBLISH] and \ publisher_membership.rights[RIGHT_AT_PORTAL.PUBLICATION_PUBLISH] }) else: publish_dialog_save( publication, json['publication'], status=Publication.STATUSES['PUBLISHED'] if also_publish else Publication.STATUSES['SUBMITTED']) if action == 'validate': publication.detach() return publication.validate(True) else: publication.save() utils.db.execute_function( "tag_publication_set_position('%s', ARRAY ['%s']);" % (publication.id, "', '".join([t.id for t in publication.tags]))) publisher_membership.NOTIFY_MATERIAL_ACTION_BY_COMPANY_OR_PORTAL( action='PUBLISH' if also_publish else 'SUBMIT', company_or_portal='company', material_title=publication.material.title) return publisher_membership.material_or_publication_grid_row( publication.material)
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()
def membership_set_tags(json, membership_id): membership = MemberCompanyPortal.get(membership_id) action = g.req('action', allowed=['load', 'validate', 'save']) if action == 'load': # catalog_division = g.db.query(PortalDivision).filter(and_(PortalDivision.portal_id == portal_id, # PortalDivision.portal_division_type_id == 'catalog')).first() return { 'membership': membership.get_client_side_dict( fields='id,portal,company,tags,portal.divisions'), } else: membership.tags = [ Tag.get(t['id']) for t in json['membership']['tags'] ] if action == 'validate': membership.detach() return PRBase.DEFAULT_VALIDATION_ANSWER() else: membership.save().set_tags_positions() return {'membership': membership.portal_memberee_grid_row()}
def request_membership_plan(json, membership_id): action = g.req('action', allowed=['load', 'save', 'validate']) membership = MemberCompanyPortal.get(membership_id) if action == 'load': return membership.get_client_side_dict_for_plan() else: immediately = True if json['membership'][ 'request_membership_plan_issued_immediately'] else False requested_plan_id = json.get('selected_by_user_plan_id', None) if action == 'validate': ret = PRBase.DEFAULT_VALIDATION_ANSWER() # if we can apply it immediately and it have to be confirmed by portal company owner if (requested_plan_id is True and not membership.requested_membership_plan_issued.auto_apply) \ or (requested_plan_id is not False and requested_plan_id is not True and not MembershipPlan.get(requested_plan_id).auto_apply and requested_plan_id != membership.portal.default_membership_plan_id): ret['warnings'][ 'general'] = 'plan must be confirmed by company owner before activation' return ret else: return membership.requested_new_plan_issued( requested_plan_id, immediately).portal_memberee_grid_row()
def profile_load_validate_save(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() user_company = UserCompany.get_by_user_and_company_ids( company_id=company_id) if user_company: company_dict['actions'] = { 'edit_company_profile': EmployeeHasRightAtCompany( RIGHT_AT_COMPANY.COMPANY_EDIT_PROFILE).check( company_id=company.id), 'edit_portal_profile': EmployeeHasRightAtCompany( RIGHT_AT_COMPANY.PORTAL_EDIT_PROFILE).check( company_id=company.id) } return company_dict else: company.attr( utils.filter_json(json, 'about', 'address', 'country', 'email', 'web', 'name', 'phone', 'city', 'postcode', '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: if company_id is None: company.setup_new_company().save() company.logo = json['logo'] return utils.dict_merge( company.save().get_client_side_dict(), actions={'edit': True if company_id else False})
def load_form_create(json_data, company_id=None, material_id=None): action = g.req('action', allowed=['load', 'validate', 'save']) if material_id: material = Material.get(material_id) else: material = Material(company=Company.get(company_id), company_id=company_id, editor=g.user) if action == 'load': return { 'material': material.get_client_side_dict( more_fields='long|company|illustration') } else: parameters = g.filter_json( json_data, 'material.title|subtitle|short|long|keywords|author') material.attr(parameters['material']) if action == 'validate': material.detach() return material.validate(material.id is not None) else: material.illustration = utils.dict_merge( json_data['material']['illustration'], { 'company': material.company, 'file_name_prefix': 'illustration_for_material_%s' % (material.id, ) }) return { 'material': material.save().get_client_side_dict( more_fields='long|company|illustration') }
def employee_update_load(json, company_id, user_id): action = g.req('action', allowed=['load', 'validate', 'save']) employment = UserCompany.get(user_id=user_id, company_id=company_id) if action == 'load': return { 'employment': employment.get_client_side_dict(), 'employee': employment.employee.get_client_side_dict(), 'employer': employment.employer.get_client_side_dict( fields='id|logo_file_id|name'), # 'statuses_available': UserCompany.get_statuses_avaible(company_id), # 'rights_available': employment.get_rights_avaible() } else: employment.set_client_side_dict(json['employment']) if action == 'validate': employment.detach() return employment.validate(False) else: employment.save() return employment.get_client_side_dict()
def publish(json, publication_id, actor_membership_id, request_from): action = g.req('action', allowed=['load', 'validate', 'save']) publication = Publication.get(publication_id) actor_membership = MemberCompanyPortal.get(actor_membership_id) publisher_membership = MemberCompanyPortal.get_by_portal_id_company_id( publication.portal_division.portal_id, publication.material.company_id) if action == 'load': return publish_dialog_load(publication, publisher_membership) else: publish_dialog_save(publication, json['publication'], status=Publication.STATUSES['PUBLISHED']) if action == 'validate': publication.detach() return publication.validate(True) else: publication.save() utils.db.execute_function( "tag_publication_set_position('%s', ARRAY ['%s']);" % (publication.id, "', '".join([t.id for t in publication.tags]))) actor_membership.NOTIFY_MATERIAL_ACTION_BY_COMPANY_OR_PORTAL( action='PUBLISH', material_title=publication.material.title, company_or_portal='company' if request_from == 'company_material_details' else 'portal') if request_from == 'company_material_details': return actor_membership.material_or_publication_grid_row( publication.material) elif request_from == 'portal_publications': return publication.portal_publication_grid_row(actor_membership)
def edit_profile_load(json, user_id): action = g.req('action', allowed=['load', 'validate', 'save']) user = User.get(user_id) if action == 'load': ret = { 'user': user.get_client_side_dict(), 'languages': Config.LANGUAGES, 'countries': Country.get_countries() } ret['user']['avatar'] = user.avatar return ret else: user_data = utils.filter_json( json['user'], 'first_name, last_name, birth_tm, lang, country_id, location, gender, address_url, address_phone, address_city, address_location, about, password, password_confirmation' ) user_data['country_id'] = user_data['country_id'] if user_data[ 'country_id'] else '56f52e6b-1273-4001-b15d-d5471ebfc075' user_data['full_name'] = user_data['first_name'] + ' ' + user_data[ 'last_name'] user_data['birth_tm'] = user_data['birth_tm'] if user_data[ 'birth_tm'] else None avatar = json['user']['avatar'] user.attr(user_data) if user.password == '': user.password = None if action == 'validate': user.detach() validate = user.validate(False) return validate else: user.avatar = avatar user.set_password_hash() ret = {'user': user.save().get_client_side_dict()} return ret
def plans_load(json, portal_id): action = g.req('action', allowed=['load', 'save', 'validate']) portal = Portal.get(portal_id) def client_side(): client_dict = { 'plans': utils.get_client_side_list(portal.plans), 'select': { 'portal': portal.get_client_side_dict( more_fields='default_membership_plan_id'), 'currency': Currency.get_all_active_ordered_by_position(), 'duration_unit': MembershipPlan.DURATION_UNITS } } for plan in client_dict['plans']: plan['duration'], plan['duration_unit'] = plan['duration'].split( ' ') return client_dict if action != 'load': if set(portal.plans) - set( utils.find_by_id(portal.plans, d['id']) for d in json['plans']) != set(): raise exceptions.BadDataProvided( 'Information for some existing plans is not provided by client' ) plan_position = 0 validation = PRBase.DEFAULT_VALIDATION_ANSWER() default_plan = 0 for jp in json['plans']: plan = utils.find_by_id(portal.plans, jp['id']) or MembershipPlan( portal=portal, id=jp['id']) plan.portal = portal plan.position = plan_position plan.attr_filter( jp, 'name', 'default', 'price', 'currency_id', 'status', 'auto_apply', *[ 'publication_count_' + t for t in ['open', 'payed', 'registered'] ]) plan.duration = "%s %s" % (jp['duration'], jp['duration_unit']) if plan not in portal.plans: portal.plans.append(plan) if jp['id'] == json['select']['portal'].get('default_membership_plan_id', None) and plan.status == \ MembershipPlan.STATUSES['MEMBERSHIP_PLAN_ACTIVE']: default_plan += 1 portal.default_membership_plan = plan plan_position += 1 portal.validation_append_by_ids(validation, portal.plans, 'plans') if default_plan != 1: validation['errors'][ 'one_default_active_plan'] = 'You need one and only one default plan' if action == 'validate': g.db.expunge_all() return validation else: for plan in portal.plans: if not plan.cr_tm: plan.id = None if not len(validation['errors']): portal.save() g.db.commit() return client_side()
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) }
def profile_load(json, company_id=None, portal_id=None): action = g.req('action', allowed=['load', 'save', 'validate']) portal = Portal.get(portal_id) if portal_id else Portal.launch_new_portal( Company.get(company_id)) layouts = utils.db.query_filter(PortalLayout) if not g.user.is_maintainer(): layouts = layouts.filter( or_(PortalLayout.hidden == False, PortalLayout.id == portal.portal_layout_id)) division_types = PortalDivisionType.get_division_types() def client_side(): ret = { 'select': { 'languages': Config.LANGUAGES, 'layouts': utils.get_client_side_list(layouts.all(), more_fields='hidden'), 'division_types': utils.get_client_side_dict(division_types) }, 'portal': portal.get_client_side_dict( fields= 'name,host, logo, favicon, lang, url_facebook, url_google, url_twitter, url_linkedin,' 'portal_layout_id,divisions,divisions.html_description|html_keywords|url|html_title|cr_tm|name,own_company,company_memberships.company', get_own_or_profi_host=True, get_publications_count=True) } return ret if action == 'load': return client_side() else: jp = json['portal'] jp['host'] = ( jp['host_profi'] + '.' + Config.MAIN_DOMAIN ) if jp['host_profi_or_own'] == 'profi' else jp['host_own'] portal.attr_filter( jp, 'name', 'lang', 'portal_layout_id', 'host', *map(lambda x: 'url_' + x, ['facebook', 'google', 'twitter', 'linkedin'])) if set(portal.divisions) - set( utils.find_by_id(portal.divisions, d['id']) for d in jp['divisions']) != set(): raise exceptions.BadDataProvided( 'Information for some existing portal division is not provided by client' ) division_position = 0 unpublish_warning = {} changed_division_types = {} deleted_divisions = [] for jd in jp['divisions']: ndi = utils.find_by_id(portal.divisions, jd['id']) or PortalDivision(portal=portal, id=jd['id']) if jd.get('remove_this_existing_division', None): deleted_divisions.append(ndi) if len(ndi.publications): utils.dict_deep_replace( 'this division have %s published articles. they will be unpublished' % (len(ndi.publications), ), unpublish_warning, ndi.id, 'actions') portal.divisions.remove(ndi) else: ndi.portal = portal ndi.position = division_position ndi.attr_filter(jd, 'name', 'html_title', 'html_keywords', 'html_description', 'url') if ndi in portal.divisions: if len(ndi.publications) and jd[ 'portal_division_type_id'] != ndi.portal_division_type.id: changed_division_types[ ndi.id] = ndi.portal_division_type.id utils.dict_deep_replace( 'this division have %s published articles. they will be unpublished becouse of division type changed' % (len(ndi.publications), ), unpublish_warning, ndi.id, 'type') else: portal.divisions.append(ndi) ndi.portal_division_type = utils.find_by_id( division_types, jd['portal_division_type_id']) ndi.settings = jd.get('settings', {}) division_position += 1 if action == 'validate': ret = portal.validate(not portal_id) if len(unpublish_warning.keys()): if 'divisions' not in ret['warnings']: ret['warnings']['divisions'] = {} ret['warnings']['divisions'] = utils.dict_merge_recursive( ret['warnings']['divisions'], unpublish_warning) g.db.expunge_all() return ret else: for nd in portal.divisions: if not nd.cr_tm: nd.id = None portal.logo = jp['logo'] portal.favicon = jp['favicon'] unpublished_publications_by_membership_id = {} def append_to_phrases(publication): utils.dict_deep_replace( [], unpublished_publications_by_membership_id, publication.get_publisher_membership().id, add_only_if_not_exists=True).append( Phrase("deleted a publication named `%(title)s`", dict={'title': publication.material.title})) for div in deleted_divisions: for p in div.publications: append_to_phrases(p) for div in portal.divisions: if div.id in changed_division_types: for p in div.publications: append_to_phrases(p) div.publications = [] if not portal_id: g.db.add(portal) portal.company_memberships[ 0].current_membership_plan_issued = portal.company_memberships[ 0].create_issued_plan() portal.company_memberships[ 0].current_membership_plan_issued.start() UserCompany.get_by_user_and_company_ids(company_id=company_id). \ NOTIFY_PORTAL_CREATED(portal_host=portal.host, portal_name=portal.name) if unpublished_publications_by_membership_id: for membership_id, phrases in unpublished_publications_by_membership_id.items( ): MemberCompanyPortal.get( membership_id ).NOTIFY_MATERIAL_PUBLICATION_BULK_ACTION( 'purged', 'division type was changed or division was deleted', more_phrases_to_company=phrases, more_phrases_to_portal=phrases) try: portal.setup_ssl() except Exception as e: current_app.log.error( 'Error processing ssl for portal', **current_app.log.extra(exception=e, portal=portal.get_client_side_dict( fields='id,host,name'))) try: from profapp.models.third.google_analytics_management import GoogleAnalyticsManagement ga_man = GoogleAnalyticsManagement() portal.setup_google_analytics(ga_man, force_recreate=False) portal.update_google_analytics_host(ga_man) except Exception as e: current_app.log.error(e) current_app.log.error( 'Error processing google analytics for portal', **current_app.log.extra(portal=portal.get_client_side_dict( fields='id,host,name'))) portal.save() g.db.commit() return client_side()
def submit_publish(json, article_action): action = g.req('action', allowed=['load', 'validate', 'save']) company = Company.get(json['company']['id']) if article_action == 'SUBMIT': material = Material.get(json['material']['id']) check = EditOrSubmitMaterialInPortal( material=material, portal=json['portal']['id']).action_is_allowed(article_action) if check != True: return check publication = Publication(material=material) more_data_to_ret = { 'material': { 'id': material.id }, 'can_material_also_be_published': check == True } else: publication = Publication.get(json['publication']['id']) check = PublishUnpublishInPortal( publication=publication, division=publication.portal_division_id, company=company).action_is_allowed(article_action) if check != True: return check more_data_to_ret = {} if action == 'load': portal = Portal.get(json['portal']['id']) ret = { 'publication': publication.get_client_side_dict(), 'company': company.get_client_side_dict(), 'portal': portal.get_client_side_dict() } ret['portal']['divisions'] = PRBase.get_ordered_dict( PublishUnpublishInPortal().get_active_division(portal.divisions)) return utils.dict_merge(ret, more_data_to_ret) else: publication.attr( g.filter_json(json['publication'], 'portal_division_id')) publication.publishing_tm = PRBase.parse_timestamp( json['publication'].get('publishing_tm')) publication.event_begin_tm = PRBase.parse_timestamp( json['publication'].get('event_begin_tm')) publication.event_end_tm = PRBase.parse_timestamp( json['publication'].get('event_end_tm')) publication.tags = [ Tag.get(t['id']) for t in json['publication']['tags'] ] if 'also_publish' in json and json['also_publish']: publication.status = PublishUnpublishInPortal.STATUSES['PUBLISHED'] else: if article_action in [ PublishUnpublishInPortal.ACTIONS['PUBLISH'], PublishUnpublishInPortal.ACTIONS['REPUBLISH'] ]: publication.status = PublishUnpublishInPortal.STATUSES[ 'PUBLISHED'] elif article_action in [ PublishUnpublishInPortal.ACTIONS['UNPUBLISH'], PublishUnpublishInPortal.ACTIONS['UNDELETE'] ]: publication.status = PublishUnpublishInPortal.STATUSES[ 'UNPUBLISHED'] elif article_action in [ PublishUnpublishInPortal.ACTIONS['DELETE'] ]: publication.status = PublishUnpublishInPortal.STATUSES[ 'DELETED'] if action == 'validate': publication.detach() return publication.validate(True if article_action == 'SUBMIT' else False) else: # if article_action == 'SUBMIT': # publication.long = material.clone_for_portal_images_and_replace_urls(publication.portal_division_id, # publication) publication.save().set_tags_positions() return get_portal_dict_for_material( publication.portal, company, publication=publication, submit=article_action == 'SUBMIT')
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) }
def submit_publish(json, article_action): action = g.req('action', allowed=['load', 'validate', 'save']) company = Company.get(json['company']['id']) if article_action == 'SUBMIT': material = ArticleCompany.get(json['material']['id']) publication = ArticlePortalDivision(title=material.title, subtitle=material.subtitle, keywords=material.keywords, short=material.short, long=material.long, article_company_id=material.id) more_data_to_ret = { 'material': { 'id': material.id }, 'can_material_also_be_published': MemberCompanyPortal.get( portal_id=json['portal']['id'], company_id=json['company']['id']).has_rights( MemberCompanyPortal.RIGHT_AT_PORTAL.PUBLICATION_PUBLISH) } else: publication = ArticlePortalDivision.get(json['publication']['id']) more_data_to_ret = {} if action == 'load': ret = { 'publication': publication.get_client_side_dict(), 'company': company.get_client_side_dict(), 'portal': Portal.get(json['portal']['id']).get_client_side_dict() } ret['portal']['divisions'] = PRBase.get_ordered_dict([ d for d in ret['portal']['divisions'] if (d['portal_division_type_id'] in ['events', 'news']) ]) return PRBase.merge_dicts(ret, more_data_to_ret) else: publication.attr( g.filter_json(json['publication'], 'portal_division_id')) publication.publishing_tm = PRBase.parse_timestamp( json['publication'].get('publishing_tm')) publication.event_tm = PRBase.parse_timestamp( json['publication'].get('event_tm')) if 'also_publish' in json and json['also_publish']: publication.status = ArticlePortalDivision.STATUSES['PUBLISHED'] else: if article_action in [ ArticlePortalDivision.ACTIONS['PUBLISH'], ArticlePortalDivision.ACTIONS['REPUBLISH'] ]: publication.status = ArticlePortalDivision.STATUSES[ 'PUBLISHED'] elif article_action in [ ArticlePortalDivision.ACTIONS['UNPUBLISH'], ArticlePortalDivision.ACTIONS['UNDELETE'] ]: publication.status = ArticlePortalDivision.STATUSES[ 'UNPUBLISHED'] elif article_action in [ArticlePortalDivision.ACTIONS['DELETE']]: publication.status = ArticlePortalDivision.STATUSES['DELETED'] if action == 'validate': publication.detach() return publication.validate(True if article_action == 'SUBMIT' else False) else: if article_action == 'SUBMIT': publication.long = material.clone_for_portal_images_and_replace_urls( publication.portal_division_id, publication) publication.save() return get_portal_dict_for_material(publication.portal, company, publication=publication)
def profile_load(json, create_or_update, company_id, portal_id=None): action = g.req('action', allowed=['load', 'save', 'validate']) layouts = [x.get_client_side_dict() for x in db(PortalLayout).all()] types = {x.id: x.get_client_side_dict() for x in PortalDivisionType.get_division_types()} company = Company.get(company_id) portal = Portal() if portal_id is None else Portal.get(portal_id) if action == 'load': ret = {'company': company.get_client_side_dict(), 'layouts': layouts, 'division_types': types} if create_or_update == 'update': ret['portal'] = {} ret['portal_company_members'] = company.portal_members ret['host_profi_or_own'] = 'profi' if re.match(r'^profireader\.', ret['portal']['hostname']) else 'own' else: ret['portal_company_members'] = [company.get_client_side_dict()] ret['portal'] = {'company_owner_id': company_id, 'name': '', 'host': '', 'logo_file_id': company.logo_file_id, 'host_profi_or_own': 'profi', 'host_own': '', 'host_profi': '', 'portal_layout_id': layouts[0]['id'], 'divisions': [ {'name': 'index page', 'portal_division_type_id': 'index', 'page_size': ''}, {'name': 'news', 'portal_division_type_id': 'news', 'page_size': ''}, {'name': 'events', 'portal_division_type_id': 'events', 'page_size': ''}, {'name': 'catalog', 'portal_division_type_id': 'catalog', 'page_size': ''}, {'name': 'our subportal', 'portal_division_type_id': 'company_subportal', 'page_size': '', 'settings': {'company_id': ret['portal_company_members'][0]['id']}}]} ret['logo'] = portal.get_logo_client_side_dict() return ret else: json_portal = json['portal'] if create_or_update == 'update': pass elif create_or_update == 'create': page_size_for_config = dict() for a in json_portal['divisions']: page_size_for_config[a.get('name')] = a.get('page_size') \ if type(a.get('page_size')) is int and a.get('page_size') != 0 \ else Config.ITEMS_PER_PAGE # print(json) json_portal['host'] = (json_portal['host_profi'] + '.profireader.com') \ if json_portal['host_profi_or_own'] == 'profi' else json_portal['host_own'] portal = Portal(company_owner=company, **g.filter_json(json_portal, 'name', 'portal_layout_id', 'host')) divisions = [] for division_json in json['portal']['divisions']: division = PortalDivision(portal, portal_division_type_id=division_json['portal_division_type_id'], position=len(json['portal']['divisions']) - len(divisions), name=division_json['name']) if division_json['portal_division_type_id'] == 'company_subportal': division.settings = {'member_company_portal': portal.company_members[0]} divisions.append(division) # self, portal=portal, portal_division_type=portal_division_type, name='', settings={} portal.divisions = divisions PortalConfig(portal=portal, page_size_for_divisions=page_size_for_config) if action == 'save': portal.setup_created_portal(g.filter_json(json_portal, 'logo_file_id')).save() portal_dict = portal.set_logo_client_side_dict(json['logo']).save().get_client_side_dict() return portal_dict else: return portal.validate(create_or_update == 'create')
def news_feeds_load(json, company_id): action = g.req('action', allowed=['load', 'save', 'validate']) company = Company.get(company_id) def client_side(): client_dict = { 'news_feeds': utils.get_client_side_list(company.news_feeds), 'select': { 'news_feed_types': ['rss'] } } # for news_feed in client_dict['plans']: # plan['duration'], plan['duration_unit'] = plan['duration'].split(' ') return client_dict if action != 'load': if set(company.news_feeds) - set( utils.find_by_id(company.news_feeds, d['id']) for d in json['news_feeds']) != set(): raise exceptions.BadDataProvided( 'Information for some existing news_feeds is not provided by client' ) # plan_position = 0 validation = PRBase.DEFAULT_VALIDATION_ANSWER() default_plan = 0 for nf in json['news_feeds']: news_feed = utils.find_by_id(company.news_feeds, nf['id']) or NewsFeedCompany( company=company, id=nf['id']) if nf.get('status', '') == 'DELETED': company.news_feeds.remove(news_feed) else: news_feed.company = company # plan.position = plan_position news_feed.attr_filter(nf, 'name', 'source') news_feed.type = 'RSS' # plan.duration = "%s %s" % (nf['duration'], nf['duration_unit']) if news_feed not in company.news_feeds: company.news_feeds.append(news_feed) # if nf['id'] == json['select']['portal'].get('default_membership_plan_id', None) and plan.status == \ # MembershipPlan.STATUSES['MEMBERSHIP_PLAN_ACTIVE']: # default_plan += 1 # portal.default_membership_plan = plan # # plan_position += 1 company.validation_append_by_ids(validation, company.news_feeds, 'news_feeds') # if default_plan != 1: # validation['errors']['one_default_active_plan'] = 'You need one and only one default plan' if action == 'validate': g.db.expunge_all() return validation else: for news_feed in company.news_feeds: if not news_feed.cr_tm: news_feed.id = None if not len(validation['errors']): company.save() g.db.commit() return client_side()