Esempio n. 1
0
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}
Esempio n. 2
0
 obj_id = ObjectId(request.matchdict.get('contact_id'))
 action = request.route_url('edit', contact_id=obj_id)
 form = make_edit_form(request, action)
 try:
     data = form.validate(request.POST.items())
 except ValidationFailure, e:
     return view(request, e)
 data['_id'] = obj_id
 data['text'] = get_text(data)
 if data['coord']:
     data['coord'] = map(float_or_none, data['coord'].split(','))
 else:
     data['coord'] = []
 data.pop('csrf_token')
 new_links = data.pop('links')
 if new_links != get_linked_contact_ids(request.db, obj_id):
     link_ids = [l['_id'] for l in request.db.links.find(
             {'$or': ({'from': obj_id}, {'to': obj_id})})]
     request.db.links.remove({'_id': {'$in': link_ids}}, safe=True)
     add_links(request.db, obj_id, new_links)
 contact = request.db.contacts.find_one({'_id': obj_id})
 if addresses_differ(contact, data):
     if has_address:
         geocode(str(obj_id), request.registry.settings)
     else:
         data['coord'] = []
 request.db.contacts.update({'_id': obj_id}, data, safe=True)
 msg = _('Your changes have been saved.')
 request.session.flash(msg, 'success')
 url = request.route_url('view', contact_id=str(obj_id))
 return HTTPSeeOther(url)