def put(self): """PUT endpoint that register a new user. Returns dict Dictionary containing error or success key. int Response Code """ token = request.headers.get("authorization-code") if token is not None: data = request.get_json(force=True) id = data.get("user_id", None) name = data.get("user_name", None) if id is None or name is None: return ( { "error": "Some body arguments is missing. It needs id, email and name" }, 463, ) user = User(id=id, name=name, query_id=id) auxapi = ContactApi() contacts = auxapi._get_list_of_contacts(grouped=False) if contacts[1] == 200: contacts_statistics = Contact.get_quantity_per_domain( contacts[0]) contacts_statistics_organization = Contact.get_quantity_per_organization( contacts[0]) contacts_statistics_jobtitle = Contact.get_quantity_per_job( contacts[0]) contacts_statistics_city = Contact.get_quantity_per_city( contacts[0]) contacts_statistics_region = Contact.get_quantity_per_region( contacts[0]) user.contacts_statistics = { "domain": contacts_statistics["contacts"], "organization": contacts_statistics_organization["contacts"], "jobtitle": contacts_statistics_jobtitle["contacts"], "city": contacts_statistics_city["contacts"], "region": contacts_statistics_region["contacts"], } user.save() return {"success": "User Saved"}, 200 return contacts else: return NO_AUTH_CODE
def _get_specific_contact(self, personId): token = request.headers.get("authorization-code") if token is not None: authorization_header = {"Authorization": "Bearer %s" % token} r = requests.get( "https://people.googleapis.com/v1/people/{contact_id}?personFields={query}".format( contact_id=personId, query="birthdays,addresses,organizations", ), headers=authorization_header, ) json_data = json.loads(r.text) if json_data.get("error", None) is not None: if json_data["error"]["code"] == 401: return INVALID_CREDENTIALS else: # Outros tipos de erros return json_data, 462 json_data = Contact._get_specific_contact_informations(json_data) return json_data, 200 else: return NO_AUTH_CODE
def init_database(): # Create the database and the database table db.create_all( ) # Insert user data contact = Contact(email='*****@*****.**', full_name='Pat Kennedy') contact2 = Contact(email='*****@*****.**', full_name='Kennedy Family') db.session.add(contact) db.session.add(contact2) # Commit the changes for the users db.session.commit( ) yield db # this is where the testing happens! db.drop_all()
def test_create_contact(): """ GIVEN a Contact model WHEN a new Contact is created THEN check the email, full_name fields are defined correctly """ new_contact = Contact(full_name='Test User', email='*****@*****.**') # assert new_contact.email == '*****@*****.**' assert new_contact.full_name != ''
def add_contacts_to_campaign(id): campaign = get_campaign_by_id(id) contacts = get_campaign_contact_params_from_request() for c in contacts: new_contact = Contact(email=c['email'], full_name=c['full_name']) new_contact = save_contact_to_db(new_contact, False) flush_db() crc = ContactRelCampaign(id, new_contact.id) add_contacts_to_campaign_to_db(crc) commit_db() return campaign_schema.jsonify(campaign)
def create_contact(): full_name, email = get_params_from_request() is_validation_succeed, err_msg = validate_contact(full_name, email) if not is_validation_succeed: return generate_error_response(err_msg) new_contact = Contact(full_name, email) new_contact = save_contact_to_db(new_contact) if not new_contact: return generate_error_response("Error: An error occured") return contact_schema.jsonify(new_contact)
def _get_list_of_contacts(self, grouped=True): token = request.headers.get("authorization-code") if token is not None: authorization_header = {"Authorization": "Bearer %s" % token} r = requests.get( "https://people.googleapis.com/v1/people/me/connections?personFields=names,emailAddresses,photos,addresses,organizations", headers=authorization_header, ) json_data = json.loads(r.text) if json_data.get("error", None) is not None: if json_data["error"]["code"] == 401: return INVALID_CREDENTIALS else: # Outros tipos de erros return json_data, 462 else: # Processar os dados e transformar em algo simples p/ front objects = Contact.multiples_json_contacts_to_objects(json_data) if grouped: grouped_by_domains = Contact.group_by_email_group(objects) sorted( grouped_by_domains, key=lambda k: len(grouped_by_domains[k]), reverse=False, ) return (grouped_by_domains, 200) else: print(objects) return (objects, 200) else: return NO_AUTH_CODE
def new_contact(): contact = Contact('Sercan Cicek', '*****@*****.**') return contact