def process(self):
        # Get the model
        purpose = Purpose.objects.filter(id=self._id)
        if not purpose:
            return failure_response("Purpose does not exist", status.HTTP_404_NOT_FOUND)
        purpose = purpose[0]

        # Extract attributes
        name = self._data.get("name")

        # Modify new fields
        modify_attribute(purpose, "name", name)

        purpose.save()
        return success_response(self._serializer(purpose).data, status.HTTP_200_OK)
Exemple #2
0
    def process(self):
        # Get the model
        prompt = Prompt.objects.filter(id=self._id)
        if not prompt:
            return failure_response("Prompt does not exist",
                                    status.HTTP_404_NOT_FOUND)
        prompt = prompt[0]

        # Extract attributes
        name = self._data.get("question_name")
        label = self._data.get("question_placeholder")

        # Modify new fields
        modify_attribute(prompt, "question_name", name)
        modify_attribute(prompt, "question_placeholder", label)

        prompt.save()
        return success_response(
            self._serializer(prompt).data, status.HTTP_200_OK)
    def process(self):
        # Get the model
        location = Location.objects.filter(id=self._id)
        if not location:
            return failure_response("Location does not exist",
                                    status.HTTP_404_NOT_FOUND)
        location = location[0]

        # Extract attributes
        area = self._data.get("area")
        name = self._data.get("name")

        # Modify new fields
        modify_attribute(location, "area", area)
        modify_attribute(location, "name", name)

        # Save new changes
        location.save()
        return success_response(
            self._serializer(location).data, status.HTTP_200_OK)
    def process(self):
        # Get the model
        group = Group.objects.filter(id=self._id)
        if not group:
            return failure_response("Group does not exist", status.HTTP_404_NOT_FOUND)
        group = group[0]

        # Extract attributes
        name = self._data.get("name")
        subtitle = self._data.get("subtitle")
        img_url = self._data.get("img_url")

        # Modify new fields
        modify_attribute(group, "name", name)
        modify_attribute(group, "subtitle", subtitle)
        modify_attribute(group, "img_url", img_url)

        # Save new changes
        group.save()
        return success_response(self._serializer(group).data, status.HTTP_200_OK)
    def process(self):
        # Get the model
        interest = Interest.objects.filter(id=self._id)
        if not interest:
            return failure_response("Interest does not exist",
                                    status.HTTP_404_NOT_FOUND)
        interest = interest[0]

        # Extract attributes
        name = self._data.get("name")
        subtitle = self._data.get("subtitle")
        img_url = self._data.get("img_url")

        # Modify new fields
        modify_attribute(interest, "name", name)
        modify_attribute(interest, "subtitle", subtitle)
        modify_attribute(interest, "img_url", img_url)

        interest.save()
        return success_response(
            self._serializer(interest).data, status.HTTP_200_OK)
    def process(self):
        # Get the model
        survey = Survey.objects.filter(id=self._id)
        if survey is None:
            return failure_response("Survey does not exist", status.HTTP_404_NOT_FOUND)
        survey = survey[0]

        # Extract attributes
        did_meet = self._data.get("did_meet")
        explanation = self._data.get("explanation")
        rating = self._data.get("rating")
        if rating is not None and rating not in RATINGS:
            return failure_response(
                "The provided rating is invalid", status.HTTP_400_BAD_REQUEST
            )

        # Modify new fields
        modify_attribute(survey, "did_meet", did_meet)
        modify_attribute(survey, "explanation", explanation)
        modify_attribute(survey, "rating", rating)

        survey.save()
        return success_response(None, status.HTTP_200_OK)
