<commit_before># -*- coding: utf-8 -*-

from django.shortcuts import get_object_or_404, render, redirect
from django.http import Http404, HttpResponse
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.core.paginator import InvalidPage, Paginator
from django.utils.timezone import now
from django.db import connection
from django.core.urlresolvers import reverse
from django.contrib import messages
from django.utils.translation import ugettext_lazy as _


from haystack.views import SearchView
from sendfile import sendfile


def _home_news(request):

    from main.models import HomePageNews

    news = HomePageNews.objects.filter(status='1_online', deleted=False).order_by('-pk').all()

    news = filter(lambda s: (not s.start_date or s.start_date <= now()) and (not s.end_date or s.end_date >= now()), list(news))

    return {'news': news}


def _home_accreds(request):

    from units.models import Accreditation

    return {'accreds_to_validate': Accreditation.objects.filter(end_date=None, need_validation=True)}


def _home_invoices(request):

    from accounting_tools.models import Invoice

    if request.user.rights_in_root_unit(request.user, 'SECRETARIAT') or request.user.is_superuser:
        invoices_need_bvr = Invoice.objects.filter(deleted=False, status='1_need_bvr').order_by('-pk')
        invoices_waiting = Invoice.objects.filter(deleted=False, status='2_sent').order_by('-pk')
    else:
        invoices_need_bvr = None
        invoices_waiting = filter(lambda i: i.rights_can('SHOW', request.user), Invoice.objects.filter(deleted=False, status='2_sent'))

    return {'invoices_need_bvr': invoices_need_bvr, 'invoices_waiting': invoices_waiting}


def _home_internal_transferts(request):

    from accounting_tools.models import InternalTransfer

    internaltransfer_to_validate, internaltransfer_to_account = None, None

    if request.user.rights_in_root_unit(request.user, ['TRESORERIE', 'SECRETARIAT']) or request.user.is_superuser:
        internaltransfer_to_validate = InternalTransfer.objects.filter(deleted=False, status='1_agep_validable').order_by('-pk')

    if request.user.rights_in_root_unit(request.user, 'SECRETARIAT') or request.user.is_superuser:
        internaltransfer_to_account = InternalTransfer.objects.filter(deleted=False, status='2_accountable').order_by('-pk')

    return {'internaltransfer_to_validate': internaltransfer_to_validate, 'internaltransfer_to_account': internaltransfer_to_account}


def _home_withdrawals(request):

    from accounting_tools.models import Withdrawal

    rcash_to_validate = Withdrawal.objects.filter(deleted=False, status='1_agep_validable').order_by('-desired_date')
    rcash_to_withdraw = Withdrawal.objects.filter(deleted=False, status='2_withdrawn').order_by('-withdrawn_date')
    rcash_to_justify = Withdrawal.objects.filter(deleted=False, status='3_used').order_by('-withdrawn_date')

    if not request.user.rights_in_root_unit(request.user, 'SECRETARIAT') and not request.user.is_superuser:
        rcash_to_withdraw = filter(lambda rcash: rcash.rights_can_SHOW(request.user), list(rcash_to_withdraw))
        rcash_to_justify = filter(lambda rcash: rcash.rights_can_SHOW(request.user), list(rcash_to_justify))
        rcash_to_validate = None

    return {'rcash_to_validate': rcash_to_validate, 'rcash_to_withdraw': rcash_to_withdraw, 'rcash_to_justify': rcash_to_justify}


def _home_accounting_lines(request):

    from units.models import Unit
    from accounting_main.models import AccountingLine

    lines_status_by_unit = {}

    # ça serait beaucoup trop lourd de tester toutes les lignes, on fait donc
    # de manière fausse: basée sur les droits
    for unit in Unit.objects.filter(deleted=False).order_by('name'):
        if request.user.rights_in_unit(request.user, unit, ['TRESORERIE', 'SECRETARIAT']) or request.user.is_superuser:
            lines_status_by_unit[unit] = (AccountingLine.objects.filter(deleted=False, costcenter__unit=unit, status='0_imported').count(), AccountingLine.objects.filter(deleted=False, costcenter__unit=unit, status='2_error').count())

    return {'lines_status_by_unit': lines_status_by_unit}


def _home_accounting_errors(request):

    from accounting_main.models import AccountingError

    open_errors = []

    for error in AccountingError.objects.filter(deleted=False).exclude(status='2_fixed').exclude(accounting_year__status='3_archived').order_by('pk'):
        if error.rights_can('SHOW', request.user):
            open_errors.append(error)

    return {'open_errors': open_errors}


def _home_expenseclaim(request):

    from accounting_tools.models import ExpenseClaim

    if request.user.rights_in_root_unit(request.user, ['TRESORERIE', 'SECRETARIAT']) or request.user.is_superuser:
        expenseclaim_to_validate = ExpenseClaim.objects.filter(deleted=False, status__in=['1_unit_validable', '2_agep_validable']).order_by('-pk')
    else:
        expenseclaim_to_validate = sorted(filter(lambda ec: ec.is_unit_validator(request.user), list(ExpenseClaim.objects.filter(deleted=False, status='1_unit_validable'))), key=lambda ec: -ec.pk)<commit_msg>Don't count archived year's accounting lines for home<commit_after># -*- coding: utf-8 -*-

