예제 #1
0
def list_specific_applications(request, animal_id):
    """
        This view function loads all pending and rejected applications for a specific animal

        args: request, animal_id
    """

    animal = check_for_unadopted_animal(animal_id)

    if animal is None:
        messages.error(
            request,
            "Either the animal you're looking for was adopted or doesn't exist, or the application you're lookingfor isn't there."
        )
        return HttpResponseRedirect(reverse('app:list_applications'))

    applications = Application.objects.filter(animal=animal).filter(
        approved=None).order_by('date_submitted')
    rejections = Application.objects.filter(animal=animal).filter(
        approved=False).order_by('date_submitted')

    context = {
        'animal': animal,
        'applications': applications,
        'num_applications': len(applications) if not None else 0,
        'rejections': rejections,
        'num_rejections': len(rejections) if not None else 0
    }
    return render(request, 'app/list_specific_applications.html', context)
예제 #2
0
def revise_judgment(request, animal_id, application_id):
    """
        This view function is responsible for determining that the selected animal is not yet adopted before removing the False condition from Application.approved so that an application can be re-considered for adoption.

        args: request, animal_id, application_id
    """

    # check that animal and application are in the database and animal isn't adopted yet
    animal = check_for_unadopted_animal(animal_id)
    application = check_for_existing_adoption_application(application_id)

    if animal is None or application is None:
        messages.error(
            request,
            "Either the animal you're looking for was adopted or doesn't exist, or the application you're looking for isn't there."
        )
        return HttpResponseRedirect(reverse('app:list_applications'))

    if request.method == 'GET':
        # get instance of application
        application = Application.objects.get(pk=application_id)
        # assign staff memeber who revised rejection to the application
        application.staff = request.user
        # apply revision (i.e. change 0 to null in database)
        application.approved = None
        application.reason = None
        application.save()
        messages.success(
            request,
            f"The application submitted by {application.user.first_name} {application.user.last_name} was marked for revision."
        )
        return HttpResponseRedirect(
            reverse('app:list_specific_applications', args=(animal_id, )))
예제 #3
0
def animal_detail(request, animal_id):
    """
        This view retrieves a single animal from the animal table, checks to ensure the animal is not adopted or id doesn't exist (control for manual user navigation to the url) and renders a detail template (otherwise redirects user with notification of animal status).

        args: request, animal_id
    """

    animal = check_for_unadopted_animal(animal_id)

    if animal is None:
        messages.error(request, 'The animal you\'re looking for has been adopted or does not exist.')
        return HttpResponseRedirect(reverse('app:pets'))

    context = {
        'animal': animal,
    }

    # Check to see if a logged in user has already applied to adopt this animal. Get the status of their application if they have.
    if request.user.is_authenticated:
        application = Application.objects.filter(user=request.user, animal=animal)

        if len(application) == 1:
            context['existing_application'] = True
            context['application'] = application[0]

    return render(request, 'app/animal_detail.html', context)
예제 #4
0
def animal_edit(request, animal_id):
    """
        This view retrieves a single animal from the animal table, checks to ensure the animal is not adopted or id doesn't exist (control for manual user navigation to the url) and renders a pre-populated animal form for an administrator.

        On POST: animal instance is updated in database

        args: request, animal_id
    """

    animal = check_for_unadopted_animal(animal_id)

    if animal is None:
        messages.error(request, 'The animal you\'re looking for has been adopted or does not exist.')
        return HttpResponseRedirect(reverse('app:pets'))

    if request.method == 'GET':
        animal_form = AnimalForm(instance=animal)
        context = {
            'animal': animal,
            'animal_form': animal_form,
            'editing': True
            }
        return render(request, 'app/animal_form.html', context)

    if request.method == 'POST':
        animal_form = AnimalForm(data=request.POST, files=request.FILES)

        if animal_form.is_valid():
            animal.name = request.POST['name']
            animal.age = request.POST['age']
            animal.sex = request.POST['sex']
            animal.description = request.POST['description']
            animal.breed = Breed.objects.get(pk=request.POST['breed'])
            animal.color = Color.objects.get(pk=request.POST['color'])
            animal.species = Species.objects.get(pk=request.POST['species'])
            animal.staff = CustomUser.objects.get(pk=request.POST['staff'])
            animal.arrival_date = request.POST['arrival_date']
            if 'image' in request.FILES:
                animal.image.delete()
                animal.image = request.FILES['image']
            animal.save()

            messages.success(request, f'{animal.name} was updated successfully.')
            return HttpResponseRedirect(reverse('app:animal_detail', args=(animal_id,)))

        else:
            context = {
                'animal': animal,
                'animal_form': animal_form,
                'editing': True
            }
            messages.error(request, 'There was a problem with the edit. The request failed.')
            return render(request, 'app/animal_form.html', context)
