def fetch_contacts(): email = request.args.get('email') access_token = request.args.get('access_token') user = User.get_by_id(email) headers = {'Authorization': 'Bearer {}'.format(access_token)} req_uri = 'https://www.google.com/m8/feeds/contacts/default/full?alt=json&max-results=25000&v=3.0' r = urlfetch.fetch(req_uri, headers=headers, method='GET') data = json.loads(r.content) contact_list = data.get('feed', {}).get('entry', []) # List ndb.delete_multi( Contacts.query(Contacts.owner == email).fetch(keys_only=True)) for contact in contact_list: if ('gd$phoneNumber' not in contact): continue name = contact.get('gd$name', {}) \ .get('gd$fullName', {}) \ .get('$t', '') numbers = [ number.get('$t', '') for number in contact.get('gd$phoneNumber', []) ] Contacts.add_contact(email, name, numbers) user.import_status = "imported" user.put() return "", 200
def list_contacts(): session_id = request.cookies.get('sessionID') if not session_id or session_id == '': return jsonify(dict(success=False, error='unauthorized')) user = Session.get_by_id(session_id) if not user: return jsonify(dict(success=False, error='unauthorized')) email = user.email # logging.info('email : {}'.format(email)) cursor = request.args.get('cursor') if not cursor: cursor = None cursor = Cursor(urlsafe=cursor) query = Contacts.query(Contacts.owner == email).order(Contacts.name_lc) contacts, next_cursor, more = query.fetch_page(10, start_cursor=cursor) # logging.info('cursor: {} more: {}'.format(next_cursor, more)) if next_cursor: cursor = next_cursor.urlsafe() else: cursor = None data = [contact.to_dict() for contact in contacts] return jsonify({ 'cursor': cursor, 'more': more, 'contacts': data, 'success': True })
def contacts_list(self): try: contactsList = Contacts.select().order_by(Contacts.name) self.privData['CONTACTS_LIST'] = contactsList return self.display('contacts-list') except Exception, e: return self.error(msg='获取通讯录列表失败!')
def sms(): _from = request.values.get('from',None) #from means the person from whom the text is coming from. text = request.values.get('text',None) #this gets the text in the message sent by user split_text = text.split('*') name = split_text[0].capitalize() age = split_text[1] gender = split_text[2].lower() count = Contacts.query.filter_by(phoneNumber = _from).count() if not count > 0: #no idea what api key is, what it does. Also, the use of the username is a mystery. #username is your Africa'sTalking username and api key is obtained from website gateway = AfricasTalkingGateway(os.environ.get('username'), os.environ.get('apikey')) gateway.sendMessage (_from, "Thank you for registering for this service. To get inspiration messages, call 20880. Calls charged at 10 bob per minute. Have a blessed day.") #here the details are added to db table contacts contacts = Contacts (name=name, age = age, phoneNumber = _from) db.session.add(contacts) db.session.commit() logging.info("user added{}".format(contacts)) else: logging.info("User already registered.") resp = make_response ("OK", 200) return resp
def delete(self): inputParams = self.getInput() contact_id = int(inputParams['id']) try: contact = Contacts.get(Contacts.id == contact_id) contact.delete_instance() except Exception, e: return self.success(msg='删除失败: %s' % e, url=self.makeUrl('/admin/contacts/list'))
def save(self): inputParams = self.getInput() try: Contacts.create( cellphone = inputParams['cellphone'], email = inputParams['email'], name = inputParams['name'], sn = inputParams['sn'], weixin = inputParams['weixin'], title = inputParams['title'], address = inputParams['address'], avatur = int(inputParams['thumbnail']), gender = int(inputParams['gender']), description = inputParams['desc'], ) except Exception, e: return self.error(msg = '保存失败: %s' % e, url=self.makeUrl('/admin/contacts/list'))
def edit(self): inputParams = self.getInput() imagesList = Images().select() if not imagesList.count(): return self.error(msg = '请创建至少一个图片!', url=self.makeUrl('/admin/images/list')) try: contactObj = Contacts.get(Contacts.id == int(inputParams['id'])) except Exception, e: return self.error(msg = '对象不存在: %s' % e, url=self.makeUrl('/admin/contacts/list'))
def contact_details(self): inputParams = self.getInput() try: contact = Contacts.get(Contacts.id == int(inputParams['id'])) contact.description = self.htmlunescape(contact.description) self.privData['CONTACT_DETAILS'] = contact return self.display('contact-details') except Exception, e: print e return self.error(msg='获取联系人详情失败!')
def modify(self): inputParams= self.getInput() try: contactObj = Contacts.get(Contacts.id == int(inputParams['id'])) contactObj.email = inputParams['email'] contactObj.name = inputParams['name'] contactObj.sn = inputParams['sn'] contactObj.weixin = inputParams['weixin'] contactObj.title = inputParams['title'] contactObj.address = inputParams['address'] contactObj.cellphone = inputParams['cellphone'] contactObj.description = inputParams['desc'] contactObj.gender = int(inputParams['gender']) contactObj.avatur = int(inputParams['thumbnail']) contactObj.save() except Exception, e: return self.error(msg = '修改失败: %s' % e, url=self.makeUrl('/admin/contacts/list'))