def contact_profile(request, id): """ Sends a request to the Contact Profile page and retrieves Contact information for the profile. Arguments: id (string): The id of the contact. Returns: A rendered page of the Contact Profile. """ user_id = request.session['user_id'] if 'user_id' in request.session else None account_type = request.session['account_type'] if 'account_type' in request.session else None logger.info('Request made for profile of contact={0} by user={1}'.format(id, user_id)) user_dao = ctx.get_object('UserDAO') try: user = user_dao.find(id=id) except: logger.error('Exception encountered on user lookup for user={0}'.format(id)) return not_found(request) if user and not user_id: logger.warn('Unauthorized request made for user={0}'.format(user.id)) return unauthorized(request) contact_dao = ctx.get_object('ContactDAO') try: contact = contact_dao.find(id=id) except: logger.error('Exception encountered on contact lookup for contact={0}'.format(id)) return not_found(request) if contact: results = contact.__dict__['_data'] elif user: results = user.__dict__['_data'] org_dao = ctx.get_object('OrganizationDAO') try: if results['organization']: org = org_dao.find(id=results['organization'].id) results['organization'] = org.__dict__['_data'] except: logger.error('Exception encountered on organization lookup for organization={0}' .format(results['organization'].id)) return not_found(request) can_edit = contact and account_type == AccountType.CONTRIBUTOR return render(request, 'contact/contact_profile.html', { 'contact': results, 'can_edit': can_edit, })
def organization_profile(request, org_id): """ Sends a request to the Organization Profile page and retrieves Organization information for the profile. Arguments: org_id (string): The id of the organization. Returns: A rendered page of the Organization Profile. """ user_id = request.session['user_id'] if 'user_id' in request.session else None account_type = request.session['account_type'] if 'account_type' in request.session else None logger.info('Request made for profile of org={0} by user={1}'.format(org_id, user_id)) org_dao = ctx.get_object('OrganizationDAO') try: org = org_dao.find(id=org_id) except: logger.error('Exception encountered on organization lookup for org={0} by user={1}'.format(org_id, user_id)) return not_found(request) scheme = "" if org.organization_url is not None: scheme = urlparse(org.organization_url).scheme type_nums = org['types'] org_types = [OrgTypesEnum.reverse_mapping[t].title() for t in type_nums if t != OrgTypesEnum.UNKNOWN] facebook_str = None if org.facebook: fb_regex = re.compile('(?:(?:http|https):\/\/)?(?:www.)?' 'facebook.com\/(?:(?:\w)*#!\/)?' '(?:pages\/)?(?:[?\w\-]*\/)?' '(?:profile.php\?id=(?=\d.*))?([\w\-]*)?') fb_match = fb_regex.match(org.facebook) facebook_str = fb_match.group(1) if fb_match else None twitter_str = "@" + org.twitter.split('/')[-1] if org.twitter else None can_edit = account_type == AccountType.CONTRIBUTOR cleaned_partners = [partner for partner in org.partners if partner.name] all_contacts = org.contacts + org.user_contacts if user_id else org.contacts params = {"organization": org, "scheme": scheme, "types": org_types, "facebook": facebook_str, "twitter": twitter_str, "can_edit": can_edit, "partners": cleaned_partners, "contacts": all_contacts, } return render(request, 'organization/organization_profile.html', params)
def edit_contact(request, contact_id): """ Sends a request to the Edit Contact page if the user is logged in and has a contributor account type. Arguments: contact_id (string): The id of the contact that is being edited. Returns: A rendered page containing the Edit Contact form. """ if 'user_id' not in request.session: logger.error('Request to edit contact={0} without login'.format(contact_id)) return unauthorized(request) else: user_id = request.session['user_id'] if 'account_type' not in request.session or request.session['account_type'] != AccountType.CONTRIBUTOR: logger.error('Request to edit contact={0} without credentials by user={1}'.format(contact_id, user_id)) return unauthorized(request) contact_dao = ctx.get_object('ContactDAO') error = '' success = '' try: contact = contact_dao.find(id=contact_id) except: logger.error('Exception encountered on contact lookup for contact={0} by user={1}'.format(contact_id, user_id)) return not_found(request) phones = contact.phones if contact.phones else [] form = EditContactForm(request.POST or None, initial=_create_contact_dict(contact), phones=phones) if request.method == 'POST': if form.is_valid(): data = form.cleaned_data new_phones = [] if 'invalid' in data: contact.valid = not data['invalid'] try: for key, value in data.items(): if key.startswith('phone'): new_phones.append(value.strip()) else: setattr(contact, key, value.strip()) if value else setattr(contact, key, None) except: error = 'Oops! Something went wrong processing your request. Please try again later.' logger.error('Error occurred while updating fields for contact={0} by user={1}'.format(contact_id, user_id)) if not error: if new_phones: contact.phones = [p for p in new_phones if p] try: contact_dao.create_update(contact) success = 'The contact has been updated successfully!' logger.info('Contact={0} updated by user={1}'.format(contact_id, user_id)) except: error = 'Oops! There was an error updating the contact. Please try again soon.' return render(request, 'contact/edit_contact.html', {'form': form, 'contact_id': contact_id, 'success': success, 'error': error})
def organization_profile(request, org_id): """ Sends a request to the Organization Profile page and retrieves Organization information for the profile. Arguments: org_id (string): The id of the organization. Returns: A rendered page of the Organization Profile. """ user_id = request.session[ 'user_id'] if 'user_id' in request.session else None account_type = request.session[ 'account_type'] if 'account_type' in request.session else None logger.info('Request made for profile of org={0} by user={1}'.format( org_id, user_id)) org_dao = ctx.get_object('OrganizationDAO') try: org = org_dao.find(id=org_id) except: logger.error( 'Exception encountered on organization lookup for org={0} by user={1}' .format(org_id, user_id)) return not_found(request) scheme = "" if org.organization_url is not None: scheme = urlparse(org.organization_url).scheme type_nums = org['types'] org_types = [ OrgTypesEnum.reverse_mapping[t].title() for t in type_nums if t != OrgTypesEnum.UNKNOWN ] facebook_str = None if org.facebook: fb_regex = re.compile('(?:(?:http|https):\/\/)?(?:www.)?' 'facebook.com\/(?:(?:\w)*#!\/)?' '(?:pages\/)?(?:[?\w\-]*\/)?' '(?:profile.php\?id=(?=\d.*))?([\w\-]*)?') fb_match = fb_regex.match(org.facebook) facebook_str = fb_match.group(1) if fb_match else None twitter_str = "@" + org.twitter.split('/')[-1] if org.twitter else None can_edit = account_type == AccountType.CONTRIBUTOR cleaned_partners = [partner for partner in org.partners if partner.name] all_contacts = org.contacts + org.user_contacts if user_id else org.contacts params = { "organization": org, "scheme": scheme, "types": org_types, "facebook": facebook_str, "twitter": twitter_str, "can_edit": can_edit, "partners": cleaned_partners, "contacts": all_contacts, } return render(request, 'organization/organization_profile.html', params)
def edit_organization(request, org_id): """ Sends a request to the Edit Organization page if the user is logged in and a contributor account type. Arguments: org_id (string): The id of the organization that is being edited. Returns: A rendered page containing the Edit Organization form. """ if 'user_id' not in request.session: logger.error( 'Request to edit organization={0} without login'.format(org_id)) return unauthorized(request) else: user_id = request.session['user_id'] if 'account_type' not in request.session or request.session[ 'account_type'] != AccountType.CONTRIBUTOR: logger.error( 'Request to edit organization={0} without credentials by user={1}' .format(org_id, user_id)) return unauthorized(request) try: dao = ctx.get_object('OrganizationDAO') org = dao.find(id=org_id) except: logger.error( 'Exception encountered on organization lookup for org={0} by user={1}' .format(org_id, user_id)) return not_found() emails = org.emails if org.emails else [] phone_numbers = org.phone_numbers if org.phone_numbers else [] types = org.types if org.types else [] form = EditOrganizationForm( request.POST or None, initial=_create_org_dict(org), emails=emails, phone_numbers=phone_numbers, types=types, ) error = '' success = '' if request.method == 'POST': if form.is_valid(): data = form.cleaned_data new_emails = [] new_phone_nums = [] new_types = [] try: for key, value in data.items(): if key.startswith('email'): new_emails.append(value.strip()) elif key.startswith('phone'): new_phone_nums.append(value.strip()) elif key.startswith('type'): new_types.append(value.strip()) else: setattr(org, key, value.strip()) if value else setattr( org, key, None) except: error = 'Oops! Something went wrong processing your request. Please try again later.' logger.error( 'Error occurred while updating fields for org={0} by user={1}' .format(org_id, user_id)) if not error: if new_emails: org.emails = [e for e in new_emails if e] if org.emails: org.email_key = org.emails[0] if new_phone_nums: org.phone_numbers = [p for p in new_phone_nums if p] if new_types: org.types = [t for t in new_types if t] try: dao.create_update(org) success = 'The organization has been updated successfully!' logger.info('Org={0} updated by user={1}'.format( org_id, user_id)) except: error = 'Oops! There was an error updating the organization. Please try again later.' logger.error( 'Error occurred saving org={0} by user={1}'.format( org_id, user_id)) return render( request, "organization/edit_organization.html", { 'form': form, 'type_choices': ORG_TYPE_CHOICES, 'org_id': org_id, 'success': success, 'error': error })
def edit_contact(request, contact_id): """ Sends a request to the Edit Contact page if the user is logged in and has a contributor account type. Arguments: contact_id (string): The id of the contact that is being edited. Returns: A rendered page containing the Edit Contact form. """ if 'user_id' not in request.session: logger.error( 'Request to edit contact={0} without login'.format(contact_id)) return unauthorized(request) else: user_id = request.session['user_id'] if 'account_type' not in request.session or request.session[ 'account_type'] != AccountType.CONTRIBUTOR: logger.error( 'Request to edit contact={0} without credentials by user={1}'. format(contact_id, user_id)) return unauthorized(request) contact_dao = ctx.get_object('ContactDAO') error = '' success = '' try: contact = contact_dao.find(id=contact_id) except: logger.error( 'Exception encountered on contact lookup for contact={0} by user={1}' .format(contact_id, user_id)) return not_found(request) phones = contact.phones if contact.phones else [] form = EditContactForm(request.POST or None, initial=_create_contact_dict(contact), phones=phones) if request.method == 'POST': if form.is_valid(): data = form.cleaned_data new_phones = [] if 'invalid' in data: contact.valid = not data['invalid'] try: for key, value in data.items(): if key.startswith('phone'): new_phones.append(value.strip()) else: setattr(contact, key, value.strip()) if value else setattr( contact, key, None) except: error = 'Oops! Something went wrong processing your request. Please try again later.' logger.error( 'Error occurred while updating fields for contact={0} by user={1}' .format(contact_id, user_id)) if not error: if new_phones: contact.phones = [p for p in new_phones if p] try: contact_dao.create_update(contact) success = 'The contact has been updated successfully!' logger.info('Contact={0} updated by user={1}'.format( contact_id, user_id)) except: error = 'Oops! There was an error updating the contact. Please try again soon.' return render(request, 'contact/edit_contact.html', { 'form': form, 'contact_id': contact_id, 'success': success, 'error': error })
def contact_profile(request, id): """ Sends a request to the Contact Profile page and retrieves Contact information for the profile. Arguments: id (string): The id of the contact. Returns: A rendered page of the Contact Profile. """ user_id = request.session[ 'user_id'] if 'user_id' in request.session else None account_type = request.session[ 'account_type'] if 'account_type' in request.session else None logger.info('Request made for profile of contact={0} by user={1}'.format( id, user_id)) user_dao = ctx.get_object('UserDAO') try: user = user_dao.find(id=id) except: logger.error( 'Exception encountered on user lookup for user={0}'.format(id)) return not_found(request) if user and not user_id: logger.warn('Unauthorized request made for user={0}'.format(user.id)) return unauthorized(request) contact_dao = ctx.get_object('ContactDAO') try: contact = contact_dao.find(id=id) except: logger.error( 'Exception encountered on contact lookup for contact={0}'.format( id)) return not_found(request) if contact: results = contact.__dict__['_data'] elif user: results = user.__dict__['_data'] org_dao = ctx.get_object('OrganizationDAO') try: if results['organization']: org = org_dao.find(id=results['organization'].id) results['organization'] = org.__dict__['_data'] except: logger.error( 'Exception encountered on organization lookup for organization={0}' .format(results['organization'].id)) return not_found(request) can_edit = contact and account_type == AccountType.CONTRIBUTOR return render(request, 'contact/contact_profile.html', { 'contact': results, 'can_edit': can_edit, })
def edit_organization(request, org_id): """ Sends a request to the Edit Organization page if the user is logged in and a contributor account type. Arguments: org_id (string): The id of the organization that is being edited. Returns: A rendered page containing the Edit Organization form. """ if 'user_id' not in request.session: logger.error('Request to edit organization={0} without login'.format(org_id)) return unauthorized(request) else: user_id = request.session['user_id'] if 'account_type' not in request.session or request.session['account_type'] != AccountType.CONTRIBUTOR: logger.error('Request to edit organization={0} without credentials by user={1}'.format(org_id, user_id)) return unauthorized(request) try: dao = ctx.get_object('OrganizationDAO') org = dao.find(id=org_id) except: logger.error('Exception encountered on organization lookup for org={0} by user={1}'.format(org_id, user_id)) return not_found() emails = org.emails if org.emails else [] phone_numbers = org.phone_numbers if org.phone_numbers else [] types = org.types if org.types else [] form = EditOrganizationForm(request.POST or None, initial=_create_org_dict(org), emails=emails, phone_numbers=phone_numbers, types=types, ) error = '' success = '' if request.method == 'POST': if form.is_valid(): data = form.cleaned_data new_emails = [] new_phone_nums = [] new_types = [] try: for key, value in data.items(): if key.startswith('email'): new_emails.append(value.strip()) elif key.startswith('phone'): new_phone_nums.append(value.strip()) elif key.startswith('type'): new_types.append(value.strip()) else: setattr(org, key, value.strip()) if value else setattr(org, key, None) except: error = 'Oops! Something went wrong processing your request. Please try again later.' logger.error('Error occurred while updating fields for org={0} by user={1}'.format(org_id, user_id)) if not error: if new_emails: org.emails = [e for e in new_emails if e] if org.emails: org.email_key = org.emails[0] if new_phone_nums: org.phone_numbers = [p for p in new_phone_nums if p] if new_types: org.types = [t for t in new_types if t] try: dao.create_update(org) success = 'The organization has been updated successfully!' logger.info('Org={0} updated by user={1}'.format(org_id, user_id)) except: error = 'Oops! There was an error updating the organization. Please try again later.' logger.error('Error occurred saving org={0} by user={1}'.format(org_id, user_id)) return render(request, "organization/edit_organization.html", {'form': form, 'type_choices': ORG_TYPE_CHOICES, 'org_id': org_id, 'success': success, 'error': error})