Example #1
0
def vaccination_signup_view(request):
    """
    Get user's vaccination from from
    then create vaccine and save to database.
    """
    if request.method == 'GET':
        formset = VaccineFormSet(request.GET or None)
    elif request.method == 'POST':
        formset = VaccineFormSet(request.POST)
        if formset.is_valid():
            for form in formset:
                if form.cleaned_data.get('vaccine_name'):
                    vaccine_name = form.cleaned_data.get('vaccine_name')
                    dose_count = form.cleaned_data.get('dose_count')
                    date_taken = form.cleaned_data.get('date_taken')
                    create_vaccine(request.user.id, vaccine_name, dose_count,
                                   date_taken)
            client_ip = get_client_ip(request)
            logger.info('Request update vaccination for {} from {}'.format(
                request.user, client_ip))
        vaccine_suggest(request.user)
        return redirect('users:profile')
    return render(request, 'registration/vaccination.html', {
        'formset': formset,
    })
Example #2
0
def request_user_view(request):
    """Render page for user to request to view other user page."""
    if request.method == 'GET':
        return render(request, 'request_user.html')
    elif request.method == 'POST':
        if is_valid_uuid(request.POST['uuid']):
            try:
                user = CustomUser.objects.get(
                    parental_key=request.POST['uuid'])
            except (KeyError, CustomUser.DoesNotExist):
                client_ip = get_client_ip(request)
                logger.warning('Request wrong uuid from {}'.format(client_ip))
                return render(
                    request, 'request_user.html', {
                        'error_message': "Could not find user with this uuid.",
                    })
            else:
                uuid = request.POST['uuid'][:4]
                return HttpResponseRedirect(
                    reverse('users:parental',
                            kwargs={
                                'user_id': user.id,
                                'uuid': uuid
                            }))
        else:
            return render(request, 'request_user.html',
                          {'error_message': "Invalid uuid."})
Example #3
0
def signup_view(request):
    """Get user's infomation from from then create user and save to database"""
    if request.method == 'POST':
        form = CustomUserForm(request.POST)
        user = CustomUser.objects.get(pk=request.user.pk)
        if form.is_valid():
            first_name = form.cleaned_data.get('first_name')
            last_name = form.cleaned_data.get('last_name')
            contact = form.cleaned_data.get('contact')
            emergency_contact = form.cleaned_data.get('emergency_contact')
            gender = form.cleaned_data.get('gender')
            birthdate = form.cleaned_data.get('birthdate')
            age = calculate_age(birthdate)
            user.update_profile(username=user.email,
                                first_name=first_name,
                                last_name=last_name,
                                contact=contact,
                                emergency_contact=emergency_contact,
                                gender=gender,
                                birthdate=birthdate,
                                age=age)
            user.save()
            client_ip = get_client_ip(request)
            logger.info('Update profile for {} from {}'.format(
                request.user, client_ip))
            return HttpResponseRedirect(reverse('users:vaccination'))
    else:
        form = CustomUserForm()
        return render(request, 'registration/signup.html', {
            'form': form,
            'have_outbreak': False
        })
Example #4
0
def add_vaccine(request):
    """Get vaccine name, dose, and date taken input from the user and add vaccine"""
    if request.method == 'GET':
        formset = VaccineFormSet(request.GET or None)
    elif request.method == 'POST':
        formset = VaccineFormSet(request.POST)
        user_vac_name = [
            v.vaccine_name for v in request.user.vaccine_set.all()]
        if formset.is_valid():
            for form in formset:
                if form.cleaned_data.get('vaccine_name'):
                    vaccine_name = form.cleaned_data.get('vaccine_name')
                    dose_count = form.cleaned_data.get('dose_count')
                    date_taken = form.cleaned_data.get('date_taken')
                    if vaccine_name not in user_vac_name:
                        create_vaccine(request.user.id,
                                       vaccine_name,
                                       dose_count,
                                       date_taken)
        client_ip = get_client_ip(request)
        logger.info('User: {} add vaccines request from {}'.format(
            str(request.user), client_ip))
        return redirect('users:profile')
    return render(request, 'add_vaccine.html',
                  {'formset': formset, })
Example #5
0
def track_first_date(request, vaccine_id: int):
    """
    Get input from the user which is the first date that user will receive
    the first dose of that vaccine and track other dose if have.
    """
    vaccine = Vaccine.objects.get(id=vaccine_id)
    vacc_model = VaccineModel.objects.get(vaccine_name=vaccine.vaccine_name)
    if request.method == 'POST':
        form = DateExpiredForm(request.POST)
        if form.is_valid():
            date_taken = form.cleaned_data.get('date_taken')
            if vaccine.stimulate_phase > 0:
                user_dose = vaccine.dose_set.all()[0]
                user_dose.date_taken = date_taken
                user_dose.save()
            else:
                for dose in vacc_model.dosemodel_set.all():
                    user_dose = vaccine.dose_set.get(
                        dose_count=dose.dose_count)
                    user_dose.date_taken = next_date(
                        date_taken, dose.dose_duration)
                    user_dose.save()
        client_ip = get_client_ip(request)
        logger.info(
            'User: {} request update first date to receive {} from {}'.format(
                str(request.user), vaccine, client_ip))
    return redirect('users:profile')
Example #6
0
def del_vaccine(request, vaccine_id: int):
    """Remove specify vaccine from user's input"""
    if request.method == 'POST':
        vaccine = Vaccine.objects.get(pk=request.POST['delvacc'])
        client_ip = get_client_ip(request)
        logger.info('User: {} remove {} request from {}'.format(
            str(request.user), vaccine, client_ip))
        vaccine.delete()
        return redirect('users:profile')
Example #7
0
def received_dose(request, dose_id: int):
    """Get input from the user that the user received that dose."""
    if request.method == 'POST':
        status = request.POST['receivedbtn']
        dose = Dose.objects.get(id=dose_id)
        if status == 'received':
            dose.received = True
            dose.save()
        if dose.vaccine.stimulate_phase > 0:
            new_dose = Dose(vaccine=dose.vaccine,
                            dose_count=dose.vaccine.dose_set.count() + 1,
                            date_taken=next_date(
                                dose.date_taken, dose.vaccine.stimulate_phase),
                            received=False)
            new_dose.save()
        client_ip = get_client_ip(request)
        logger.info('User: {} received dose request from {}'.format(
            str(request.user), client_ip))
    return redirect('users:profile')
Example #8
0
def parental_view(request, user_id: int, uuid: str):
    user = CustomUser.objects.get(id=user_id)
    if user.age >= 1:
        age = int(user.age)
    else:
        age = user.age * 100
    if uuid == user.parental_key[:4]:
        user = CustomUser.objects.get(id=user_id)
        user_vacc = []
        for vac in user.vaccine_set.all():
            dose_list = list(vac.dose_set.all())
            if dose_list[0].received:
                user_vacc.append(vac)
        upcoming_vaccine_list = upcoming_vaccine(user)
        context = {
            'user': user,
            'age': age,
            'vaccine_set': user_vacc,
            'upcoming_vaccine': upcoming_vaccine_list
        }
        client_ip = get_client_ip(request)
        logger.info('Parental view for {} from {}'.format(user, client_ip))
        return render(request, 'parental.html', context)