<commit_before>from django.views.generic.base import View
from django.shortcuts import render_to_response, RequestContext, redirect
from django.core.urlresolvers import reverse
from django.contrib import messages

from registry.patients.models import ParentGuardian, Patient, PatientAddress, AddressType
from models import Registry, RegistryForm
from registry.patients.admin_forms import ParentGuardianForm


class ParentView(View):

    _ADDRESS_TYPE = "Postal"

    def get(self, request, registry_code):
        context = {}
        if request.user.is_authenticated():
            parent = ParentGuardian.objects.get(user=request.user)
            registry = Registry.objects.get(code=registry_code)
            forms = RegistryForm.objects.filter(registry=registry)

            context['parent'] = parent
            context['patients'] = parent.patient.all()
            context['registry_code'] = registry_code
            context['registry_forms'] = forms

        return render_to_response('rdrf_cdes/parent.html', context, context_instance=RequestContext(request))

    def post(self, request, registry_code):
        parent = ParentGuardian.objects.get(user=request.user)
        registry = Registry.objects.get(code=registry_code)

        patient = Patient.objects.create(
            consent=True,
            family_name=request.POST["surname"],
            given_names=request.POST["first_name"],
            date_of_birth=request.POST["date_of_birth"],
            sex=request.POST["gender"],
        )
        patient.rdrf_registry.add(registry)
        patient.save()

        use_parent_address = "use_parent_address" in request.POST

        PatientAddress.objects.create(
            patient=patient,
            address_type=AddressType.objects.get(description__icontains=self._ADDRESS_TYPE),
            address=parent.address if use_parent_address else request.POST["address"],
            suburb=parent.suburb if use_parent_address else request.POST["suburb"],
            state=parent.state if use_parent_address else request.POST["state"],
            postcode=parent.postcode if use_parent_address else request.POST["postcode"],
            country=parent.country if use_parent_address else request.POST["country"]
        )

        parent.patient.add(patient)
        parent.save()
        messages.add_message(request, messages.SUCCESS, 'Patient added successfully')
        return redirect(reverse("parent_page", args={registry_code: registry_code}))


class ParentEditView(View):
    _ADDRESS_TYPE = "Postal"

    def get(self, request, registry_code, parent_id):
        context = {}
        parent = ParentGuardian.objects.get(user=request.user)

        context['parent'] = parent
        context['registry_code'] = registry_code
        context['parent_form'] = ParentGuardianForm(instance=parent)
<commit_msg>LoginRequired mixin for Parent/Guardian views<commit_after>from django.views.generic.base import View
from django.shortcuts import render_to_response, RequestContext, redirect
from django.core.urlresolvers import reverse
from django.contrib import messages
from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required

from registry.patients.models import ParentGuardian, Patient, PatientAddress, AddressType
from models import Registry, RegistryForm
from registry.patients.admin_forms import ParentGuardianForm


class LoginRequiredMixin(object):

    @method_decorator(login_required)
    def dispatch(self, request, *args, **kwargs):
        return super(LoginRequiredMixin, self).dispatch(
            request, *args, **kwargs)


class ParentView(LoginRequiredMixin, View):

    _ADDRESS_TYPE = "Postal"

    def get(self, request, registry_code):
        context = {}
        if request.user.is_authenticated():
            parent = ParentGuardian.objects.get(user=request.user)
            registry = Registry.objects.get(code=registry_code)
            forms = RegistryForm.objects.filter(registry=registry)

            context['parent'] = parent
            context['patients'] = parent.patient.all()
            context['registry_code'] = registry_code
            context['registry_forms'] = forms

        return render_to_response('rdrf_cdes/parent.html', context, context_instance=RequestContext(request))

    def post(self, request, registry_code):
        parent = ParentGuardian.objects.get(user=request.user)
        registry = Registry.objects.get(code=registry_code)

        patient = Patient.objects.create(
            consent=True,
            family_name=request.POST["surname"],
            given_names=request.POST["first_name"],
            date_of_birth=request.POST["date_of_birth"],
            sex=request.POST["gender"],
        )
        patient.rdrf_registry.add(registry)
        patient.save()

        use_parent_address = "use_parent_address" in request.POST

        PatientAddress.objects.create(
            patient=patient,
            address_type=AddressType.objects.get(description__icontains=self._ADDRESS_TYPE),
            address=parent.address if use_parent_address else request.POST["address"],
            suburb=parent.suburb if use_parent_address else request.POST["suburb"],
            state=parent.state if use_parent_address else request.POST["state"],
            postcode=parent.postcode if use_parent_address else request.POST["postcode"],
            country=parent.country if use_parent_address else request.POST["country"]
        )

        parent.patient.add(patient)
        parent.save()
        messages.add_message(request, messages.SUCCESS, 'Patient added successfully')
        return redirect(reverse("parent_page", args={registry_code: registry_code}))


class ParentEditView(LoginRequiredMixin, View):
    _ADDRESS_TYPE = "Postal"

    def get(self, request, registry_code, parent_id):
        context = {}
        parent = ParentGuardian.objects.get(user=request.user)

        context['parent'] = parent
        context['registry_code'] = registry_code
        context['parent_form'] = ParentGuardianForm(instance=parent)

{'fraction_non_alphanumeric': 0.09608132480734546, 'fraction_numerical': 0.0, 'mean_word_length': 3.410701373825018, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5362019', 'n_tokens_mistral': 1636, 'n_tokens_neox': 1573, 'n_words': 310}