Esempio n. 1
0
def clientCheckin(request, client_id):
    if not request.user.is_authenticated():
        return HttpResponseRedirect("/")

    theClient = Client.objects.filter(pk=client_id)
    if len(theClient) == 1:
        theClient = theClient[0]
    else:
        return HttpResponseRedirect("/")

    if request.method == 'POST': # If the form has been submitted...
        form = CheckInForm(request.POST) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
            # Process the data in form.cleaned_data
            recordTypeId = form.cleaned_data['checkInType']
            theRecordType = RecordType.objects.filter(id=recordTypeId)
            if len(theRecordType)==1:
                theRecordType = theRecordType[0]

                theTeams = Team.objects.filter(members=theClient).filter(active=True)

                aRecord = Record()
                aRecord.name = theRecordType.name
                aRecord.points = theRecordType.points
                aRecord.date = datetime.datetime.utcnow().replace(tzinfo=utc)
                aRecord.completedBy = theClient
                aRecord.submittedBy = request.user
                aRecord.save()
                for aTeam in theTeams:
                    aRecord.teams.add(aTeam)
                        
            return HttpResponseRedirect('/clients/') # Redirect after POST
    else:
        form = CheckInForm() # An unbound form

    context = {
        'form': form,
        'client': theClient,
    }

    return render(request, 'checkInClient.html', context)