def get(self, request, *args, **kwargs): """ View to serve GET requests :param request: the request that is to be responded to :param args: arguments :param kwargs: keyword arguments :return: the response for request """ person = request.person roles = get_all_roles(person) try: contact_information = person.contact_information.get() except ContactInformation.DoesNotExist: contact_information = None user = None if "Student" in roles: user = StudentSerializer(person.student).data elif "FacultyMember" in roles: user = FacultyMemberSerializer(person.facultymember).data contact_information = ContactInformationSerializer( contact_information).data is_phone_visible = False sale_product = SaleProduct.objects.filter(person=person) request_product = RequestProduct.objects.filter(person=person) all_results = list(chain(sale_product, request_product)) for product in all_results: if product.is_phone_visible is True: is_phone_visible = True break user['is_phone_visible'] = is_phone_visible user['person']['contact_information'] = contact_information return Response(user, status=status.HTTP_200_OK)
def get_person(self, obj): roles = get_all_roles(obj.person) try: contact_information = obj.person.contact_information.get() except ContactInformation.DoesNotExist: contact_information = None person = None if "Student" in roles: person = StudentSerializer( obj.person.student ).data elif "FacultyMember" in roles: person = FacultyMemberSerializer( obj.person.facultymember ).data contact_information = ContactInformationSerializer( contact_information ).data if obj.is_phone_visible is False: contact_information.pop('primary_phone_number') contact_information.pop('secondary_phone_number') person['person']['contact_information'] = contact_information return person
def get_custom_roles(person): """ Get all the custom roles of a person in convenient list format :param person: the person whose custom roles are being retrieved :return: the custom roles of the person as a list """ roles = get_all_roles(person) custom_roles = [{ 'role': role, 'activeStatus': str(roles[role]['activeStatus']), } for role in roles if '.' in role] return custom_roles
def process_roles(person): """ Get all the roles of a person in convenient list format :param person: the person whose roles are being retrieved :return: the roles of the person as a list """ roles = get_all_roles(person) roles = [{ 'role': key, 'activeStatus': str(roles[key]['activeStatus']), 'data': switcher.load_serializer('kernel', key)(roles[key]['instance'], excluded_fields=[ 'person', ]).data } for key in roles] return roles
def __call__(self, request): """ Perform the actual processing on the request before it goes to the view and on the response returned by the view :param request: the request being processed :return: the processed response """ if request.user and request.user.is_authenticated: try: person = request.user.person request.person = person request.roles = get_all_roles(person) except Person.DoesNotExist: request.person = None request.roles = None else: request.person = None request.roles = None response = self.get_response(request) return response