def encode_contact(contact, login_user, include_attic=False, is_admin=False): """Encodes Contact data for export and returns a python data structure of dictionaries and lists. The function takes into account the access rights and encodes only elements which the user is allowed to see. me is set to the user's own database entry (or None if not logged in or not in the DB) signed_in is set to True if the user is signed in If attic=True, data will include the complete history and also archived data. """ logging.debug("encode contact name: %s" % (contact.name)) res = {} # do only enclose non-attic contacts unless attic parameter is set if contact.attic and not include_attic: return {} res['name'] = contact.name if not login_user: # the name is all which anonymous users will see return res if contact.class_name() == "Person": if contact.lastname: res['lastname'] = contact.lastname # In order to reveal more data, we must check if 'me' is allowed # to see it. visible = visible_contacts(login_user, include_attic) if not (contact.key() in visible or is_admin): return res if contact.class_name() == "Person": if contact.nickname: res['nickname'] = contact.nickname if contact.birthday.has_year() or contact.birthday.has_month() or contact.birthday.has_day(): res['birthday'] = "%04d-%02d-%02d" % (contact.birthday.year,contact.birthday.month,contact.birthday.day) elif contact.class_name() == "Company": # nothing to do pass else: assert True, "Invalid class name: %s" % contact.class_name() res['attic'] = contact.attic res['key'] = str(contact.key()) res['type'] = contact.class_name().lower() res['timestamp'] = contact.timestamp.isoformat() # google account res['owned_by'] = {'nickname': contact.owned_by.user.nickname(), 'email': contact.owned_by.user.email(), 'user_id': contact.owned_by.user.user_id(), 'federated_identity': contact.owned_by.user.federated_identity(), 'federated_provider': contact.owned_by.user.federated_provider()} # references other contact if contact.relation: res['introduction'] = contact.relation if middleman_ref: res['middleman_ref'] = contact.middleman_ref # takes care of the different take2 object structures res.update(encode_take2(contact, include_attic)) return res
def get(self): login_user = get_login_user() format = self.request.get("format", "JSON") if format not in ['JSON','yaml']: logging.Critical("Unknown format for export: %s" % (format)) self.error(500) return # not logged in if not login_user: self.redirect('/login') return if self.request.get('attic',"") == 'True': attic = True else: attic = False # shall a specific dataset be exported? key = self.request.get("key", None) logging.info("export format: attic: %d user: %s admin: %d" % (attic,user.nickname(),users.is_current_user_admin())) self.response.headers['Content-Type'] = 'text/plain' # Administrator exports everything contacts = [] if users.is_current_user_admin(): if key: con = Contact.get(key) if con: contacts.append(encode_contact(con, include_attic=attic, signed_in=True, is_admin=True)) else: q_con = Contact.all() for con in q_con: contacts.append(encode_contact(con, include_attic=attic, signed_in=True, is_admin=True)) else: login_user = get_login_user(user) if key: con = Contact.get(key) if con: contacts.append(encode_contact(con, include_attic=attic, signed_in=True, is_admin=True)) else: # export everything this user can see for ckey in visible_contacts(login_user, include_attic=attic): con = Contact.get(ckey) contacts.append(encode_contact(con, include_attic=attic, me=login_user.me)) self.response.headers['Content-Disposition'] = "attachment; filename=address_export.json" if format == 'JSON': self.response.headers['Content-Type'] = "text/plain" self.response.out.write(json.dumps(contacts,indent=2)) else: self.response.headers['Content-Type'] = "text/yaml" self.response.out.write(yaml.dump(contacts,))