def get_item_value(self, resource, context, item, column, cache={}): item_brain, item_resource = item if column == 'title': value = get_name(item_brain) href = '%s/' % context.get_link(item_resource) return value, href elif column == 'company': p_company = item_brain.crm_p_company if not p_company: return u'' crm = get_crm(resource) company = crm.get_resource('companies/' + p_company) href = context.get_link(company) title = company.get_title() return title, href elif column == 'email': value = item_brain.crm_p_email href = 'mailto:%s' % value return value, href elif column == 'phones': return Phones(item_brain, 'crm_p_phone', 'crm_p_mobile') elif column == 'crm_p_assured': value = item_brain.crm_p_assured return format_amount(value, context) elif column == 'crm_p_probable': value = item_brain.crm_p_probable return format_amount(value, context) elif column.startswith('crm_m_'): # CSV export contact_name = item_brain.name mission_brain = cache.get(contact_name) if mission_brain is None: crm = get_crm(resource) query = AndQuery( get_crm_path_query(crm), PhraseQuery('format', 'mission'), PhraseQuery('crm_m_contact', item_brain.name)) results = context.root.search(query) last_missions = results.get_documents(sort_by='mtime', reverse=True, size=1) if not last_missions: return None mission_brain = cache[contact_name] = last_missions[0] if column == 'crm_m_title': column = 'title' return getattr(mission_brain, column) proxy = super(CRM_SearchContacts, self) return proxy.get_item_value(resource, context, item, column, cache=cache)
def get_items(self, resource, context, *args): # Get the parameters from the query query = context.query search_text = query['search_text'].strip() field = query['search_type'] m_status = query['crm_m_status'] # Build the query args = list(args) args.append(PhraseQuery('format', 'mission')) args.append(PhraseQuery('crm_m_contact', resource.name)) missions = resource.parent.parent.get_resource('missions') abspath = str(missions.get_canonical_path()) args.append(PhraseQuery('parent_path', abspath)) if search_text: args.append(PhraseQuery(field, search_text)) # Insert status filter if m_status: status_query = [] for s in m_status: status_query.append(PhraseQuery('crm_m_status', s)) args.append(OrQuery(*status_query)) if len(args) == 1: query = args[0] else: query = AndQuery(*args) # Ok crm = get_crm(resource) base_path_query = get_crm_path_query(crm) return context.root.search(AndQuery(query, base_path_query))
def get_default_company(self, resource, context): m_contact = resource.get_property('crm_m_contact') if not m_contact: return None crm = get_crm(resource) contact = crm.get_resource('contacts/' + m_contact[0]) p_company = contact.get_property('crm_p_company') return crm.get_resource('companies/' + p_company)
def action(self, resource, context, form): company = resource.add_company() Company_EditForm.action(self, company, context, form) if is_thingy(context.message, ERROR): return crm = get_crm(resource) goto = "%s/contacts/;new_contact?crm_p_company=%s" % (context.get_link(crm), company.name) return context.come_back(MSG_NEW_COMPANY, goto)
def get_items(self, resource, context, *args): # Build the query args = list(args) args.append(PhraseQuery('crm_m_contact', resource.name)) if len(args) == 1: query = args[0] else: query = AndQuery(*args) # Ok crm = get_crm(resource) base_path_query = get_crm_path_query(crm) return context.root.search(AndQuery(query, base_path_query))
def get_options(cls): context = get_context() crm = get_crm(context.resource) parent_path = '%s/contacts' % crm.get_abspath() root = context.root results = root.search(format='contact', parent_path=parent_path) options = [] for brain in results.get_documents(sort_by='crm_p_lastname'): options.append({ 'name': brain.name, 'value': brain.title}) return options
def send_notification(resource, context, form, changes, new=False): # From user = context.user user_title = user.get_title() user_email = user.get_property('email') # To to_addrs = set(form['crm_m_cc']) if form['crm_m_assigned']: to_addrs.add(form['crm_m_assigned']) # Except sender if user.name in to_addrs: to_addrs.remove(user.name) if not to_addrs: return # Subject crm_title = get_crm(resource).get_title() or u"CRM" mission_name = resource.name mission_title = resource.get_property('title') subject = u"[%s #%s] %s" % (crm_title, mission_name, mission_title) # Body mission_uri = context.uri.resolve(context.get_link(resource)) # Changes changes.insert(0, u"-" * 76) header = CHANGES_LINE.gettext(what=u"What", removed=u"Removed", added=u"Added") changes.insert(0, header) changes = u"\n".join(changes) # New comment comment = u"" if form.get('comment'): n = len(resource.get_property('comment')) - 1 now = context.format_datetime(context.timestamp) comment = [COMMENT_LINE.gettext(n=n, user_title=user_title, user_email=user_email, date=now)] comment_value = form['comment'] if type(comment_value) is Property: comment_value = comment_value.value comment.extend(comment_value.splitlines()) comment = u"\n".join(comment) body = BODY.gettext(mission_uri=mission_uri, mission_name=mission_name, mission_title=mission_title, user_title=user_title, user_email=user_email, changes=changes, comment=comment) # Send root = context.root for to_addr in to_addrs: user = root.get_user(to_addr) if not user: continue to_addr = user.get_property('email') root.send_email(to_addr, subject, text=body)
def action_add_contact(self, resource, context, form): # Save changes m_contact = resource.get_property('crm_m_contact') m_contact = list(set(m_contact + form['ids'])) resource.set_property('crm_m_contact', m_contact) # Reindex contacts so they know about the mission crm = get_crm(resource) contacts = crm.get_resource('contacts') for contact_id in form['ids']: contact = contacts.get_resource(contact_id) context.database.change_resource(contact) context.message = MSG_CONTACT_ADDED
def _get_query(self, resource, context, *args): crm = get_crm(resource) search_term = context.query['search_term'].strip() tags = context.query['tags'] # Build the query args = list(args) args.append(PhraseQuery('format', self.search_format)) args.append(get_crm_path_query(crm)) if search_term: args.append(PhraseQuery('text', search_term)) if tags: args.append(PhraseQuery('tags', tags)) return AndQuery(*args)
def action(self, resource, context, form, new=False): # First compute differences changes = get_changes(resource, context, form, new=new) # Save changes super(Mission_EditForm, self).action(resource, context, form) if is_thingy(context.message, ERROR): return # Reindex contacts to update Opp/Proj/NoGo, p_assured and p_probable crm = get_crm(resource) contacts = crm.get_resource('contacts') for contact_id in resource.get_property('crm_m_contact'): contact = contacts.get_resource(contact_id) context.database.change_resource(contact) # Send notification to CC send_notification(resource, context, form, changes, new=new)
def action(self, resource, context, form): crm = get_crm(resource) contacts = crm.get_resource('contacts') missions = crm.get_resource('missions') # Split values contact/mission p_values = {} m_values = {} for key, value in form.iteritems(): if key.startswith('crm_p_'): p_values[key] = value elif key.startswith('crm_m_'): m_values[key] = value elif key.startswith('mission_'): m_values[key[8:]] = value # Add contact contact = contacts.add_contact(**p_values) # Add mission if title is defined language = resource.get_edit_languages(context)[0] if form['mission'] or m_values['title'][language]: if form['mission']: # Link to existing mission missions = resource.get_resource('../missions') mission = missions.get_resource(form['mission']) m_contact = mission.get_property('crm_m_contact') m_contact = list(set(m_contact + [contact.name])) mission.set_property('crm_m_contact', m_contact) #changes = get_changes(mission, context, m_values) #send_notification(mission, context, m_values, changes) elif m_values['title'][language]: # Create new mission and link to it m_values['crm_m_contact'] = contact.name if m_values['comment']: m_values['comment'] = Property(m_values['comment'], date=context.timestamp, author=context.user.name) mission = missions.add_mission(**m_values) changes = get_changes(mission, context, m_values, new=True) send_notification(mission, context, m_values, changes, new=True) goto = context.get_link(mission) return context.come_back(MSG_CONTACT_ADDED, goto=goto) else: goto = context.get_link(contact) return context.come_back(MSG_NEW_RESOURCE, goto=goto)
def get_options(cls): context = get_context() crm = get_crm(context.resource) parent_path = '%s/companies' % crm.get_abspath() root = context.root results = root.search(format='company', parent_path=parent_path) options = [] # FIXME catalog case-independant sort for brain in results.get_documents(): value = brain.title # Reduce the length of the title if len(value) > 63: value = '%s...%s' % (value[:30], value[-30:]) options.append({ 'name': brain.name, 'value': value, 'sort_value': value.lower().translate(transmap)}) # FIXME move hack to a catalog index options.sort(key=itemgetter('sort_value')) return options
def get_item_value(self, resource, context, item, column, cache={}): item_brain, item_resource = item if column == 'checkbox': parent = item_resource.parent if parent is None: return None if item_resource.name in parent.__fixed_handlers__: return None id = resource.get_canonical_path().get_pathto(item_brain.abspath) id = str(id) return id, False elif column == 'alert': return get_alert_icon(item_brain.crm_m_alert, context) elif column == 'crm_m_alert': alert = item_brain.crm_m_alert if alert: return alert.date() return None elif column == 'status': # Status return ShortStatusIcon(item_brain.crm_m_status) elif column in ('contacts', 'contacts_csv'): m_contacts = item_brain.crm_m_contact query = [PhraseQuery('name', name) for name in m_contacts] if len(query) == 1: query = query[0] else: query = OrQuery(*query) crm = get_crm(resource) query = AndQuery(get_crm_path_query(crm), query) query = AndQuery(PhraseQuery('format', 'contact'), query) results = context.root.search(query) if column == 'contacts': pattern = u'<a href="{link}">{lastname}<br/>{firstname}</a>' else: pattern = u"{lastname} {firstname}" names = [] for brain in results.get_documents(sort_by='crm_p_lastname'): link = context.get_link(brain) lastname = brain.crm_p_lastname.upper() firstname = brain.crm_p_firstname names.append(pattern.format(link=link, lastname=lastname, firstname=firstname)) if column == 'contacts': return MSG(u"<br/>".join(names), format='html') return u"\n".join(names) elif column == 'company': contact_id = item_brain.crm_m_contact[0] contact = resource.get_resource('contacts/' + contact_id) p_company = contact.get_property('crm_p_company') if not p_company: return u"" company = resource.get_resource('companies/' + p_company) title = company.get_title() href = context.get_link(company) return title, href elif column == 'assigned': user_id = item_brain.crm_m_assigned return context.root.get_user_title(user_id) return super(CRM_SearchMissions, self).get_item_value(resource, context, item, column, cache=cache)
def get_crm_path_query(self, context): """Shortcut. """ return get_crm_path_query(get_crm(context.resource))