<commit_before>from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from activity.models import ProjectActivities, Seminar, SeminarType
from member.models import Person
from django.utils.translation import ugettext_lazy as _
import datetime
from django.contrib import messages
from django.shortcuts import get_object_or_404
from django.http import Http404
from django.http import HttpResponse
import json as simplejson

TIME = " 00:00:00"


def start_date_typed(start_date):
    start_day = start_date[0:2]
    start_month = start_date[3:5]
    start_year = start_date[6:10]
    start_date = start_year+start_month+start_day+TIME
    start_date = datetime.datetime.strptime(start_date, "%Y%m%d %H:%M:%S").date()
    start_date -= datetime.timedelta(days=1)
    return start_date


def end_date_typed(end_date):
    end_day = end_date[0:2]
    end_month = end_date[3:5]
    end_year = end_date[6:10]
    end_date = end_year+end_month+end_day+TIME
    end_date = datetime.datetime.strptime(end_date, "%Y%m%d %H:%M:%S").date()
    end_date += datetime.timedelta(days=1)
    return end_date


def now_plus_thirty():
    date = datetime.datetime.now() + datetime.timedelta(days=30)
    date = date.strftime("%Y%m%d %H:%M:%S")
    date = datetime.datetime.strptime(date, '%Y%m%d %H:%M:%S').date()
    return date


@login_required
def seminars_report(request):

    categories = SeminarType.objects.all()

    if request.method == 'POST':
        start_date = request.POST['start_date']
        if start_date:
            start_date = start_date_typed(start_date)
        else:
            start_date = datetime.datetime.strptime('19700101 00:00:00', '%Y%m%d %H:%M:%S').date()

        end_date = request.POST['end_date']
        if end_date:
            end_date = end_date_typed(end_date)
        else:
            end_date = now_plus_thirty()

        category = request.POST['category']

        if category == '0':
            seminars = ProjectActivities.objects.filter(type_of_activity='s',
                                                        seminar__date__gt=start_date,
                                                        seminar__date__lt=end_date).order_by('seminar__date')
        else:
            seminars = ProjectActivities.objects.filter(type_of_activity='s', seminar__category=category,
                                                        seminar__date__gt=start_date,
                                                        seminar__date__lt=end_date).order_by('seminar__date')

        if end_date >= start_date:
            context = {'seminars': seminars}
            return render(request, 'report/seminars_report.html', context)
        else:
            messages.error(request, _('End date should be equal or greater than start date.'))
            return render(request, 'report/seminars.html')

    context = {'categories': categories}

    return render(request, 'report/seminars.html', context)


@login_required
def seminar_poster(request):

    speakers = Person.objects.all()
    seminars = ProjectActivities.objects.filter(type_of_activity='s')

    if request.method == 'POST':
        title_id = request.POST['title']

        if title_id is None or title_id == '':
            messages.error(request, _('You have to choose a seminar!'))
            context = {'speakers': speakers, 'seminars': seminars}
            return render(request, 'poster/seminar.html', context)
        else:
            try:
                seminar = Seminar.objects.get(id=title_id)
            except Seminar.DoesNotExist:
                raise Http404(_('No seminar matches the given query.'))

            context = {'seminar': seminar}
            return render(request, 'poster/seminar_poster.html', context)

    context = {'speakers': speakers, 'seminars': seminars}
    return render(request, 'poster/seminar.html', context)


@login_required
def seminar_show_titles(request):
    if request.method == 'GET':
        speaker_id = request.GET.get('speaker')
        speaker = get_object_or_404(Person, id=speaker_id)

        select = ProjectActivities.objects.filter(type_of_activity='s', speaker=speaker)
        titles = []
        for title in select:
            titles.append({'pk': title.id, 'valor': title.__unicode__()})

        json = simplejson.dumps(titles)
        return HttpResponse(json, content_type="application/json")


@login_required
def training_programs_report(request):

    if request.method == 'POST':
        start_date = request.POST['start_date']
        if start_date:
            start_date = start_date_typed(start_date)<commit_msg>Added defs to create a pdf poster<commit_after>from django.shortcuts import render
from django.contrib.auth.decorators import login_required
from activity.models import ProjectActivities, Seminar, SeminarType
from member.models import Person
from django.utils.translation import ugettext_lazy as _
import datetime
from django.contrib import messages
from django.shortcuts import get_object_or_404
from django.http import Http404
from django.http import HttpResponse
import json as simplejson
## Imports to generete PDF file ##
import StringIO
from cgi import escape
from xhtml2pdf import pisa
from django.template.loader import get_template
from django.template import Context
import os
from django.conf import settings

TIME = " 00:00:00"


