Esempio n. 1
0
def render_create_dog(request: HttpRequest) -> HttpResponse:
    if request.method == 'POST':
        form = CreateDogForm(request.POST)
        if form.is_valid():
            now: datetime = timezone.now()
            account: Account = fetch_account(request)
            if len(fetch_dogs(account.id, now)) < MAX_NUMBER_OF_DOGS:
                id_public: str = random_token(30)
                dog: Dog = create_dog(
                    account=account,
                    id_public=id_public,
                    created_at=now,
                    name=form.cleaned_data['dog_name'],
                    breed=form.cleaned_data['dog_breed']
                )
                dog.save()
                add_dog_event(dog, DogEventType.ADOPTION, now)
                return redirect(reverse('dogs'))
            else:
                return render(request, 'error.html', {'error': 'You already have ' + str(MAX_NUMBER_OF_DOGS) + ' dogs!'})
    else:
        form = CreateDogForm()
    return render(request, 'create_dog.html', {
        'form': form,
        'dog_random_image': 'index/dog_' + str(randrange(5)) + '.jpg'
    })
Esempio n. 2
0
def render_delete_account(request: HttpRequest) -> HttpResponse:
    if request.method == 'POST':
        form = CreateDeleteAccountForm(request.POST)
        if form.is_valid():
            delete_account(fetch_account(request))
            return redirect('/')
    else:
        form = CreateDeleteAccountForm()
    return render(request, 'delete_account.html', {'form': form})
Esempio n. 3
0
 def __call__(self, request: HttpRequest):
     if request.path in WHITELIST_WITHOUT_COOKIE:
         return self.get_response(request)
     else:
         now: datetime = timezone.now()
         account: Account = fetch_account(request)
         if account is not None and account.is_cookie_consent_valid(now):
             account.update_last_seen_at(now)
             account.save()
             return self.get_response(request)
         else:
             return redirect(COOKIE_CONSENT_PATH)
Esempio n. 4
0
def render_cancel_story(request: HttpRequest,
                        dog_id: str = '') -> HttpResponse:
    account: Optional[Account] = fetch_account(request)
    dog: Optional[Dog] = fetch_dog(dog_id)
    if account is None:
        return render_error(request, "Account not found.")
    if dog is None:
        return render_error(request, "Dog not found.")
    if not dog.is_owner(account):
        return render_error(request, "This doggo is not yours!")
    story: Optional[Story] = fetch_story(dog)
    if story is None:
        return render_error(request, "Story not found.")
    story.delete()
    return redirect(reverse('dog', kwargs={'id': dog.id_public}))
Esempio n. 5
0
def render_dogs(request: HttpRequest) -> HttpResponse:
    # check there is at least one dog alive or switch to create_dog
    now: datetime = timezone.now()
    account: Account = fetch_account(request)
    dogs: List[Dog] = fetch_dogs(account.id, now)
    if len(dogs) == 0:
        return redirect('create_dog')
    else:
        return render(request,
                      'dogs.html',
                      context={
                          'account': account,
                          'dogs': dogs,
                          'can_create_dog': len(dogs) < MAX_NUMBER_OF_DOGS,
                      })
Esempio n. 6
0
def cookie_consent(request: HttpRequest) -> HttpResponse:
    if request.method == 'POST':
        now: datetime = timezone.now()
        account: Account = fetch_account(request)
        if account is None:
            account = create_account(created_at=now)
        account.cookie_consent_at = now
        account.save()
        response: HttpResponse = redirect('/')
        return set_account_id_cookie(response, account.id_secret, now)
    else:
        return render(
            request, 'cookie_consent.html', {
                'disable_navbar': True,
                'cookie_consent_duration_days': str(
                    COOKIE_CONSENT_DURATION.days)
            })
Esempio n. 7
0
def render_home(request: HttpRequest) -> HttpResponse:
    now: datetime = timezone.now()
    # process random dog
    random_dog: Optional[Dog] = fetch_and_update_random_dog(now)
    # process can create dog
    account: Account = cookie.fetch_account(request)
    dogs: List[Dog] = fetch_dogs(account.id, now)
    # process last events
    dog_events: List[DogEvent] = get_last_dog_events(5)
    return render(request,
                  'home.html',
                  context={
                      'random_dog': random_dog,
                      'has_no_dog': len(dogs) == 0,
                      'announcements': ANNOUNCEMENTS,
                      'DogEventType': DogEventType,
                      'dog_events': dog_events
                  })
Esempio n. 8
0
def fetch_update_dog(
    request: HttpRequest, id_public: str,
    updater: Optional[Callable[[Dog, Account, datetime],
                               bool]]) -> HttpResponse:
    now: datetime = timezone.now()
    dog: Optional[Dog] = fetch_dog(id_public)
    if dog is not None:
        dog_updater.update(dog, now)
        account: Account = fetch_account(request)
        if updater is not None:
            # TODO redirect to error page if the updater returns false (and test)
            updater(dog, account, now)
        if updater is None:
            return display_dog(request, dog, account, now)
        else:
            return redirect(reverse('dog', kwargs={'id': dog.id_public}))
    else:
        return render(request, 'dog_not_found.html', status=404)
Esempio n. 9
0
def render_story_choice(request: HttpRequest,
                        dog_id: str = '',
                        choice_id: str = '') -> HttpResponse:
    """
    TODO add security check that the account is the owner
    :param request:
    :param dog_id:
    :param choice_id:
    :return:
    """
    account: Optional[Account] = fetch_account(request)
    dog: Optional[Dog] = fetch_dog(dog_id)
    if account is None:
        return render_error(request, "Account not found.")
    if dog is None:
        return render_error(request, "Dog not found.")
    if not dog.is_owner(account):
        return render_error(request, "This doggo is not yours!")
    story: Optional[Story] = fetch_story(dog)
    if story is None:
        return render_error(request, "Story not found.")
    apply_story_choice(story, choice_id, account, dog)
    return redirect(reverse('dog', kwargs={'id': dog.id_public}))