Exemple #1
0
 def _create_mentor_certificate(user, sc_slug, st_slug):
     icodes = user.created_codes.all()
     nschool = nstate = 0
     awards = defaultdict(int)
     for icode in icodes:
         for attempt in Attempt.objects.filter(access_code=icode):
             if attempt.competition.slug == sc_slug:
                 if list(attempt.confirmed_by.all()) != [user]:
                     continue
                 nschool += 1
             elif attempt.competition.slug == st_slug:
                 nstate += 1
             else:
                 continue
             for award in set(award.award.name for award in attempt.attemptaward_set.all()):
                 awards[award] += 1
     name = (user.first_name.strip() + u" " + user.last_name.strip()).title()
     text = _compose_text(name, nschool, awards)
     text = "\n".join(map(u'<tspan x="0" dy="1.2em">{}</tspan>'.format, text.splitlines()))
     cert_dir = os.path.join(settings.MEDIA_ROOT, _profile_file_path(user, ""))
     try:
         os.mkdir(cert_dir)
     except:
         pass
     template_file = os.path.join(AWARD_TEMPLATE_DIR, "certificate.svg")
     with open(template_file) as f:
         template = f.read()
     template = template.replace("ime_in_priimek", name.encode("utf-8")).replace("kategorija", text.encode("utf-8"))
     return cairosvg.svg2pdf(template)
Exemple #2
0
def school_awards_pdf(request, username, slug, school_id, cqs_name):
    profile = Profile.objects.get(user__username=username)
    if profile.user != request.user and \
            request.profile.managed_profiles.filter(
                id=profile.id).count() <= 0:
        raise PermissionDenied
    cert_dir = os.path.join(_profile_file_path(
        profile, os.path.join(slug, school_id, 'all')))
    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:
        assert False
        assert os.path.isfile(cert_full_fname)
    except Exception:
        # TODO: handle exception
        try:
            cert_full_dir = os.path.join(settings.MEDIA_ROOT, cert_dir)
            os.makedirs(cert_full_dir)
        except Exception as e:
            # TODO: handle exception
            pass
        data = []
        competition = SchoolCompetition.get_cached_by_slug(slug=slug)
        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,
                        'competition': award.attempt.competitionquestionset.competition,
                        'group': award.attempt.competitionquestionset.name,
                        'school': award.school_name,
                        'group': award.group_name,
                        'serial': award.serial,
                        'template': award.award.template,
                    }
                )
        try:
            template_dir = os.path.join(AWARD_TEMPLATE_DIR, competition.slug)
            assert os.path.isdir(template_dir)
        except Exception:
            template_dir = os.path.join(AWARD_TEMPLATE_DIR, 'default')
            # TODO: handle exception
        generate_award_pdf(cert_full_fname, data, template_dir)
    return safe_media_redirect(cert_path)
Exemple #3
0
def school_awards_pdf(request, username, slug, school_id, cqs_name):
    profile = Profile.objects.get(user__username=username)
    if profile.user != request.user and \
            request.profile.managed_profiles.filter(
                id=profile.id).count() <= 0:
        raise PermissionDenied
    cert_dir = os.path.join(_profile_file_path(
        profile, os.path.join(slug, school_id, 'all')))
    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:
        assert os.path.isfile(cert_full_fname)
    except Exception:
        # TODO: handle exception
        try:
            cert_full_dir = os.path.join(settings.MEDIA_ROOT, cert_dir)
            os.makedirs(cert_full_dir)
        except Exception as e:
            # TODO: handle exception
            pass
        data = []
        competition = SchoolCompetition.get_cached_by_slug(slug=slug)
        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,
                        'competition': award.attempt.competitionquestionset.competition,
                        'group': award.attempt.competitionquestionset.name,
                        'school': award.school_name,
                        'group': award.group_name,
                        'serial': award.serial,
                        'template': award.award.template,
                    }
                )
        try:
            template_dir = os.path.join(AWARD_TEMPLATE_DIR, competition.slug)
            assert os.path.isdir(template_dir)
        except Exception:
            template_dir = os.path.join(AWARD_TEMPLATE_DIR, 'default')
            # TODO: handle exception
        generate_award_pdf(cert_full_fname, data, template_dir)
    return safe_media_redirect(cert_path)