def fetch_resources(uri, rel):
    path = os.path.join(settings.STATIC_ROOT, uri.replace(settings.STATIC_URL, ""))
    return path


def render_to_pdf(template_src, context_dict):
    template = get_template(template_src)
    context = Context(context_dict)
    html  = template.render(context)
    result = StringIO.StringIO()

    pdf = pisa.pisaDocument(StringIO.StringIO(html.encode("UTF-8")), dest=result, encoding='UTF-8',
                            link_callback=fetch_resources)

    if not pdf.err:
        return HttpResponse(result.getvalue(), content_type='application/pdf')

    return HttpResponse('_(We had some errors<pre>%s</pre>)' % escape(html))


def start_date_typed(start_date):
    start_day = start_date[0:2]
    start_month = start_date[3:5]
    start_year = start_date[6:10]
    start_date = start_year+start_month+start_day+TIME
    start_date = datetime.datetime.strptime(start_date, "%Y%m%d %H:%M:%S").date()
    start_date -= datetime.timedelta(days=1)
    return start_date


def end_date_typed(end_date):
    end_day = end_date[0:2]
    end_month = end_date[3:5]
    end_year = end_date[6:10]
    end_date = end_year+end_month+end_day+TIME
    end_date = datetime.datetime.strptime(end_date, "%Y%m%d %H:%M:%S").date()
    end_date += datetime.timedelta(days=1)
    return end_date


def now_plus_thirty():
    date = datetime.datetime.now() + datetime.timedelta(days=30)
    date = date.strftime("%Y%m%d %H:%M:%S")
    date = datetime.datetime.strptime(date, '%Y%m%d %H:%M:%S').date()
    return date


@login_required
def seminars_report(request):

    categories = SeminarType.objects.all()

    if request.method == 'POST':
        start_date = request.POST['start_date']
        if start_date:
            start_date = start_date_typed(start_date)
        else:
            start_date = datetime.datetime.strptime('19700101 00:00:00', '%Y%m%d %H:%M:%S').date()

        end_date = request.POST['end_date']
        if end_date:
            end_date = end_date_typed(end_date)
        else:
            end_date = now_plus_thirty()

        category = request.POST['category']

        if category == '0':
            seminars = ProjectActivities.objects.filter(type_of_activity='s',
                                                        seminar__date__gt=start_date,
                                                        seminar__date__lt=end_date).order_by('seminar__date')
        else:
            seminars = ProjectActivities.objects.filter(type_of_activity='s', seminar__category=category,
                                                        seminar__date__gt=start_date,
                                                        seminar__date__lt=end_date).order_by('seminar__date')

        if end_date >= start_date:
            context = {'seminars': seminars}
            return render(request, 'report/seminars_report.html', context)
        else:
            messages.error(request, _('End date should be equal or greater than start date.'))
            return render(request, 'report/seminars.html')

    context = {'categories': categories}

    return render(request, 'report/seminars.html', context)


@login_required
def seminar_poster(request):

    speakers = Person.objects.all()
    seminars = ProjectActivities.objects.filter(type_of_activity='s')

    if request.method == 'POST':
        title_id = request.POST['title']

        if title_id is None or title_id == '':
            messages.error(request, _('You have to choose a seminar!'))
            context = {'speakers': speakers, 'seminars': seminars}
            return render(request, 'poster/seminar.html', context)
        else:
            try:
                seminar = Seminar.objects.get(id=title_id)
            except Seminar.DoesNotExist:
                raise Http404(_('No seminar matches the given query.'))

            #context = {'seminar': seminar}
            #return render(request, 'poster/seminar_poster.html', context)
            return render_to_pdf(
                'poster/seminar_poster_pdf.html',
                {
                    'pagesize':'A4',
                    'seminar': seminar,
                }
            )

    context = {'speakers': speakers, 'seminars': seminars}
    return render(request, 'poster/seminar.html', context)


@login_required
def seminar_show_titles(request):
    if request.method == 'GET':
        speaker_id = request.GET.get('speaker')
        speaker = get_object_or_404(Person, id=speaker_id)

        select = ProjectActivities.objects.filter(type_of_activity='s', speaker=speaker)
        titles = []
        for title in select:
            titles.append({'pk': title.id, 'valor': title.__unicode__()})

        json = simplejson.dumps(titles)
        return HttpResponse(json, content_type="application/json")


@login_required
def training_programs_report(request):

    if request.method == 'POST':
        start_date = request.POST['start_date']
        if start_date:
            start_date = start_date_typed(start_date)
{'fraction_non_alphanumeric': 0.10868519765316918, 'fraction_numerical': 0.01019524862941233, 'mean_word_length': 2.776970577551762, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4846480', 'n_tokens_mistral': 3205, 'n_tokens_neox': 3035, 'n_words': 628}