from creme import commercial from .constants import REL_OBJ_COMPLETE_GOAL from .models import ( CommercialApproach, MarketSegment, MarketSegmentDescription, CommercialAsset, MarketSegmentCharm, ActObjective, ActObjectivePatternComponent, ) from .setting_keys import orga_approaches_key get_ct = ContentType.objects.get_for_model Opportunity = get_opportunity_model() Act = commercial.get_act_model() ActObjectivePattern = commercial.get_pattern_model() Strategy = commercial.get_strategy_model() class ApproachesBrick(QuerysetBrick): id_ = QuerysetBrick.generate_id('commercial', 'approaches') dependencies = (CommercialApproach, ) # order_by = 'title' order_by = '-creation_date' verbose_name = _('Commercial approaches') template_name = 'commercial/bricks/approaches.html' # TODO: factorise with assistants blocks (CremeEntity method ??) @staticmethod def _populate_related_real_entities(comapps, user):
def populate(self): already_populated = RelationType.objects.filter( pk=constants.REL_SUB_SOLD).exists() Act = commercial.get_act_model() ActObjectivePattern = commercial.get_pattern_model() Strategy = commercial.get_strategy_model() Contact = persons.get_contact_model() Organisation = persons.get_organisation_model() Product = products.get_product_model() Service = products.get_service_model() RelationType.create( (constants.REL_SUB_SOLD, _('has sold'), [Contact, Organisation]), (constants.REL_OBJ_SOLD, _('has been sold by'), [Product, Service ]), ) complete_goal_models = {*creme_registry.iter_entity_models()} complete_goal_models.discard(Strategy) if apps.is_installed('creme.billing'): from creme import billing from creme.billing.registry import lines_registry complete_goal_models.discard(billing.get_credit_note_model()) complete_goal_models.discard(billing.get_template_base_model()) complete_goal_models.difference_update(lines_registry) RelationType.create( ( constants.REL_SUB_COMPLETE_GOAL, _('completes a goal of the commercial action'), complete_goal_models, ), ( constants.REL_OBJ_COMPLETE_GOAL, _('is completed thanks to'), [Act], ), ) # --------------------------- CremePropertyType.create(constants.PROP_IS_A_SALESMAN, _('is a salesman'), [Contact]) # --------------------------- MarketSegment.objects.get_or_create( property_type=None, defaults={'name': _('All the organisations')}, ) # --------------------------- for i, title in enumerate( [_('Phone calls'), _('Show'), _('Demo')], start=1): create_if_needed(ActType, {'pk': i}, title=title, is_custom=False) # --------------------------- create_hf = HeaderFilter.objects.create_if_needed create_hf( pk=constants.DEFAULT_HFILTER_ACT, model=Act, name=_('Com Action view'), cells_desc=[ (EntityCellRegularField, { 'name': 'name' }), (EntityCellRegularField, { 'name': 'expected_sales' }), (EntityCellRegularField, { 'name': 'due_date' }), ], ) create_hf( pk=constants.DEFAULT_HFILTER_STRATEGY, model=Strategy, name=_('Strategy view'), cells_desc=[(EntityCellRegularField, { 'name': 'name' })], ) create_hf( pk=constants.DEFAULT_HFILTER_PATTERN, model=ActObjectivePattern, name=_('Objective pattern view'), cells_desc=[ (EntityCellRegularField, { 'name': 'name' }), (EntityCellRegularField, { 'name': 'segment' }), ], ) # --------------------------- def build_custom_form_items(creation_descriptor, edition_descriptor, field_names): base_groups_desc = [ { 'name': _('General information'), 'layout': LAYOUT_DUAL_FIRST, 'cells': [ *((EntityCellRegularField, { 'name': fname }) for fname in field_names), ( EntityCellCustomFormSpecial, { 'name': EntityCellCustomFormSpecial. REMAINING_REGULARFIELDS }, ), ], }, { 'name': _('Description'), 'layout': LAYOUT_DUAL_SECOND, 'cells': [ (EntityCellRegularField, { 'name': 'description' }), ], }, { 'name': _('Custom fields'), 'layout': LAYOUT_DUAL_SECOND, 'cells': [ ( EntityCellCustomFormSpecial, { 'name': EntityCellCustomFormSpecial. REMAINING_CUSTOMFIELDS }, ), ], }, ] CustomFormConfigItem.objects.create_if_needed( descriptor=creation_descriptor, groups_desc=[ *base_groups_desc, { 'name': _('Properties'), 'cells': [ ( EntityCellCustomFormSpecial, { 'name': EntityCellCustomFormSpecial. CREME_PROPERTIES }, ), ], }, { 'name': _('Relationships'), 'cells': [ ( EntityCellCustomFormSpecial, { 'name': EntityCellCustomFormSpecial.RELATIONS }, ), ], }, ], ) CustomFormConfigItem.objects.create_if_needed( descriptor=edition_descriptor, groups_desc=base_groups_desc, ) build_custom_form_items( creation_descriptor=custom_forms.ACT_CREATION_CFORM, edition_descriptor=custom_forms.ACT_EDITION_CFORM, field_names=[ 'user', 'name', 'expected_sales', 'cost', 'goal', 'start', 'due_date', 'act_type', 'segment', ], ) build_custom_form_items( creation_descriptor=custom_forms.PATTERN_CREATION_CFORM, edition_descriptor=custom_forms.PATTERN_EDITION_CFORM, field_names=[ 'user', 'name', 'average_sales', 'segment', ], ) build_custom_form_items( creation_descriptor=custom_forms.STRATEGY_CREATION_CFORM, edition_descriptor=custom_forms.STRATEGY_EDITION_CFORM, field_names=[ 'user', 'name', ], ) # --------------------------- create_searchconf = SearchConfigItem.objects.create_if_needed create_searchconf(Act, ['name', 'expected_sales', 'cost', 'goal']) create_searchconf(Strategy, ['name']) create_searchconf(ActObjectivePattern, [], disabled=True) # --------------------------- SettingValue.objects.get_or_create( key_id=setting_keys.orga_approaches_key.id, defaults={'value': True}, ) # --------------------------- Job.objects.get_or_create( type_id=creme_jobs.com_approaches_emails_send_type.id, defaults={ 'language': settings.LANGUAGE_CODE, 'periodicity': date_period_registry.get_period('days', 1), 'status': Job.STATUS_OK, # The CommercialApproach field for Activities' CustomForms is not # in the default configuration, so a enabled job would be annoying. 'enabled': False, }, ) # --------------------------- # TODO: move to "not already_populated" section in creme2.4 if not MenuConfigItem.objects.filter( entry_id__startswith='commercial-').exists(): container = MenuConfigItem.objects.get_or_create( entry_id=ContainerEntry.id, entry_data={'label': _('Commercial')}, defaults={'order': 30}, )[0] create_mitem = MenuConfigItem.objects.create create_mitem(entry_id=menu.ActsEntry.id, order=50, parent=container) create_mitem(entry_id=menu.StrategiesEntry.id, order=55, parent=container) create_mitem(entry_id=menu.SegmentsEntry.id, order=60, parent=container) create_mitem(entry_id=menu.PatternsEntry.id, order=70, parent=container) directory = MenuConfigItem.objects.filter( entry_id=ContainerEntry.id, entry_data={ 'label': _('Directory') }, ).first() if directory is not None: create_mitem(entry_id=menu.SalesmenEntry.id, order=100, parent=directory) creations = MenuConfigItem.objects.filter( entry_id=ContainerEntry.id, entry_data={ 'label': _('+ Creation') }, ).first() if creations is not None: create_mitem(entry_id=menu.ActCreationEntry.id, order=40, parent=creations) # --------------------------- if not already_populated: ButtonMenuItem.objects.create_if_needed( button=buttons.CompleteGoalButton, order=60, ) TOP = BrickDetailviewLocation.TOP RIGHT = BrickDetailviewLocation.RIGHT LEFT = BrickDetailviewLocation.LEFT # BrickDetailviewLocation.objects.multi_create( # defaults={'brick': bricks.ApproachesBrick, 'order': 10, 'zone': RIGHT}, # data=[ # {}, # default configuration # {'model': Contact}, # {'model': Organisation}, # ] # ) BrickDetailviewLocation.objects.multi_create( defaults={ 'model': Act, 'zone': LEFT }, data=[ { 'order': 5 }, # generic information brick { 'brick': bricks.ActObjectivesBrick, 'order': 10 }, { 'brick': bricks.RelatedOpportunitiesBrick, 'order': 20 }, { 'brick': core_bricks.CustomFieldsBrick, 'order': 40 }, { 'brick': core_bricks.PropertiesBrick, 'order': 450 }, { 'brick': core_bricks.RelationsBrick, 'order': 500 }, { 'brick': core_bricks.HistoryBrick, 'order': 20, 'zone': RIGHT }, ], ) BrickDetailviewLocation.objects.multi_create( defaults={ 'model': ActObjectivePattern, 'zone': LEFT }, data=[ { 'brick': bricks.PatternComponentsBrick, 'order': 10, 'zone': TOP }, { 'order': 5 }, { 'brick': core_bricks.CustomFieldsBrick, 'order': 40 }, { 'brick': core_bricks.PropertiesBrick, 'order': 450 }, { 'brick': core_bricks.RelationsBrick, 'order': 500 }, { 'brick': core_bricks.HistoryBrick, 'order': 20, 'zone': RIGHT }, ], ) BrickDetailviewLocation.objects.multi_create( defaults={ 'model': Strategy, 'zone': LEFT }, data=[ { 'brick': bricks.SegmentDescriptionsBrick, 'order': 10, 'zone': TOP }, { 'order': 5 }, { 'brick': core_bricks.CustomFieldsBrick, 'order': 40 }, { 'brick': bricks.EvaluatedOrgasBrick, 'order': 50 }, { 'brick': bricks.AssetsBrick, 'order': 60 }, { 'brick': bricks.CharmsBrick, 'order': 70 }, { 'brick': core_bricks.PropertiesBrick, 'order': 450 }, { 'brick': core_bricks.RelationsBrick, 'order': 500 }, { 'brick': core_bricks.HistoryBrick, 'order': 20, 'zone': RIGHT }, ], ) if apps.is_installed('creme.assistants'): logger.info('Assistants app is installed ' '=> we use the assistants blocks on detail views') from creme.assistants import bricks as a_bricks for model in (Act, ActObjectivePattern, Strategy): BrickDetailviewLocation.objects.multi_create( defaults={ 'model': model, 'zone': RIGHT }, data=[ { 'brick': a_bricks.TodosBrick, 'order': 100 }, { 'brick': a_bricks.MemosBrick, 'order': 200 }, { 'brick': a_bricks.AlertsBrick, 'order': 300 }, { 'brick': a_bricks.UserMessagesBrick, 'order': 400 }, ], ) if apps.is_installed('creme.documents'): # logger.info("Documents app is installed # => we use the documents blocks on Strategy's detail views") from creme.documents.bricks import LinkedDocsBrick BrickDetailviewLocation.objects.multi_create( defaults={ 'brick': LinkedDocsBrick, 'order': 600, 'zone': RIGHT }, data=[ { 'model': Act }, { 'model': ActObjectivePattern }, { 'model': Strategy }, ], )
else: class Opportunities4Card: dependencies: List[Type[CremeEntity]] = [] relation_type_deps: List[str] = [] @staticmethod def get(context, entity): return None if apps.is_installed('creme.commercial'): from creme.commercial import constants as commercial_constants from creme.commercial import get_act_model Act = get_act_model() class CommercialActs4Card: dependencies = [Act] relation_type_deps = [commercial_constants.REL_SUB_COMPLETE_GOAL] @staticmethod def get(context, entity): return EntityCredentials.filter( context['user'], Act.objects.annotate(relations_w_person=FilteredRelation( 'relations', condition=Q(relations__object_entity=entity.id), ), ).filter( is_deleted=False, relations_w_person__type=commercial_constants.
def populate(self): already_populated = RelationType.objects.filter(pk=constants.REL_SUB_SOLD_BY).exists() Act = commercial.get_act_model() ActObjectivePattern = commercial.get_pattern_model() Strategy = commercial.get_strategy_model() Contact = persons.get_contact_model() Organisation = persons.get_organisation_model() RelationType.create((constants.REL_SUB_SOLD_BY, _(u'has sold')), (constants.REL_OBJ_SOLD_BY, _(u'has been sold by'))) RelationType.create((constants.REL_SUB_COMPLETE_GOAL, _(u'completes a goal of the commercial action')), (constants.REL_OBJ_COMPLETE_GOAL, _(u'is completed thanks to'), [Act])) # --------------------------- CremePropertyType.create(constants.PROP_IS_A_SALESMAN, _(u'is a salesman'), [Contact]) # --------------------------- MarketSegment.objects.get_or_create(property_type=None, defaults={'name': _(u'All the organisations')}, ) # --------------------------- for i, title in enumerate([_(u'Phone calls'), _(u'Show'), _(u'Demo')], start=1): create_if_needed(ActType, {'pk': i}, title=title, is_custom=False) # --------------------------- create_hf = HeaderFilter.create create_hf(pk=constants.DEFAULT_HFILTER_ACT, model=Act, name=_(u'Com Action view'), cells_desc=[(EntityCellRegularField, {'name': 'name'}), (EntityCellRegularField, {'name': 'expected_sales'}), (EntityCellRegularField, {'name': 'due_date'}), ], ) create_hf(pk=constants.DEFAULT_HFILTER_STRATEGY, model=Strategy, name=_(u"Strategy view"), cells_desc=[(EntityCellRegularField, {'name': 'name'})], ) create_hf(pk=constants.DEFAULT_HFILTER_PATTERN, model=ActObjectivePattern, name=_(u"Objective pattern view"), cells_desc=[(EntityCellRegularField, {'name': 'name'}), (EntityCellRegularField, {'name': 'segment'}), ] ) # --------------------------- create_searchconf = SearchConfigItem.create_if_needed create_searchconf(Act, ['name', 'expected_sales', 'cost', 'goal']) create_searchconf(Strategy, ['name']) create_searchconf(ActObjectivePattern, [], disabled=True) # --------------------------- SettingValue.objects.get_or_create(key_id=setting_keys.orga_approaches_key.id, defaults={'value': True}) # --------------------------- Job.objects.get_or_create(type_id=creme_jobs.com_approaches_emails_send_type.id, defaults={'language': settings.LANGUAGE_CODE, 'periodicity': date_period_registry.get_period('days', 1), 'status': Job.STATUS_OK, } ) # --------------------------- if not already_populated: ButtonMenuItem.create_if_needed(pk='commercial-complete_goal_button', model=None, button=buttons.CompleteGoalButton, order=60, ) create_bdl = BrickDetailviewLocation.create_if_needed TOP = BrickDetailviewLocation.TOP RIGHT = BrickDetailviewLocation.RIGHT LEFT = BrickDetailviewLocation.LEFT create_bdl(brick_id=bricks.ApproachesBrick.id_, order=10, zone=RIGHT) create_bdl(brick_id=bricks.ApproachesBrick.id_, order=10, zone=RIGHT, model=Contact) create_bdl(brick_id=bricks.ApproachesBrick.id_, order=10, zone=RIGHT, model=Organisation) BrickDetailviewLocation.create_4_model_brick( order=5, zone=LEFT, model=Act) create_bdl(brick_id=bricks.ActObjectivesBrick.id_, order=10, zone=LEFT, model=Act) create_bdl(brick_id=bricks.RelatedOpportunitiesBrick.id_, order=20, zone=LEFT, model=Act) create_bdl(brick_id=core_bricks.CustomFieldsBrick.id_, order=40, zone=LEFT, model=Act) create_bdl(brick_id=core_bricks.PropertiesBrick.id_, order=450, zone=LEFT, model=Act) create_bdl(brick_id=core_bricks.RelationsBrick.id_, order=500, zone=LEFT, model=Act) create_bdl(brick_id=core_bricks.HistoryBrick.id_, order=20, zone=RIGHT, model=Act) create_bdl(brick_id=bricks.PatternComponentsBrick.id_, order=10, zone=TOP, model=ActObjectivePattern) BrickDetailviewLocation.create_4_model_brick( order=5, zone=LEFT, model=ActObjectivePattern) create_bdl(brick_id=core_bricks.CustomFieldsBrick.id_, order=40, zone=LEFT, model=ActObjectivePattern) create_bdl(brick_id=core_bricks.PropertiesBrick.id_, order=450, zone=LEFT, model=ActObjectivePattern) create_bdl(brick_id=core_bricks.RelationsBrick.id_, order=500, zone=LEFT, model=ActObjectivePattern) create_bdl(brick_id=core_bricks.HistoryBrick.id_, order=20, zone=RIGHT, model=ActObjectivePattern) create_bdl(brick_id=bricks.SegmentDescriptionsBrick.id_, order=10, zone=TOP, model=Strategy) BrickDetailviewLocation.create_4_model_brick( order=5, zone=LEFT, model=Strategy) create_bdl(brick_id=core_bricks.CustomFieldsBrick.id_, order=40, zone=LEFT, model=Strategy) create_bdl(brick_id=bricks.EvaluatedOrgasBrick.id_, order=50, zone=LEFT, model=Strategy) create_bdl(brick_id=bricks.AssetsBrick.id_, order=60, zone=LEFT, model=Strategy) create_bdl(brick_id=bricks.CharmsBrick.id_, order=70, zone=LEFT, model=Strategy) create_bdl(brick_id=core_bricks.PropertiesBrick.id_, order=450, zone=LEFT, model=Strategy) create_bdl(brick_id=core_bricks.RelationsBrick.id_, order=500, zone=LEFT, model=Strategy) create_bdl(brick_id=core_bricks.HistoryBrick.id_, order=20, zone=RIGHT, model=Strategy) if apps.is_installed('creme.assistants'): logger.info('Assistants app is installed => we use the assistants blocks on detail views') from creme.assistants import bricks as assistants_bricks for model in (Act, ActObjectivePattern, Strategy): create_bdl(brick_id=assistants_bricks.TodosBrick.id_, order=100, zone=RIGHT, model=model) create_bdl(brick_id=assistants_bricks.MemosBrick.id_, order=200, zone=RIGHT, model=model) create_bdl(brick_id=assistants_bricks.AlertsBrick.id_, order=300, zone=RIGHT, model=model) create_bdl(brick_id=assistants_bricks.UserMessagesBrick.id_, order=400, zone=RIGHT, model=model) if apps.is_installed('creme.documents'): # logger.info("Documents app is installed => we use the documents blocks on Strategy's detail views") from creme.documents.bricks import LinkedDocsBrick for model in (Act, ActObjectivePattern, Strategy): create_bdl(brick_id=LinkedDocsBrick.id_, order=600, zone=RIGHT, model=model)