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 check_zoho_ticket(foia): """Check if an open zoho ticket exists for this FOIA""" response = zoho_get( "tickets/search", { "limit": 1, "channel": "Web", "category": "Flag", "subject": "FOIAOnline*", "status": "Open", "_all": "{}-{}".format(foia.slug, foia.pk), }, ) return response.status_code == requests.codes.ok
def foiaonline_autologin(): """Automatically login to all open FOIAOnline accounts once every 2 weeks to keep accounts active """ bad_msg = 'Either the email address or password is invalid. Please try again.' good_msg = 'Your session has been extended for 30 more minutes.' lock_msg = 'Your account has been locked, please contact the FOIAonline Help Desk.' # login based on the day number mod 14, so all requests get logged into once # every 14 days without needing to store any additional state mod_filter = (date.today() - date.fromordinal(1)).days % 14 foias = FOIARequest.objects.annotate(mod_id=F('id') % 14).filter( status__in=[ 'ack', 'processed', 'appealing', 'fix', 'payment', 'lawsuit' ], portal__type='foiaonline', mod_id=mod_filter, ) def create_flag(foia, msg): """Create a flag and log a warning for a failed autologin attempt""" logger.warn( 'FOIAOnline autologin: request %s - %s', foia.pk, msg, ) FlaggedTask.objects.create( text='FOIAOnline autologin failed: {}'.format(msg), foia=foia, category='foiaonline', ) for foia in foias: # check if there is an open ticket response = zoho_get( 'tickets/search', { 'limit': 1, 'channel': 'Web', 'category': 'Flag', 'subject': 'FOIAOnline*', 'status': 'Open', '_all': '{}-{}'.format(foia.slug, foia.pk), }) if response.status_code == requests.codes.ok: # found an existing open ticket logger.info( 'FOIAOnline autologin: request %s has an open zoho ticket, not logging in', foia.pk) continue logger.info('FOIAOnline autologin: Logging in for request %s', foia.pk) if not foia.portal_password: create_flag(foia, 'no portal password set') continue response = requests.post( 'https://foiaonline.gov/foiaonline/perform_login', data={ 'username': foia.get_request_email(), 'password': foia.portal_password, }) if bad_msg in response.content: create_flag(foia, 'bad password') elif lock_msg in response.content: create_flag(foia, 'account locked') elif good_msg not in response.content: create_flag(foia, 'unknown') else: logger.info('FOIAOnline autologin: request %s login succeeded', foia.pk)