def get_contact_id(self, user): """Get a zoho contact id for the contact with the given email address""" if user is None: user = User.objects.get(username='******') response = zoho_get( 'contacts/search', params={ 'limit': 1, 'email': user.email, }, ) response.raise_for_status() if response.status_code == 200: contacts = response.json() if contacts['count'] > 0: return contacts['data'][0]['id'] # if we could not find an existing contact, we will create one response = zoho_post( 'contacts', json={ 'lastName': user.profile.full_name or 'Anonymous', # lastName is required 'email': user.email, 'customFields': { 'username': user.username }, }, ) response.raise_for_status() if response.status_code == 200: return response.json()['id']
def get_contact_id(self, user): """Get a zoho contact id for the contact with the given email address""" if user is None or not user.email: user = User.objects.get(username="******") response = zoho_get("contacts/search", params={"limit": 1, "email": user.email}) response.raise_for_status() if response.status_code == 200: contacts = response.json() if contacts["count"] > 0: return contacts["data"][0]["id"] # if we could not find an existing contact, we will create one response = zoho_post( "contacts", json={ "lastName": user.profile.full_name or "Anonymous", # lastName is required "email": user.email, "customFields": {"username": user.username}, }, ) response.raise_for_status() if response.status_code == 200: return response.json()["id"] else: return None
def create_zoho_ticket(self): """Create a Zoho ticket""" def make_url(obj): """Make a URL""" if obj is None: return "" else: return '<p><a href="{}{}" target="_blank">{}</a></p>'.format( settings.MUCKROCK_URL, obj.get_absolute_url(), obj ) contact_id = self.get_contact_id(self.user) if contact_id is None: return None description = bleach.clean(self.text) subject = description[:50] or "-No Subject-" description = linebreaks(urlize(description)) description += make_url(self.foia) description += make_url(self.agency) description += make_url(self.jurisdiction) email = ( self.user.email if self.user and self.user.email else settings.DEFAULT_FROM_EMAIL ) response = zoho_post( "tickets", json={ "subject": subject, "departmentId": settings.ZOHO_DEPT_IDS["muckrock"], "contactId": contact_id, "email": email, "description": description, "channel": "Web", "category": "Flag", "subCategory": self.get_category_display(), }, ) response.raise_for_status() if response.status_code == 200: return response.json()["id"] else: return None
def create_zoho_ticket(self): """Create a Zoho ticket""" def make_url(obj): """Make a URL""" if obj is None: return '' else: return ( u'<p><a href="{}{}" target="_blank">{}</a></p>'.format( settings.MUCKROCK_URL, obj.get_absolute_url(), obj, ) ) contact_id = self.get_contact_id(self.user) if contact_id is None: return None description = bleach.clean(self.text) subject = description[:50] or u'-No Subject-' description = linebreaks(urlize(description)) description += make_url(self.foia) description += make_url(self.agency) description += make_url(self.jurisdiction) response = zoho_post( 'tickets', json={ 'subject': subject, 'departmentId': settings.ZOHO_DEPT_IDS['muckrock'], 'contactId': contact_id, 'email': self.user.email if self.user else '*****@*****.**', 'description': description, 'channel': 'Web', 'category': 'Flag', 'subCategory': self.get_category_display(), } ) response.raise_for_status() if response.status_code == 200: return response.json()['id']