Exemple #4
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.profile
    # TODO update all possible awards files containing this attempt
    # print attempt
    __update_juniorattempt(attempt)
    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)
        if aaward.competitor_name != competitor_name or \
                aaward.school_name != school.name or \
                aaward.group_name != cqs.name:
            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
        while new_serial in serials:
            new_serial = "{}-{}".format(base_serial, i)
            i += 1
        award.serial = new_serial
        award.save()
        serials.add(new_serial)
    if len(awards_changed):
        cert_dir = os.path.join(
            _profile_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)
        if os.path.isfile(cert_full_fname):
            os.rename(cert_full_fname, new_cert_full_fname)

    return JsonResponse({'status': 'success'})
Exemple #5
0
def school_awards_pdf(request, username, slug, school_id, cqs_name):
    profile = Profile.objects.get(user__username=username)

    if profile.user != request.user and request.profile.managed_profiles.filter(id=profile.id).count() <= 0:
        raise PermissionDenied
    cert_dir = os.path.join(_profile_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 #6
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.profile
    # TODO update all possible awards files containing this attempt
    # print attempt
    __update_juniorattempt(attempt)
    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)
        if aaward.competitor_name != competitor_name or \
                aaward.school_name != school.name or \
                aaward.group_name != cqs.name:
            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
        while new_serial in serials:
            new_serial = "{}-{}".format(base_serial, i)
            i += 1
        award.serial = new_serial
        award.save()
        serials.add(new_serial)
    if len(awards_changed):
        cert_dir = os.path.join(_profile_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)
        if os.path.isfile(cert_full_fname):
            os.rename(cert_full_fname, new_cert_full_fname)

    return JsonResponse({'status': 'success'})
Exemple #7
0
def awards_type_pdf(request, username, slug, award_name, cqs_name):
    profile = Profile.objects.get(user__username=username)
    if profile.user != request.user and \
            request.profile.managed_profiles.filter(
                id=profile.id).count() <= 0:
        raise PermissionDenied
    cert_dir = os.path.join(_profile_file_path(
        profile, os.path.join(slug, 'by_type', award_name)))
    cert_fname = cqs_name + '-' + award_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 Exception:
        # TODO: handle exception
        try:
            cert_full_dir = os.path.join(settings.MEDIA_ROOT, cert_dir)
            os.makedirs(cert_full_dir)
        except Exception as e:
            # TODO: handle exception
            pass
        data = []
        competition = SchoolCompetition.get_cached_by_slug(slug=slug)
        awards = AttemptAward.objects.filter(
                attempt__competitionquestionset__competition=competition,
                attempt__competitionquestionset__name=cqs_name,
                award__name=award_name,
                revoked_by=None,
            ).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,
                    # 'date': '7. - 11. novembra 2016',
                    'serial': award.serial,
                    'template': award.award.template,
                }
            )
        try:
            template_dir = os.path.join(AWARD_TEMPLATE_DIR, competition.slug)
            assert os.path.isdir(template_dir)
        except Exception:
            # TODO: handle exception
            template_dir = os.path.join(AWARD_TEMPLATE_DIR, 'default')
        generate_award_pdf(cert_full_fname, data, template_dir)
    return safe_media_redirect(cert_path)
