Esempio n. 1
0
def parse(request, documentPk):
    document = Document.objects.get(pk=documentPk)
    categories = Category.objects.all()
    runs = Run.objects.all()
    persons = Person.objects.all()
    results = Result.objects.all()
    xmlParser = Parser('/home/django/django_project' + document.xmlfile.url).select_parser()
    newRun = Run(name=xmlParser.event_data.get('name','NEPOMENOVANY BEH').strip(), date=xmlParser.event_data.get('date','1700-01-01' ), season=document.season)
    runAlreadyInDatabase = False
    for everyRun in runs:
        if everyRun.season == newRun.season and everyRun.name == newRun.name and str(everyRun.date) == newRun.date:
            runAlreadyInDatabase = True
            runInXML = everyRun
            break
    if  runAlreadyInDatabase == False:
        runInXML = newRun
        newRun.save()
    for key in xmlParser.runners_data.keys():
        categoryAlreadyInDatabase = False
        tmpCat = Category(name=key.strip())
        for runnerCategory in categories:
            if tmpCat.name == runnerCategory.name:
                categoryAlreadyInDatabase = True
                break
        if categoryAlreadyInDatabase == False:
            tmpCat.save()

        for xmlPerson in xmlParser.runners_data[key]:
            try:
                newClub = Club.objects.get(name=unicode(xmlPerson.get('club','N/A')).strip())
            except Club.DoesNotExist:
                newClub = Club(name=unicode(xmlPerson.get('club','N/A')).strip())
                newClub.save()
            newPerson = Person(first_name = xmlPerson['meno'].strip(), last_name = xmlPerson['priezvisko'].strip(), category = Category.objects.get(name=key.strip()), club= Club.objects.get(name=unicode(xmlPerson.get('club','N/A')).strip()))
            newPerson.ccard=xmlPerson.get('ccard',0)
            newPerson.person_id=xmlPerson.get('person_id',"0")
            if len(newPerson.person_id) >= 3:
                for club in Club.objects.all():
                    pass
            if newPerson.ccard is None:
                newPerson.ccard = 0
            personAlreadyInDatabase = False
            for everyPerson in persons:
                if newPerson.first_name == everyPerson.first_name and newPerson.last_name == everyPerson.last_name and newPerson.category == everyPerson.category:
                    personAlreadyInDatabase = True
                    personInXML = everyPerson
                    break
            if personAlreadyInDatabase == False:
                personInXML = newPerson
                newPerson.save()
            newRes = Result(person=Person.objects.get(pk=personInXML.pk), run=Run.objects.get(pk=runInXML.pk),start_time=xmlPerson.get('starttime','00:00:00'),finish_time=xmlPerson.get('finishtime','00:00:00'), status=xmlPerson['status'], points=Decimal("0"), position_run=xmlPerson.get('position',0))
            result_time=xmlPerson.get('time','00:00:00')
            if result_time.count(":") == 1:
                result_time = "00:" + result_time
            if result_time.count(":") > 0:
                time = result_time.encode('ascii', 'ignore')
                result_time = timeSum(time)
            newRes.result_time = result_time
            resultAlreadyInDatabase = False
            for everyResult in results:
                if personInXML == everyResult.person and runInXML == everyResult.run:
                    resultAlreadyInDatabase = True
                    break
            if resultAlreadyInDatabase == False:
                newRes.save()

    return HttpResponseRedirect(reverse('web:databasePerson'))
Esempio n. 2
0
def merge(request):
    data = pickle.loads(request.session['data'])
    model = pickle.loads(request.session['model'])
    if request.method == "POST":
        if str(model) == 'club':
            form = ClubForm(request.POST)
            if form.is_valid():
                newClub = Club(name = request.POST['name'])
                newClub.save()
                clubsToChange = Club.objects.filter(pk__in = data)
                runnersToChange = Person.objects.filter(club__in = clubsToChange)
                for runner in runnersToChange:
                    runner.club = newClub
                    runner.save()
                #deleting clubs which are now obsolete
                for club in clubsToChange:
                    club.delete()
            return HttpResponseRedirect(reverse('admin:index'))

        elif str(model) == 'category':
            form = CategoryForm(request.POST)
            if form.is_valid():
                newCategory = Category(name = request.POST['name'])
                newCategory.save()
                categoriesToChange = Category.objects.filter(pk__in = data)
                runnersToChange = Person.objects.filter(category__in = categoriesToChange)
                for runner in runnersToChange:
                    runner.category = newCategory
                    runner.save()
                #deleting categories which are now obsolete
                for category in categoriesToChange:
                    category.delete()
            return HttpResponseRedirect(reverse('admin:index'))

        elif str(model) == 'person':
            form = PersonForm(request.POST)
            if form.is_valid():
                newPerson = Person(first_name = request.POST['first_name'],last_name = request.POST['last_name'],category = Category.objects.get(pk=request.POST['category']),person_id = request.POST['person_id'],club= Club.objects.get(pk=request.POST['club']),ccard = request.POST['ccard'])
                newPerson.save()
                runnersToChange = Person.objects.filter(pk__in = data)
                for oldRunner in runnersToChange:
                    for oldRunnerRun in oldRunner.runs.all():
                        oldResult = Result.objects.get(person=oldRunner.pk,run = oldRunnerRun.pk)
                        renewResult = oldResult
                        renewResult.person = newPerson
                        renewResult.save()
                #deleting categories which are now obsolete
                for runner in runnersToChange:
                    runner.delete()
                return HttpResponseRedirect(reverse('admin:index'))
            return HttpResponseRedirect(reverse('web:index'))


    else:
        if str(model) == 'club':
            zoznam = Club.objects.filter(pk__in = data)
            form = ClubForm(initial={'name':zoznam[0].name, 'shortcut':zoznam[0].shortcut})
        elif str(model) == 'person':
            zoznam = Person.objects.filter(pk__in = data)
            form = PersonForm(initial={'first_name':zoznam[0].first_name,'last_name':zoznam[0].last_name, 'person_id':zoznam[0].person_id, 'club':zoznam[0].club, 'ccard':zoznam[0].ccard, 'category':zoznam[0].category})
        elif str(model) == 'category':
            zoznam = Category.objects.filter(pk__in = data)
            form = CategoryForm(initial={'name':zoznam[0].name})
    return render(request, 'web/merge.html', {'form':form,
                                              'zoznam':zoznam,
                                              })