def get_membership_requests(request_user, org): ensure_user_has_permission(request_user, org, 'organization.staff_view') return org.organizationmembershiprequest_set.all().order_by( Case(When(status=ReviewStatus.NEW, then=0), When(status=ReviewStatus.ACCEPTED, then=1), When(status=ReviewStatus.REJECTED, then=1)), '-request_date')
def set_volunteer_skills(request_user, user_pk, post_object): target_user = User.objects.get(pk=user_pk) ensure_user_has_permission(request_user, target_user, 'user.is_same_user') volunteer_skill_list = VolunteerSkill.objects.filter(user__id=user_pk) volunteer_skill_dict = {} for skill in volunteer_skill_list: volunteer_skill_dict[skill.skill.id] = skill all_skills = Skill.objects.all() for skill in all_skills: form_value = int(post_object.get(str(skill.id))) volunteer_value = volunteer_skill_dict.get(skill.id) if form_value == -1: if volunteer_value: volunteer_value.delete() else: if volunteer_value: volunteer_value.level = form_value else: volunteer_value = VolunteerSkill() volunteer_value.skill = skill volunteer_value.level = form_value volunteer_value.user = target_user volunteer_value.save()
def save_organization_info(request_user, orgid, organization): ensure_user_has_permission(request_user, organization, 'organization.information_edit') validate_consistent_keys(organization, ('id', orgid)) if organization.id == orgid: organization.save() else: raise ValueError('Request does not match organization')
def save_volunteer_profile(request_user, volunteer_profile_pk, volunteer_profile): validate_consistent_keys(volunteer_profile, ('id', volunteer_profile_pk)) ensure_user_has_permission(request_user, volunteer_profile.user, 'user.is_same_user') volunteer_profile.is_edited = True volunteer_profile.save()
def save_organization_social_causes(request_user, orgid, organization, post_object): ensure_user_has_permission(request_user, organization, 'organization.information_edit') validate_consistent_keys(organization, ('id', orgid)) social_causes = post_object.getlist('id_social_causes') with transaction.atomic(): for sc in OrganizationSocialCause.objects.filter( organization=orgid): sc.delete() for sc in social_causes: new_sc = OrganizationSocialCause() new_sc.social_cause = sc new_sc.organization = organization new_sc.save()
def reject_membership_request(request_user, orgid, membership_request): validate_consistent_keys(membership_request, (['organization', 'id'], orgid)) ensure_user_has_permission(request_user, membership_request, 'organization.membership_review') membership_request.status = ReviewStatus.REJECTED membership_request.reviewer = request_user OrganizationService.save_membership_request(request_user, orgid, membership_request) NotificationService.add_user_notification( membership_request.user, "Your membership request for " + membership_request.organization.name + " was rejected.", NotificationSeverity.WARNING, NotificationSource.ORGANIZATION_MEMBERSHIP_REQUEST, membership_request.id)
def accept_membership_request(request_user, orgid, membership_request): validate_consistent_keys(membership_request, (['organization', 'id'], orgid)) ensure_user_has_permission(request_user, membership_request, 'organization.membership_review') membership_request.status = ReviewStatus.ACCEPTED membership_request.reviewer = request_user OrganizationService.save_membership_request(request_user, orgid, membership_request) NotificationDomain.add_user_notification( membership_request.user, "Congratulations! Your membership request for " + membership_request.organization.name + " was accepted.", NotificationSeverity.INFO, NotificationSource.ORGANIZATION_MEMBERSHIP_REQUEST, membership_request.id)
def reject_volunteer_profile(request_user, volunteer_profile_pk): ensure_user_has_permission(request_user, request_user, 'volunteer.new_user_review') volunteer_profile = VolunteerProfile.objects.get( pk=volunteer_profile_pk) if volunteer_profile: volunteer_profile.volunteer_status = ReviewStatus.REJECTED volunteer_profile.is_edited = True volunteer_profile.save() NotificationService.add_user_notification( volunteer_profile.user, "Unfortunately your volunteer application was not approved at the time.", NotificationSeverity.INFO, NotificationSource.VOLUNTEER_APPLICATION, volunteer_profile.id) else: raise KeyError("Volunteer profile not found.")
def accept_volunteer_profile(request_user, volunteer_profile_pk): ensure_user_has_permission(request_user, request_user, 'volunteer.new_user_review') volunteer_profile = VolunteerProfile.objects.get( pk=volunteer_profile_pk) if volunteer_profile: volunteer_profile.volunteer_status = ReviewStatus.ACCEPTED volunteer_profile.is_edited = True volunteer_profile.save() NotificationService.add_user_notification( volunteer_profile.user, "Congratulations! you have been accepted as a volunteer and can now apply to work on open projects.", NotificationSeverity.INFO, NotificationSource.VOLUNTEER_APPLICATION, volunteer_profile.id) else: raise KeyError("Volunteer profile not found.")
def delete_organization_role(request_user, orgid, organization_role): validate_consistent_keys(organization_role, (['organization', 'id'], orgid)) ensure_user_has_permission(request_user, organization_role, 'organization.role_delete') if organization_role.role == OrgRole.ADMINISTRATOR and len( OrganizationService.get_organization_admins( request_user, orgid)) <= 1: raise ValueError( 'You are trying to remove the last administrator of the organization. Please appoint another administrator before removing the current one.' ) organization_role.delete() NotificationService.add_user_notification( organization_role.user, "You were removed as a staff member of " + organization_role.organization.name, NotificationSeverity.INFO, NotificationSource.ORGANIZATION, organization_role.organization.id)
def save_membership_request(request_user, orgid, membership_request): validate_consistent_keys(membership_request, (['organization', 'id'], orgid)) ensure_user_has_permission(request_user, membership_request, 'organization.membership_review') if membership_request.organization.id == orgid: with transaction.atomic(): membership_request.save() if membership_request.status == ReviewStatus.ACCEPTED and not OrganizationService.user_is_organization_member( membership_request.user, membership_request.organization): new_role = OrganizationRole( role=membership_request.role, user=membership_request.user, organization=membership_request.organization) new_role.save() else: raise ValueError('Membership request does not match organization')
def add_staff_member(request_user, orgid, organization_role): organization = Organization.objects.get(pk=orgid) if organization: ensure_user_has_permission(request_user, organization, 'organization.staff_edit') organization_role.organization = organization try: organization_role.save() NotificationService.add_user_notification( organization_role.user, "You have been added as a member of " + organization_role.organization.name + " with " + organization_role.get_role_display() + " role.", NotificationSeverity.INFO, NotificationSource.ORGANIZATION, organization_role.organization.id) except IntegrityError: raise ValueError('Duplicate user role') else: raise KeyError('Organization not found ' + str(orgid))
def create_organization(request_user, organization, org_type='socialgood'): ensure_user_has_permission(request_user, org_type, 'organization.create') if Organization.objects.filter(name=organization.name).exists(): raise ValueError('An organization with this name already exists.') with transaction.atomic(): organization.type = org_type_view_model_translation[org_type] if organization.type is None: organization.type = OrganizationType.SOCIAL_GOOD organization.save() admin_role = OrganizationRole() admin_role.user = request_user admin_role.organization = organization admin_role.role = OrgRole.ADMINISTRATOR admin_role.save() message = "You have created the organization {0} and have been made its administrator user.".format( organization.name) NotificationService.add_user_notification( request_user, message, NotificationSeverity.INFO, NotificationSource.ORGANIZATION, organization.id) return organization
def leave_organization(request_user, orgid, organization_role): validate_consistent_keys(organization_role, (['organization', 'id'], orgid)) ensure_user_has_permission(request_user, organization_role, 'organization.membership_leave') if organization_role.user != request_user: raise ValueError('Role does not match current user') else: organization_role.delete() NotificationService.add_user_notification( request_user, "You left " + organization_role.organization.name, NotificationSeverity.INFO, NotificationSource.ORGANIZATION, organization_role.organization.id) admins = OrganizationService.get_organization_admins( request_user, organization_role.organization) NotificationService.add_multiuser_notification( admins, organization_role.user.first_name + " " + organization_role.user.last_name + " left " + organization_role.organization.name, NotificationSeverity.INFO, NotificationSource.ORGANIZATION, organization_role.organization.id)
def save_organization_role(request_user, orgid, organization_role): validate_consistent_keys(organization_role, (['organization', 'id'], orgid)) ensure_user_has_permission(request_user, organization_role, 'organization.role_edit') if organization_role.organization.id == orgid: current_role = OrganizationService.get_organization_role_by_pk( organization_role.user, orgid, organization_role.id) if current_role.role == OrgRole.ADMINISTRATOR and \ organization_role.role != OrgRole.ADMINISTRATOR and \ len(OrganizationService.get_organization_admins(request_user, orgid)) <= 1: raise ValueError( 'You are trying to remove the last administrator of the organization. Please appoint another administrator before removing the current one.' ) organization_role.save() NotificationService.add_user_notification( organization_role.user, "Your role within " + organization_role.organization.name + " has been changed to " + organization_role.get_role_display() + ".", NotificationSeverity.INFO, NotificationSource.ORGANIZATION, organization_role.organization.id) else: raise ValueError('Role does not match organization')
def get_user_todos(request_user, user): ensure_user_has_permission(request_user, user, 'user.is_same_user') todos = [] if user.initial_type == UserType.VOLUNTEER: if not UserService.user_has_volunteer_profile(request_user): todos.append( {'text': 'You have not created a volunteer profile yet!'}) else: if not request_user.volunteerprofile.is_edited: todos.append({ 'text': 'You should fill out your volunteer profile.' }) elif not UserDomain.__root__.project.user.is_active( request_user ) and request_user.volunteerprofile.is_accepted: todos.append({ 'text': 'You are not volunteering for any organization, find a new project.' }) if not UserService.user_has_skills(request_user): todos.append({ 'text': 'You have no listed skills, your should add your expertise to your profile.' }) elif user.initial_type == UserType.ORGANIZATION: if not OrganizationRole.objects.filter(user=user).exists(): todos.append({ 'text': 'You are not part of any organization - create or join one!' }) for org in OrganizationService.get_user_organizations_with_pending_requests( request_user): todos.append({ 'text': 'Organization {0} has pending membership request reviews.'. format(org.name) }) for proj in ProjectService.get_user_projects_with_pending_volunteer_requests( request_user): todos.append({ 'text': 'Project {0} has pending volunteer application reviews'.format( proj.name) }) for proj in ProjectService.get_user_projects_with_pending_task_requests( request_user): todos.append({ 'text': 'Project {0} has pending task QA reviews'.format(proj.name) }) for proj in ProjectService.get_user_projects_in_draft_status( request_user): todos.append({ 'text': 'Project {0} is still in draft status and needs to be completed and published.' .format(proj.name) }) return todos
def query_pending_volunteer_profiles(request_user): ensure_user_has_permission(request_user, request_user, 'volunteer.new_user_review') return (VolunteerProfile.objects.filter( volunteer_status=ReviewStatus.NEW).order_by('is_edited', '-creation_date'))