Exemple #7
0
    def process(self):
        # Verify that all required fields are provided
        did_meet = self._data.get("did_meet")
        if did_meet is None:
            return failure_response("did_meet", status.HTTP_400_BAD_REQUEST)

        # Get optional fields based on did_meet
        did_meet_reason = self._data.get("did_meet_reason")
        did_not_meet_reasons = self._data.get("did_not_meet_reasons")
        rating = self._data.get("rating")
        if did_meet:
            if rating is None:
                return failure_response(
                    "Rating is required for a completed match",
                    status.HTTP_400_BAD_REQUEST,
                )
            elif rating not in constants.RATINGS:
                return failure_response("The provided rating is invalid",
                                        status.HTTP_400_BAD_REQUEST)
        else:
            if not did_not_meet_reasons:
                return failure_response(
                    "did_not_meet_reasons is required if did_meet is False",
                    status.HTTP_400_BAD_REQUEST,
                )
            if not all(
                    map(lambda x: x in constants.DID_NOT_MEET.values(),
                        did_not_meet_reasons)):
                return failure_response(
                    "did_not_meet_reasons has invalid elements",
                    status.HTTP_400_BAD_REQUEST,
                )

            did_not_meet_reasons = list(
                map(lambda x: constants.DID_NOT_MEET_REV[x],
                    did_not_meet_reasons))
            if len(did_not_meet_reasons) > 5:
                return failure_response(
                    "Too many elements in did_not_meet_reasons",
                    status.HTTP_400_BAD_REQUEST,
                )

        # Verify that required ids are valid
        completed_match = Match.objects.filter(id=self._match_id)
        if not completed_match:
            return failure_response("match_id is invalid",
                                    status.HTTP_404_NOT_FOUND)
        completed_match = completed_match[0]

        # Check if the submitting user has already submitted a survey
        survey = Survey.objects.filter(
            submitting_person=self._request.user.person,
            completed_match=completed_match)
        if survey:
            return failure_response(
                "This user has already submitted feedback to the provided match!",
                status.HTTP_403_FORBIDDEN,
            )

        # Update match status based on feedback
        if not did_meet:
            # Cancel a match if any person says they did not meet
            modify_attribute(completed_match, "status", match_status.CANCELED)
        # Force a match to stay canceled if set before
        # TODO(@team) Check how often this edge case fails through explanations
        elif completed_match.status != match_status.CANCELED:
            modify_attribute(completed_match, "status", match_status.INACTIVE)
        completed_match.save()

        # Create and return a new survey with the given fields
        survey = Survey.objects.create(
            did_meet=did_meet,
            did_meet_reason=did_meet_reason,
            did_not_meet_reasons=json.dumps(did_not_meet_reasons)
            if not did_meet else None,
            rating=rating,
            submitting_person=self._request.user.person,
            completed_match=completed_match,
        )
        self._request.user.person.pending_feedback = False
        survey.save()
        self._request.user.person.save()
        return success_response(None, status.HTTP_201_CREATED)
    def process(self):
        net_id = self._data.get("net_id")
        first_name = self._data.get("first_name")
        last_name = self._data.get("last_name")
        major_ids = self._data.get("majors")
        hometown = self._data.get("hometown")
        profile_pic_url = self._data.get("profile_pic_url")
        profile_pic_base64 = self._data.get("profile_pic_base64")
        facebook_url = self._data.get("facebook_url")
        instagram_username = self._data.get("instagram_username")
        graduation_year = self._data.get("graduation_year")
        pronouns = self._data.get("pronouns")
        purpose_ids = self._data.get("purposes")
        talking_points = self._data.get("talking_points")
        availability = self._data.get("availability")
        location_ids = self._data.get("locations")
        group_ids = self._data.get("groups")
        prompts = self._data.get("prompts")
        interest_ids = self._data.get("interests")
        has_onboarded = self._data.get("has_onboarded")
        pending_feedback = self._data.get("pending_feedback")
        deleted = self._data.get("deleted")
        fcm_registration_token = self._data.get("fcm_registration_token")
        is_paused = self._data.get("is_paused")
        pause_weeks = self._data.get("pause_weeks")

        many_to_many_sets = [
            [Purpose, self._person.purposes, purpose_ids],
            [Major, self._person.majors, major_ids],
            [Location, self._person.locations, location_ids],
            [Group, self._person.groups, group_ids],
            [Interest, self._person.interests, interest_ids],
        ]

        for (class_name, existing_set, ids) in many_to_many_sets:
            possible_error = update_many_to_many_set(class_name, existing_set,
                                                     ids)
            if possible_error is not None:
                return possible_error

        if is_paused is False:
            self._person.pause_expiration = None
            pause_weeks = None

        if pause_weeks is not None:
            if pause_weeks != 0:
                days = pause_weeks * 6
                self._person.pause_expiration = timezone.now() + timedelta(
                    days=days)

        self._person.last_active = timezone.now()

        if profile_pic_base64 is not None:
            upload_profile_pic.delay(self._user.id, profile_pic_base64)

        if prompts is not None:
            # Sort prompts by id to ensure Django doesn't change the order
            prompts.sort(key=lambda prompt: prompt.get("id"))

            prompt_questions = []
            prompt_answers = []
            # Now, iterate through prompts to check validity and separate questions/answers
            for prompt in prompts:
                prompt_id = prompt.get("id")
                prompt_answer = prompt.get("answer")
                prompt_question = Prompt.objects.filter(id=prompt_id)
                if not prompt_question:
                    return failure_response(
                        f"Prompt id {prompt_id} does not exist.")
                prompt_questions.append(prompt_id)
                prompt_answers.append(prompt_answer)

            self._person.prompt_questions.set(prompt_questions)
            modify_attribute(self._person, "prompt_answers",
                             json.dumps(prompt_answers))

        if (fcm_registration_token is not None and
                self._person.fcm_registration_token != fcm_registration_token):
            GCMDevice.objects.filter(
                registration_id=self._person.fcm_registration_token).delete()
            self._person.fcm_registration_token = fcm_registration_token
            fcm_device = GCMDevice.objects.create(
                registration_id=fcm_registration_token,
                cloud_message_type="FCM",
                user=self._user,
            )
            self._user.fcm_device = fcm_device

        modify_attribute(self._person, "net_id", net_id)
        modify_attribute(self._user, "first_name", first_name)
        modify_attribute(self._user, "last_name", last_name)
        modify_attribute(self._person, "hometown", hometown)
        modify_attribute(self._person, "facebook_url", facebook_url)
        modify_attribute(self._person, "instagram_username",
                         instagram_username)
        modify_attribute(self._person, "graduation_year", graduation_year)
        modify_attribute(self._person, "pronouns", pronouns)
        modify_attribute(self._person, "talking_points",
                         json.dumps(talking_points))
        modify_attribute(self._person, "has_onboarded", has_onboarded)
        modify_attribute(self._person, "pending_feedback", pending_feedback)
        modify_attribute(self._person, "availability",
                         json.dumps(availability))
        modify_attribute(self._person, "profile_pic_url", profile_pic_url)
        modify_attribute(self._person, "soft_deleted", deleted)
        modify_attribute(self._person, "is_paused", is_paused)
        modify_attribute(self._person, "pending_feedback", pending_feedback)
        self._user.save()
        self._person.save()
        return success_response(status=status.HTTP_200_OK)