from django.shortcuts import get_object_or_404, render, redirect
from django.http import Http404, HttpResponse
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.core.paginator import InvalidPage, Paginator
from django.utils.timezone import now
from django.db import connection
from django.core.urlresolvers import reverse
from django.contrib import messages
from django.utils.translation import ugettext_lazy as _


from haystack.views import SearchView
from sendfile import sendfile


def _home_news(request):

    from main.models import HomePageNews

    news = HomePageNews.objects.filter(status='1_online', deleted=False).order_by('-pk').all()

    news = filter(lambda s: (not s.start_date or s.start_date <= now()) and (not s.end_date or s.end_date >= now()), list(news))

    return {'news': news}


def _home_accreds(request):

    from units.models import Accreditation

    return {'accreds_to_validate': Accreditation.objects.filter(end_date=None, need_validation=True)}


def _home_invoices(request):

    from accounting_tools.models import Invoice

    if request.user.rights_in_root_unit(request.user, 'SECRETARIAT') or request.user.is_superuser:
        invoices_need_bvr = Invoice.objects.filter(deleted=False, status='1_need_bvr').order_by('-pk')
        invoices_waiting = Invoice.objects.filter(deleted=False, status='2_sent').order_by('-pk')
    else:
        invoices_need_bvr = None
        invoices_waiting = filter(lambda i: i.rights_can('SHOW', request.user), Invoice.objects.filter(deleted=False, status='2_sent'))

    return {'invoices_need_bvr': invoices_need_bvr, 'invoices_waiting': invoices_waiting}


def _home_internal_transferts(request):

    from accounting_tools.models import InternalTransfer

    internaltransfer_to_validate, internaltransfer_to_account = None, None

    if request.user.rights_in_root_unit(request.user, ['TRESORERIE', 'SECRETARIAT']) or request.user.is_superuser:
        internaltransfer_to_validate = InternalTransfer.objects.filter(deleted=False, status='1_agep_validable').order_by('-pk')

    if request.user.rights_in_root_unit(request.user, 'SECRETARIAT') or request.user.is_superuser:
        internaltransfer_to_account = InternalTransfer.objects.filter(deleted=False, status='2_accountable').order_by('-pk')

    return {'internaltransfer_to_validate': internaltransfer_to_validate, 'internaltransfer_to_account': internaltransfer_to_account}


def _home_withdrawals(request):

    from accounting_tools.models import Withdrawal

    rcash_to_validate = Withdrawal.objects.filter(deleted=False, status='1_agep_validable').order_by('-desired_date')
    rcash_to_withdraw = Withdrawal.objects.filter(deleted=False, status='2_withdrawn').order_by('-withdrawn_date')
    rcash_to_justify = Withdrawal.objects.filter(deleted=False, status='3_used').order_by('-withdrawn_date')

    if not request.user.rights_in_root_unit(request.user, 'SECRETARIAT') and not request.user.is_superuser:
        rcash_to_withdraw = filter(lambda rcash: rcash.rights_can_SHOW(request.user), list(rcash_to_withdraw))
        rcash_to_justify = filter(lambda rcash: rcash.rights_can_SHOW(request.user), list(rcash_to_justify))
        rcash_to_validate = None

    return {'rcash_to_validate': rcash_to_validate, 'rcash_to_withdraw': rcash_to_withdraw, 'rcash_to_justify': rcash_to_justify}


def _home_accounting_lines(request):

    from units.models import Unit
    from accounting_main.models import AccountingLine

    lines_status_by_unit = {}

    # ça serait beaucoup trop lourd de tester toutes les lignes, on fait donc
    # de manière fausse: basée sur les droits
    for unit in Unit.objects.filter(deleted=False).order_by('name'):
        if request.user.rights_in_unit(request.user, unit, ['TRESORERIE', 'SECRETARIAT']) or request.user.is_superuser:
            lines_status_by_unit[unit] = (AccountingLine.objects.filter(deleted=False, costcenter__unit=unit, status='0_imported').exclude(accounting_year__status='3_archived').count(), 
                                          AccountingLine.objects.filter(deleted=False, costcenter__unit=unit, status='2_error').exclude(accounting_year__status='3_archived').count())

    return {'lines_status_by_unit': lines_status_by_unit}


def _home_accounting_errors(request):

    from accounting_main.models import AccountingError

    open_errors = []

    for error in AccountingError.objects.filter(deleted=False).exclude(status='2_fixed').exclude(accounting_year__status='3_archived').order_by('pk'):
        if error.rights_can('SHOW', request.user):
            open_errors.append(error)

    return {'open_errors': open_errors}


def _home_expenseclaim(request):

    from accounting_tools.models import ExpenseClaim

    if request.user.rights_in_root_unit(request.user, ['TRESORERIE', 'SECRETARIAT']) or request.user.is_superuser:
        expenseclaim_to_validate = ExpenseClaim.objects.filter(deleted=False, status__in=['1_unit_validable', '2_agep_validable']).order_by('-pk')
    else:
        expenseclaim_to_validate = sorted(filter(lambda ec: ec.is_unit_validator(request.user), list(ExpenseClaim.objects.filter(deleted=False, status='1_unit_validable'))), key=lambda ec: -ec.pk)
{'fraction_non_alphanumeric': 0.10543343800551908, 'fraction_numerical': 0.004567513559805881, 'mean_word_length': 5.581089542892924, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1838759', 'n_tokens_mistral': 3415, 'n_tokens_neox': 3304, 'n_words': 594}