コード例 #1
0
ファイル: views.py プロジェクト: BrassBandResults/bbr4
def enter_results(request, pContestSlug, pDate):
    """
    Enter the actual results of the contest
    """
    lForm = ResultsForm()
    lHowToCorrectErrors = ''
    try:
        lContest = Contest.objects.filter(slug=pContestSlug)[0]
        lContestEvent = ContestEvent.objects.filter(contest=lContest,
                                                    date_of_event=pDate)[0]
    except IndexError:
        raise Http404()

    # if flag is set, forward to adjudicator and don't allow results to be added
    lToday = date.today()
    if lToday < lContestEvent.date_of_event and lContest.prevent_future_bands:
        return HttpResponseRedirect('/addresults/%s/%s/6/' %
                                    (pContestSlug, pDate))

    if request.POST:
        try:
            lRadioSelection = request.POST['conductor_choice']
            if lRadioSelection == 'newconductor':
                lNewConductorPerson = Person()
                lNewConductorPerson.name = request.POST['conductor']
                lNewConductorPerson.slug = slugify(
                    lNewConductorPerson.name, instance=lNewConductorPerson)
                lNewConductorPerson.lastChangedBy = request.user
                lNewConductorPerson.owner = request.user
                lNewConductorPerson.save()
                notification(None, lNewConductorPerson, 'people', 'person',
                             'new', request.user, browser_details(request))

            elif lRadioSelection == 'alias':
                lPreviousConductorName = request.POST['conductoralias']
                lConductorSerial = request.POST['conductorid']

                lConductor = Person.objects.filter(id=int(lConductorSerial))[0]
                lPreviousName = PersonAlias()
                lPreviousName.name = lPreviousConductorName
                lPreviousName.person = lConductor
                lPreviousName.lastChangedBy = request.user
                lPreviousName.owner = request.user
                lPreviousName.save()
                notification(None, lPreviousName, 'people', 'person_alias',
                             'new', request.user, browser_details(request))

                ## TODO create person alias here - we'll need to pass through the person id, not the conductor id

        except MultiValueDictKeyError:
            pass

        try:
            lRadioSelection = request.POST['band']
            if lRadioSelection == 'newband':
                # create a new band
                lNewBandName = request.POST['newbandname']
                lNewBandRegion = request.POST['newbandregion']
                lNewBand = Band()
                lNewBand.name = lNewBandName
                lNewBand.slug = slugify(lNewBandName, instance=lNewBand)
                lNewBand.region = Region.objects.filter(id=lNewBandRegion)[0]
                lNewBand.owner = request.user
                lNewBand.lastChangedBy = request.user
                lNewBand.save()
                notification(None, lNewBand, 'bands', 'band', 'new',
                             request.user, browser_details(request))
            elif lRadioSelection == 'nameonly':
                lPreviousBandName = request.POST['oldbandname']
                lBandSerial = request.POST['bandid']
                lBand = Band.objects.filter(id=int(lBandSerial))[0]
                lPreviousName = PreviousBandName()
                lPreviousName.old_name = lPreviousBandName
                lPreviousName.band = lBand
                lPreviousName.lastChangedBy = request.user
                lPreviousName.owner = request.user
                lPreviousName.save()
                notification(None, lPreviousName, 'bands', 'band_alias', 'new',
                             request.user, browser_details(request))
        except MultiValueDictKeyError:
            pass

        lForm = ResultsForm(request.POST)
        lForm.event = lContestEvent
        if lForm.is_valid():
            lForm.save(request, lContestEvent)
            return HttpResponseRedirect('/addresults/%s/%s/6/' %
                                        (pContestSlug, pDate))
        else:
            lFormErrors = str(lForm.errors['results'])
            if lFormErrors.startswith(_CONDUCTOR_PREFIX):
                lConductorName = lFormErrors[len(_CONDUCTOR_PREFIX):-15]
                lConductorDropList = '<select name="conductorid">\n'
                lConductors = Person.objects.all()
                for conductor in lConductors:
                    lAdd = True
                    if conductor.end_date and lContestEvent.date_of_event > conductor.end_date:
                        lAdd = False
                    elif conductor.start_date and lContestEvent.date_of_event < conductor.start_date:
                        lAdd = False
                    if lAdd:
                        lConductorDropList = lConductorDropList + '<option value="%s">%s, %s</option>\n' % (
                            conductor.id, conductor.surname,
                            conductor.first_names)
                lConductorDropList = lConductorDropList + '</select>'

                lHowToCorrectErrors = """<input type="radio" name="conductor_choice" value="newconductor"/>Next submit will create a new conductor called: <input type="text" name="conductor" value="%s"/><br/>
                                         <input type="radio" name="conductor_choice" value="alias"/>Next submit will add a conductor alias of <b>%s</b> to %s<input type="hidden" name="conductoralias" value="%s"/><br/>
                                         <input type="radio" name="conductor_choice" value="nothing" checked="checked"/>Do nothing, correct it in the text box below.""" % (
                    lConductorName, lConductorName, lConductorDropList,
                    lConductorName)
            if lFormErrors.startswith(_BAND_PREFIX):
                lBandName = lFormErrors[len(_BAND_PREFIX):-15]
                #lBandName = lFormErrors[len(_BAND_PREFIX):-15]
                lBandDropList = '<select name="bandid">\n'
                lBands = Band.objects.all()
                for band in lBands:
                    lAdd = True
                    if band.end_date and lContestEvent.date_of_event > band.end_date:
                        lAdd = False
                    elif band.start_date and lContestEvent.date_of_event < band.start_date:
                        lAdd = False
                    if lAdd:
                        lBandDropList = lBandDropList + '<option value="%s">%s</option>\n' % (
                            band.id, band.name)
                lBandDropList = lBandDropList + '</select>'
                lRegionDropList = ""
                lContestRegion = "Unknown"
                if lContest.region:
                    lContestRegion = lContest.region.name
                for region in Region.objects.all():
                    lRegionDropList += "<option value='" + str(region.id) + "'"
                    if region.name == lContestRegion:
                        lRegionDropList += " selected='selected'"
                    lRegionDropList += ">" + region.name + "</option>\n"
                lHowToCorrectErrors = """You have two choices to fix this problem.  You can either create a new band, or assign this name as an old name of an already existing band.  Use existing bands where possible.<br/>
                                         <input type="radio" name="band" value="newband"/>Next submit will create a new band called: <input type="text" name="newbandname" value="%s"/> in <select name="newbandregion">%s</select> region<br/>
                                         <input type="radio" name="band" value="nameonly"/>Next submit will add a previous band name to %s called <b>%s</b><input type="hidden" name="oldbandname" value="%s"/><br/>
                                         <input type="radio" name="band" value="nothing" checked="checked"/>Do nothing, correct it in the text box below.
                                         """ % (lBandName, lRegionDropList,
                                                lBandDropList, lBandName,
                                                lBandName)

    return render_auth(
        request, 'addresults/bands.html', {
            "Contest": lContest,
            "ContestEvent": lContestEvent,
            "form": lForm,
            "HowToCorrectErrors": lHowToCorrectErrors,
        })
