Exemple #1
0
def award_pdf(request, slug, school_id, cqs_name):
    profile = request.user.profile
    cert_dir = os.path.join(_user_file_path(profile, 
        os.path.join(slug, school_id)))
    cert_fname = cqs_name + '.pdf'
    cert_path = os.path.join(cert_dir, cert_fname)
    cert_full_fname = os.path.join(settings.MEDIA_ROOT, cert_path)
    try:
        # print "f:", os.path.join(settings.MEDIA_ROOT, cert_path)
        assert os.path.isfile(cert_full_fname)
    except:
        try:
            cert_full_dir = os.path.join(settings.MEDIA_ROOT, cert_dir)
            os.makedirs(cert_full_dir)
        except Exception, e:
            pass
        #    print e
        # regenerate award. Ignore the template
        template_file = os.path.join(AWARD_TEMPLATE_DIR, 'all_si.svg')
        #print "generating..."
        data = []
        competition = SchoolCompetition.get_cached_by_slug(slug=slug)
        #print cqs_name
        #for i in profile.schoolteachercode_set.all():
        #    print "  ", i.competition_questionset.name
        #print profile.schoolteachercode_set.filter(school_id = school_id,
        #    competition_questionset__name=cqs_name)
        stcs = profile.schoolteachercode_set.filter(
                    code__codegenerator = competition.competitor_code_generator,
                    competition_questionset__name = cqs_name,
                    school_id = school_id
                ).order_by(
                    'code'
                ).prefetch_related(
                    'code')
        # print stcs
        for stc in stcs:
            stc.assign_si_awards(revoked_by = profile)
            awards = stc.attempt_awards().order_by(
                    'attempt__competitor__last_name',
                    'attempt__competitor__first_name'
                ).select_related(
                    'award')
            for award in awards:
                data.append(
                    {
                        'name': award.competitor_name,
                        'school': award.school_name,
                        'group': award.group_name,
                        'serial': award.serial,
                        'template': award.award.template,
                    }
                )
        generate_award_pdf(cert_full_fname,
            data, template_file)
Exemple #2
0
def revalidate_awards(request, attempt_id, *args, **kwargs):
    attempt = get_object_or_404(Attempt, id=attempt_id)
    # TODO check permissions, determine the actual teacher
    teacher = request.user.profile
    # TODO update all possible awards files containing this attempt
    # print attempt
    __update_juniorattempt(attempt)
    awards_changed = False
    sct = teacher.schoolteachercode_set.get(
        code__value = attempt.access_code,
        competition_questionset = attempt.competitionquestionset
    )
    cqs = sct.competition_questionset
    school = sct.school
    awards_changed = []
    serials = set()
    for aaward in attempt.attemptaward_set.filter(revoked_by = None):
        serials.add(aaward.serial)
        competitor_name = u"{} {}".format(attempt.competitor.first_name,
            attempt.competitor.last_name)
        # print "  ", aaward.competitor_name, competitor_name
        if aaward.competitor_name != competitor_name or \
                aaward.school_name != school.name or \
                aaward.group_name != cqs.name:
            # print "changed!"
            aaward.revoked_by = teacher
            aaward.save()
            aaward.id = None
            aaward.competitor_name = competitor_name
            aaward.school_name = school.name
            aaward.revoked_by = None
            aaward.group_name = cqs.name
            awards_changed.append(aaward)
    for award in awards_changed:
        base_serial = award.serial
        p = base_serial.rfind('-')
        if p >= 0:
            base_serial = base_serial[:p]
        new_serial = base_serial
        i = 1
        # print serials
        while new_serial in serials:
            new_serial = "{}-{}".format(base_serial, i)
            i += 1
        award.serial = new_serial
        # print "created award", award.serial, award, award.revoked_by
        award.save()
        serials.add(new_serial)
    if True or len(awards_changed):
        cert_dir = os.path.join(_user_file_path(teacher, 
            os.path.join(cqs.competition.slug, str(school.id))))
        cert_fname = cqs.name + '.pdf'
        cert_path = os.path.join(cert_dir, cert_fname)
        cert_full_fname = os.path.join(settings.MEDIA_ROOT, cert_path)
        new_cert_full_fname = cert_full_fname
        i = 0
        while os.path.isfile(new_cert_full_fname):
            i += 1
            new_cert_full_fname = u'{}-{}'.format(cert_full_fname, i)
        # print "new file name:", new_cert_full_fname
        os.rename(cert_full_fname, new_cert_full_fname)

    return JsonResponse({'status': 'success'})