예제 #5
0
def reject_application(request, animal_id, application_id):
    """
        This view function is responsible for determining that the selected animal is not yet adopted before rendering a rejection template for the administrator.

        args: request, animal_id, application_id
    """

    # check that animal and application are in the database and animal isn't adopted yet
    animal = check_for_unadopted_animal(animal_id)
    application = check_for_existing_adoption_application(application_id)

    if animal is None or application is None:
        messages.error(
            request,
            "Either the animal you're looking for was adopted or doesn't exist, or the application you're looking for isn't there."
        )
        return HttpResponseRedirect(reverse('app:list_applications'))

    if request.method == 'GET':
        rejection_form = RejectionForm()
        context = {
            'animal': animal,
            'application': application,
            'rejection_form': rejection_form
        }
        return render(request, 'app/reject_application.html', context)

    if request.method == 'POST':
        rejection_form = RejectionForm(data=request.POST)

        if rejection_form.is_valid():
            # capture rejection reason provided
            application.reason = request.POST['reason']
            # assign staff memeber who provided rejection to the application
            application.staff = request.user
            # apply rejection and save
            application.approved = False
            application.save()

            messages.error(
                request,
                f"You rejected the application submitted by {application.user.first_name} {application.user.last_name}"
            )
            return HttpResponseRedirect(
                reverse('app:list_specific_applications', args=(animal_id, )))
        else:
            messages.error(
                request,
                "There was a problem with your rejection. Please try again.")
            return HttpResponseRedirect(
                reverse('app:list_specific_applications', args=(animal_id, )))
예제 #6
0
def final_decision(request, animal_id, application_id):
    """
        This view function is responsible for determining that the selected animal is not yet adopted before rendering a confirmation of adoption template for the administrator.

        args: request, animal_id, application_id
    """

    # check that animal and application are in the database and animal isn't adopted yet
    animal = check_for_unadopted_animal(animal_id)
    application = check_for_existing_adoption_application(application_id)

    if animal is None or application is None:
        messages.error(
            request,
            "Either the animal you're looking for was adopted or doesn't exist, or the application you're looking for isn't there."
        )
        return HttpResponseRedirect(reverse('app:list_applications'))

    if request.method == 'GET':
        context = {
            'animal': animal,
            'application': application,
        }
        return render(request, 'app/final_decision.html', context)

    if request.method == 'POST':
        # mark approved as true
        application.approved = True
        # assign staff memeber who approved the application
        application.staff = request.user
        application.save()

        animal.date_adopted = datetime.datetime.now()
        animal.save()

        # "reject" all remaining applications that were not already rejected.
        other_applications = Application.objects.filter(animal=animal).exclude(
            pk=application_id).exclude(approved=False)
        for app in other_applications:
            app.reason = 'A suitable owner was selected from an earlier application. Thank you for your interest, and please consider adopting another animal!'
            app.staff = request.user
            app.approved = False
            app.save()

        messages.success(
            request,
            f'You\'ve approved the adoption of {animal.name} by {application.user.first_name}     {application.user.last_name}!'
        )
        return HttpResponseRedirect(reverse('app:list_applications'))
def adoption_app(request, animal_id):
    """
        This view performs one of two actions:
        1. On GET: Retrieves a single animal from the animal table, checks to ensure the animal is not adopted or the id doesn't exist (control for manual user navigation to the url) and renders an adoption application template (otherwise redirects user with notification of animal status).
        2. On POST: Saves a user's application for a specific animal to the database

        args: request, animal_id
    """

    animal = check_for_unadopted_animal(animal_id)

    if request.method == 'GET':

        if animal is None:
            messages.error(
                request,
                'The animal you\'re looking for has been adopted or does not exist.'
            )
            return HttpResponseRedirect(reverse('app:pets'))

        # perform a secondary check to ensure that a user hasn't navigated around controls that prevent more than one application per user for a given animal (i.e. logged out view of animal detail -> click apply to adopt -> sign in). Another check is performed in the animal_detail view for a user navigating the site while already logged in.
        application = Application.objects.filter(user=request.user,
                                                 animal=animal)

        if len(application) == 1:
            messages.error(
                request, 'Only one application can be submitted per animal.')
            return HttpResponseRedirect(
                reverse('app:animal_detail', args=(animal_id, )))
        else:
            app_form = ApplicationForm()
            context = {
                'animal': animal,
                'app_form': app_form,
            }
            return render(request, 'app/adoption_app.html', context)

    if request.method == 'POST':

        if animal is None:
            messages.error(
                request,
                'This animal has been adopted! There are plenty of forever friends left, though!'
            )
            return HttpResponseRedirect(reverse('app:pets'))

        app_form = ApplicationForm(data=request.POST)

        if app_form.is_valid():
            new_app = Application(
                user=request.user,  # set submitter id as current user
                text=request.POST['text'],
                date_submitted=datetime.datetime.now(
                ),  # set date AND time of submission
                animal=
                animal,  # set animal id as the animal the user applied to adopt
            )
            new_app.save()

            messages.success(
                request,
                'Thanks for applying to adopt! You can monitor the status of your application(s) here!'
            )
            return HttpResponseRedirect(reverse('app:profile'))