def base_insurance_creation_fields( *, type: InsuranceType, group_id: str, start_date: datetime, end_date: datetime, responsible_phone_number: str, created_by: settings.AUTH_USER_MODEL, comment: str = "", id: str = None, ) -> dict: # validate group created_by.fetch_detailed_group_info() group_object = next((scouts_group for scouts_group in created_by.scouts_groups if scouts_group.id == group_id), None) if not group_object: raise ValidationError("Given group %s is not a valid group of user" % group_id) member = MemberService.member_create_from_user( user=created_by, phone_number=responsible_phone_number) fields = { "status": InsuranceStatus.NEW, "type": type, "group": group_object, "start_date": start_date, "end_date": end_date, "responsible_member": member, "comment": comment, "created_on": timezone.now(), "id": id, } return fields
def handle_user_sign_up(sender, instance: settings.AUTH_USER_MODEL, created, **kwargs): if created: add_user_to_group(instance, 'common_users') set_user_for_subscriber(instance) Profile.objects.create(user=instance) instance.save()
def test_user_is_student(user: settings.AUTH_USER_MODEL): # by default a user that signs up is not a student or staff assert not user.is_student assert not user.is_staff user.is_student = True user.is_staff = True assert user.is_student assert user.is_staff
def save_user(self, user: settings.AUTH_USER_MODEL, *args, **kwargs) -> settings.AUTH_USER_MODEL: """ Hook to add custom logic around saving a user. Return the saved user instance. """ is_new_instance = user.pk is None user.save() if is_new_instance: logger.debug('New user created') else: logger.debug('User %s updated with incoming attributes', user) return user
def user_has_serviceform_permission(user: settings.AUTH_USER_MODEL, service_form: 'ServiceForm', raise_permissiondenied: bool = True): if user.has_perm('serviceform.can_access_serviceform', service_form): return True else: if raise_permissiondenied: raise PermissionDenied(_('User is not allowed to access document')) else: return False
def test_file_upload(self, user: settings.AUTH_USER_MODEL, request_factory: RequestFactory): # user is not staff so requests for file_upload is redirected non_auth_request = request_factory.get("/fake-url") non_auth_request.user = user non_auth_response = file_upload(non_auth_request) assert non_auth_response.status_code == 302 # user is student but not stadd so request for file_upload is redirected student_request = request_factory.get("fake-url") user.is_student = True student_request.user = user student_response = file_upload(student_request) assert student_response.status_code == 302 # user is staff so they have access to file_upload staff_request = request_factory.get("fake-url") user.is_staff = True staff_request.user = user staff_response = file_upload(staff_request) assert staff_response.status_code == 200
def test_proxy(self, user: settings.AUTH_USER_MODEL, request_factory: RequestFactory): # user is not staff or student so they will be redirected home non_auth_request = request_factory.get("/texts/the_jungle/index.html") non_auth_request.user = user non_auth_response = proxy(non_auth_request, "/texts/the_jungle/index.html") assert non_auth_response.status_code == 302 # user is student so they will have access to non-home custom urls # we assume '/texts/the_jungle/index.html' exists student_request = request_factory.get("/texts/the_jungle/index.html") user.is_student = True student_request.user = user student_response = proxy(student_request, "/texts/the_jungle/index.html") assert student_response.status_code == 200 student_content = student_response.content.decode("utf-8") assert "The Jungle" in student_content
def test_home(self, user: settings.AUTH_USER_MODEL, request_factory: RequestFactory): # user that is not staff nor student cannot access textbook links non_auth_request = request_factory.get("/") non_auth_request.user = user non_auth_response = home(non_auth_request) non_auth_content = non_auth_response.content.decode("utf-8") assert "Permission from instructor is required to view the textbooks" in non_auth_content assert "/texts/the_jungle/index.html" not in non_auth_content # user that is student can access textbook links # assume '/texts/the_jungle.index.html' exists student_request = request_factory.get("/") user.is_student = True student_request.user = user student_response = home(student_request) student_content = student_response.content.decode("utf-8") assert "Permission from instructor is required to view the textbooks" not in student_content assert "/texts/the_jungle/index.html" in student_content
def send_mail_friend_request( user: settings.AUTH_USER_MODEL, user_friend: settings.AUTH_USER_MODEL ): """ Prepare data to be sent to a user """ title: str = "PrivetThere: New friend!" body: str = "The user '{username}' ({link}) would like to add you as a " \ "friend.\nYou can approve or refuse your friendship on the " \ "website. See you there :)".format( username=user.username, link=settings.HOST_ADDRESS + user_friend.get_absolute_url() ) send_email.delay( title=title, body=body, email=user_friend.email )
def test_user_get_absolute_url(user: settings.AUTH_USER_MODEL): assert user.get_absolute_url() == f"/users/{user.username}/"
def test_user_get_absolute_url(user: settings.AUTH_USER_MODEL): assert user.get_absolute_url() == "/users/{user}/".format(user=user.username)
def test_user_get_absolute_url(user: settings.AUTH_USER_MODEL): expected_url = '/users/{0}/'.format(user.username) assert_that(user.get_absolute_url(), is_(equal_to(expected_url)))
def test_user_get_absolute_url(user: settings.AUTH_USER_MODEL): assert user.get_absolute_url(api=False) == f"/accounts/{user.username}/"
def test_user_get_absolute_url_api(user: settings.AUTH_USER_MODEL): expected_result = drf_reverse("accounts-api:user-detail", kwargs={"pk": user.pk}) assert user.get_absolute_url(api=True) == expected_result
def get_referral_link(self, instance: settings.AUTH_USER_MODEL): request = self.context.get('request') return instance.referral_link(request)
def is_clinician(user: settings.AUTH_USER_MODEL) -> bool: if not user.is_authenticated(): return False # won't have a profile return user.profile.is_clinician
def test_user_get_absolute_url(user: settings.AUTH_USER_MODEL): assert user.get_absolute_url() == f"/users/dashboard/"