コード例 #2
0
ファイル: views.py プロジェクト: BrassBandResults/bbr4
def enter_results(request, pContestSlug, pDate):
    """
    Enter the actual results of the contest
    """
    lHowToCorrectErrors = ''
    lYear, lMonth, lDay = pDate.split('-')
    lContestDate = date(year=int(lYear), month=int(lMonth), day=int(lDay))
    try:
        lContestGroup = ContestGroup.objects.filter(slug=pContestSlug)[0]
    except IndexError:
        raise Http404()
    lForm = ResultsForm(lContestGroup)

    if request.POST:
        try:
            lRadioSelection = request.POST['band']
            if lRadioSelection == 'newband':
                # create a new band
                lNewBandName = request.POST['newbandname']
                lNewBand = Band()
                lNewBand.name = lNewBandName
                lNewBand.slug = slugify(lNewBandName, instance=lNewBand)
                lNewBand.region = Region.objects.filter(name='Unknown')[0]
                lNewBand.owner = request.user
                lNewBand.lastChangedBy = request.user
                lNewBand.save()
            elif lRadioSelection == 'nameonly':
                lPreviousBandName = request.POST['oldbandname']
                lBandSerial = request.POST['bandid']
                lBand = Band.objects.filter(id=int(lBandSerial))[0]
                lPreviousName = PreviousBandName()
                lPreviousName.old_name = lPreviousBandName
                lPreviousName.band = lBand
                lPreviousName.owner = request.user
                lPreviousName.lastChangedBy = request.user
                lPreviousName.save()
        except MultiValueDictKeyError:
            pass

        lForm = ResultsForm(lContestGroup, request.POST)
        if lForm.is_valid():
            lForm.save(request, lContestGroup, lContestDate)
            return HttpResponseRedirect('/contests/%s/' %
                                        lContestGroup.actual_slug)
        else:
            lFormErrors = str(lForm.errors['__all__'])
            if lFormErrors.startswith(_BAND_PREFIX):
                lBandName = lFormErrors[len(_BAND_PREFIX):-15]
                lBandDropList = '<select name="bandid">\n'
                lBands = Band.objects.all()
                for band in lBands:
                    lBandDropList = lBandDropList + '<option value="%s">%s</option>\n' % (
                        band.id, band.name)
                lBandDropList = lBandDropList + '</select>'
                lHowToCorrectErrors = """You have two choices to fix this problem.  You can either create a new band, or assign this name as an old name of an already existing band.  Use existing bands where possible.<br/>
                                         <input type="radio" name="band" value="newband"/>Next submit will create a new band called: <input type="text" name="newbandname" value="%s"/><br/>
                                         <input type="radio" name="band" value="nameonly"/>Next submit will add a previous band name to %s called <b>%s</b><input type="hidden" name="oldbandname" value="%s"/><br/>
                                         <input type="radio" name="band" value="nothing" checked="checked"/>Do nothing, correct it in the text box below.
                                         """ % (lBandName, lBandDropList,
                                                lBandName, lBandName)

    return render_auth(
        request, 'addwhitfriday/bands.html', {
            "ContestGroup": lContestGroup,
            "ContestDate": lContestDate,
            "form": lForm,
            "HowToCorrectErrors": lHowToCorrectErrors,
        })