Exemple #8
0
def awards_type_pdf(request, username, slug, award_name, cqs_name):
    profile = Profile.objects.get(user__username=username)
    if profile.user != request.user and \
            request.profile.managed_profiles.filter(
                id=profile.id).count() <= 0:
        raise PermissionDenied
    cert_dir = os.path.join(_profile_file_path(
        profile, os.path.join(slug, 'by_type', award_name)))
    cert_fname = cqs_name + '-' + award_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 Exception:
        # TODO: handle exception
        try:
            cert_full_dir = os.path.join(settings.MEDIA_ROOT, cert_dir)
            os.makedirs(cert_full_dir)
        except Exception as e:
            # TODO: handle exception
            pass
        data = []
        competition = SchoolCompetition.get_cached_by_slug(slug=slug)
        awards = AttemptAward.objects.filter(
                attempt__competitionquestionset__competition=competition,
                attempt__competitionquestionset__name=cqs_name,
                award__name=award_name,
                revoked_by=None,
            ).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,
                    # 'date': '7. - 11. novembra 2016',
                    'serial': award.serial,
                    'template': award.award.template,
                }
            )
        try:
            template_dir = os.path.join(AWARD_TEMPLATE_DIR, competition.slug)
            assert os.path.isdir(template_dir)
        except Exception:
            # TODO: handle exception
            template_dir = os.path.join(AWARD_TEMPLATE_DIR, 'default')
        generate_award_pdf(cert_full_fname, data, template_dir)
    return safe_media_redirect(cert_path)
Exemple #9
0
def mentor_recognition_pdf(request, slug, username):
    profile = Profile.objects.get(user__username=username)
    if profile.user != request.user and \
            request.profile.managed_profiles.filter(
                id=profile.id).count() <= 0:
        raise PermissionDenied
    cert_fname = "bober-potrdilo-{}.pdf".format(slug)
    cert_dir = _profile_file_path(profile, slug)
    cert_path = os.path.join(cert_dir, cert_fname)
    cert_full_dir = os.path.join(settings.MEDIA_ROOT, cert_dir)
    cert_full_fname = os.path.join(cert_full_dir, cert_fname)
    try:
        assert os.path.isfile(cert_full_fname)
    except Exception:
        # TODO: handle exception
        competition = SchoolCompetition.get_cached_by_slug(slug=slug)
        try:
            os.makedirs(cert_full_dir)
        except Exception as e:
            # TODO: handle exception
            pass
        try:
            template_dir = os.path.join(AWARD_TEMPLATE_DIR, competition.slug)
            assert os.path.isdir(template_dir)
        except Exception:
            # TODO: handle exception
            template_dir = os.path.join(AWARD_TEMPLATE_DIR, 'default')
        data = []
        for recognition in profile.teacherrecognition_set.filter(
                template__competition=competition, revoked_by=None):
            d = {
                'text': recognition.text,
                'serial': recognition.serial,
                'name': recognition.recipient,
                'template': recognition.template.template
            }
            data.append(d)
        generate_award_pdf(cert_full_fname, data, template_dir)
    return safe_media_redirect(cert_path)
Exemple #10
0
def mentor_recognition_pdf(request, slug, username):
    profile = Profile.objects.get(user__username=username)
    if profile.user != request.user and \
            request.profile.managed_profiles.filter(
                id=profile.id).count() <= 0:
        raise PermissionDenied
    cert_fname = "bober-potrdilo-{}.pdf".format(slug)
    cert_dir = _profile_file_path(profile, slug)
    cert_path = os.path.join(cert_dir, cert_fname)
    cert_full_dir = os.path.join(settings.MEDIA_ROOT, cert_dir)
    cert_full_fname = os.path.join(cert_full_dir, cert_fname)
    try:
        assert os.path.isfile(cert_full_fname)
    except Exception:
        # TODO: handle exception
        competition = SchoolCompetition.get_cached_by_slug(slug=slug)
        try:
            os.makedirs(cert_full_dir)
        except Exception as e:
            # TODO: handle exception
            pass
        try:
            template_dir = os.path.join(AWARD_TEMPLATE_DIR, competition.slug)
            assert os.path.isdir(template_dir)
        except Exception:
            # TODO: handle exception
            template_dir = os.path.join(AWARD_TEMPLATE_DIR, 'default')
        data = []
        for recognition in profile.teacherrecognition_set.filter(
                template__competition=competition, revoked_by=None):
            d = {'text': recognition.text,
                 'serial': recognition.serial,
                 'name': recognition.recipient,
                 'template': recognition.template.template
                 }
            data.append(d)
        generate_award_pdf(cert_full_fname, data, template_dir)
    return safe_media_redirect(cert_path)