def view(request, form=None): obj_id = ObjectId(request.matchdict.get('contact_id')) contact = request.db.contacts.find_one({'_id': obj_id}) full_name = get_full_name(contact) birthdate = get_birthdate(contact, request) links = {} linked_contacts = [] for linked_id in get_linked_contact_ids(request.db, obj_id): linked_contact = request.db.contacts.find_one({'_id': linked_id}) linked_contacts.append(linked_contact) links[str(linked_id)] = get_full_name(linked_contact) if form is None: action = request.route_url('edit', contact_id=obj_id) contact['links'] = links.keys() form_data = contact.copy() form_data['coord'] = ', '.join(map(str, form_data['coord'])) form = make_edit_form(request, action) form = form.render(form_data) edit_error = False else: form = form.render() edit_error = True label_of = lambda field_name: ContactSchema()[field_name].title return {'api': TemplateAPI(request, full_name), 'contact': contact, 'get_full_name': get_full_name, 'birthdate': birthdate, 'links': linked_contacts, 'form': form, 'edit_error': edit_error, 'label_of': label_of}
def export(request): mode = request.POST.get('mode') if mode == 'partial': attrs = request.POST.getall('attributes') else: attrs = _replace_by_composites(_get_contact_attributes()) attrs = [a['name'] for a in attrs] contacts = defaultdict(list) full_names = {} sort_args = [('last_name', 1), ('first_name', 1)] for contact in request.db.contacts.find().sort(sort_args): full_names[contact['_id']] = get_full_name(contact) initial = get_initial(contact['last_name']) contacts[initial.upper()].append(contact) links = defaultdict(set) for link in request.db.links.find(): links[link['from']].add(full_names[link['to']]) links[link['to']].add(full_names[link['from']]) css_url = request.static_url('mnemos:static/css/export.css') return {'css_url': css_url, 'contacts': contacts, 'links': links, 'attributes': attrs, 'get_full_name': get_full_name, 'get_birthdate': get_birthdate}
def contacts_in_bbox(request): """Return a set of contacts who lives in the given bounding box. Called by JavaScript to show contacts on the map. """ bbox = request.GET.get('bbox') sw_lng, sw_lat, ne_lng, ne_lat = map(float, bbox.split(',')) localizer = get_localizer(request) zoom_in_label = localizer.translate(_('zoom in')) features = [] for contact in request.db.contacts.find( {'coord': {'$within': {'$box': [[sw_lng, sw_lat], [ne_lng, ne_lat]]}}}): url = request.route_url('view', contact_id=contact['_id']) features.append({ 'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': contact['coord']}, 'properties': {'fullname': get_full_name(contact), 'url': url, 'lng': contact['coord'][0], 'lat': contact['coord'][1], 'zoom_in_label': zoom_in_label} }) return {'type': 'FeatureCollection', 'features': features}
def display_value(field, cstruct): if not cstruct: return cstruct obj_id = ObjectId(cstruct) contact = request.db.contacts.find_one({'_id': obj_id}) if not contact: return '' return get_full_name(contact)
def ajax_search(request): term = request.GET['term'].lower() res = [] for contact in request.db.contacts.find({'text': {'$regex': term}}): url = request.route_url('view', contact_id=contact['_id']) fullname = get_full_name(contact) dropdown = highlight_term(term, fullname) res.append({'url': url, 'dropdown': dropdown, 'displayed': fullname, 'stored': str(contact['_id'])}) return res
def birthdays(request): months = map(_, MONTHS_ABBR) by_month = defaultdict(list) contacts = request.db.contacts.find().sort('birth_day') for contact in contacts: m = contact['birth_month'] url = request.route_url('view', contact_id=contact['_id']) item = {'name': get_full_name(contact), 'url': url, 'birthdate': get_birthdate(contact, request)} if not m: by_month['unknown'].append(item) else: by_month[m].append(item) return {'api': TemplateAPI(request, _('Birthdays')), 'by_month': by_month, 'months': months}