示例#1
0
def user(request, username):
    if not user_check(request, username):
        return HttpResponse("Restricted to " + username)
    if request.method == "POST":
        if request.POST.get("IN"):
            Swipe.objects.create(
                swipe_type="IN",
                user=request.user,
                datetime=timezone.now(),
            )
        if request.POST.get("OUT"):
            Swipe.objects.create(
                swipe_type="OUT",
                user=request.user,
                datetime=timezone.now(),
            )
        if request.POST.get("OBR"):
            Swipe.objects.create(
                swipe_type="OBR",
                user=request.user,
                datetime=timezone.now(),
            )
        if request.POST.get("FBR"):
            Swipe.objects.create(
                swipe_type="FBR",
                user=request.user,
                datetime=timezone.now(),
            )
        if request.POST.get("OTR"):
            Swipe.objects.create(
                swipe_type="OTR",
                user=request.user,
                datetime=timezone.now(),
            )
        if request.POST.get("FTR"):
            Swipe.objects.create(
                swipe_type="FTR",
                user=request.user,
                datetime=timezone.now(),
            )
        if request.POST.get("OUTUSER"):
            if (request.user.is_staff) or (request.user.is_superuser):
                if request.POST.get("OUTUSER") == "SwipeUserOut":
                    Swipe.objects.create(
                        swipe_type="OUT",
                        user=User.objects.get(
                            username=(request.POST.get("username"))),
                        datetime=timezone.now(),
                    )
                elif request.POST.get("OUTUSER") == "BreakUserOut":
                    Swipe.objects.create(
                        swipe_type="FBR",
                        user=User.objects.get(
                            username=(request.POST.get("username"))),
                        datetime=timezone.now(),
                    )
                    Swipe.objects.create(
                        swipe_type="OUT",
                        user=User.objects.get(
                            username=(request.POST.get("username"))),
                        datetime=timezone.now(),
                    )
                elif request.POST.get("OUTUSER") == "TripUserOut":
                    Swipe.objects.create(
                        swipe_type="FTR",
                        user=User.objects.get(
                            username=(request.POST.get("username"))),
                        datetime=timezone.now(),
                    )
                    Swipe.objects.create(
                        swipe_type="OUT",
                        user=User.objects.get(
                            username=(request.POST.get("username"))),
                        datetime=timezone.now(),
                    )
        return HttpResponseRedirect(reverse(user,
                                            args=[request.user.username]))
        # hard reload the page without any forms
    u = User.objects.get(username=username)
    s = Session.objects.get_sessions_this_month(user=u)

    open_sessions = Session.objects.get_open_sessions()
    try:
        current_session = open_sessions.filter(user=u)[0]
        current_session_work_hours = current_session.session_duration(
        ).seconds / 3600
    except:
        current_session = None
        current_session_work_hours = 0

    latest_swipes = []

    for session in open_sessions:
        latest_swipes.append(
            Swipe.objects.filter(session=session).order_by("-datetime")[0])

    at_work_users, on_break_users, on_trip_users = [], [], []

    for swipe in latest_swipes:
        if swipe.swipe_type == "IN" or swipe.swipe_type == "FBR" or swipe.swipe_type == "FTR":
            at_work_users.append(swipe.user)
        elif swipe.swipe_type == "OBR":
            on_break_users.append(swipe.user)
        elif swipe.swipe_type == "OTR":
            on_trip_users.append(swipe.user)

    try:
        last_swipe = Swipe.objects.filter(user=u).order_by("-datetime")[0]
        next_swipes = last_swipe.get_next_allowed_types()
    except IndexError:
        last_swipe = None
        next_swipes = ('IN', )  # empty database - first swipe is IN

    last_month_ = last_month()
    if last_month == 12:
        year = datetime.now().year - 1
    else:
        year = datetime.now().year

    hours_total_last_month = Session.objects.get_hours_month(
        u.id, last_month_, year)
    hours_unassigned_last_month = Session.objects.get_unassigned_hours_month(
        u.id, last_month_, year)
    hours_total_this_month = Session.objects.get_hours_this_month(u.id)
    hours_unassigned_this_month = Session.objects.get_unassigned_hours_month(
        u.id,
        datetime.now().month, year)
    hours_not_work_this_month = Session.objects.get_not_work_hours_month(
        u.id,
        datetime.now().month,
        datetime.now().year)
    hours_not_work_last_month = Session.objects.get_not_work_hours_month(
        u.id, last_month_, year)
    hours_work_last_month = hours_total_last_month - hours_unassigned_last_month - hours_not_work_last_month
    hours_work_this_month = hours_total_this_month - hours_unassigned_this_month - hours_not_work_this_month

    num_of_workdays = get_number_of_work_days(date.today().year,
                                              date.today().month)
    unassigned_closed_session_hours = hours_unassigned_this_month - current_session_work_hours
    hours_quota = get_quota_work_hours(datetime.now().year,
                                       datetime.now().month, WORKHOURS_PER_DAY)
    num_of_elapsed_workdays = get_num_of_elapsed_workdays_in_month(
        date.today())
    num_of_elapsed_workdays_last_month = get_number_of_work_days(
        datetime.now().year, last_month_)
    current_quota = num_of_elapsed_workdays * WORKHOURS_PER_DAY
    hours_quota_last_month = num_of_elapsed_workdays_last_month * WORKHOURS_PER_DAY
    quota_difference = hours_work_this_month + unassigned_closed_session_hours - current_quota
    quota_difference_abs = abs(quota_difference)
    hours_quota_dif_last_month = hours_work_last_month + hours_unassigned_last_month - hours_quota_last_month
    avg_work_hours_fullfill_quota = daily_hours(
        (hours_quota - unassigned_closed_session_hours - hours_work_this_month)
        / max(1, num_of_workdays - num_of_elapsed_workdays))

    context = {
        "user": u,
        "session_list": s,
        "hours_total_this_month": hours_total_this_month,
        "hours_unassigned_this_month": hours_unassigned_this_month,
        "last_swipe": last_swipe,
        "next_swipes": next_swipes,
        "at_work_users": at_work_users,
        "on_break_users": on_break_users,
        "on_trip_users": on_trip_users,
        "hours_total_last_month": hours_total_last_month,
        "hours_unassigned_last_month": hours_unassigned_last_month,
        "hours_not_work_this_month": hours_not_work_this_month,
        "hours_not_work_last_month": hours_not_work_last_month,
        "hours_work_last_month": hours_work_last_month,
        "hours_work_this_month": hours_work_this_month,
        "hours_quota": hours_quota,
        "current_session_work_hours": current_session_work_hours,
        "current_session": current_session,
        "unassigned_closed_session_hours": unassigned_closed_session_hours,
        "num_of_elapsed_workdays": num_of_elapsed_workdays,
        "num_of_workdays": num_of_workdays,
        "current_quota": current_quota,
        "quota_difference": quota_difference,
        "quota_difference_abs": quota_difference_abs,
        "avg_work_hours_fullfill_qoota": avg_work_hours_fullfill_quota,
        "workhours_per_day": WORKHOURS_PER_DAY,
        "hours_quota_last_month": hours_quota_last_month,
        "hours_quota_dif_last_month": hours_quota_dif_last_month,
    }
    return render(request, "attendance/user_page.html", context)
示例#2
0
from django.conf import settings
import time
import os, django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ticker.settings")
django.setup()
import attendance.utils as ut

from attendance.models import Swipe, Session
from django.contrib.auth.models import User
from attendance import factories

from django.core.mail import send_mail
from django.template.loader import render_to_string
from datetime import date, timedelta, datetime
from attendance.managers import SessionManager

def print_doc_str_and_return_value(functions_iterable):
    '''
    Takes list of functions and prints thier docstring with retun values
    '''
    for function in functions_iterable:
        print(function.__doc__.strip(), str(function()))
now = time.time()

user = User.objects.get(username="******")
month = datetime.now().month-1

Session.objects.get_sessions_month(user, month)

print(ut.get_quota_work_hours(2016, 8, 8))
示例#3
0
def sessions_month(request,
                   username,
                   year=datetime.now().year,
                   month=datetime.now().month):
    if not user_check(request, username):
        return HttpResponse("Restricted to " + username)

    if request.method == "POST":
        form = ProjectSeparationForm(request.POST)
        if form.is_valid():
            form.save()
            url = reverse('sessions_month',
                          kwargs={
                              "username": username,
                              "year": year,
                              "month": month
                          })
            return HttpResponseRedirect(url)

    in_swipes_ids = Swipe.objects.filter(
        swipe_type="IN",
        user__username=username,
        datetime__month=int(month),
        datetime__year=int(year),
    ).values_list('session', flat=True)
    sessions = Session.objects.filter(pk__in=in_swipes_ids)
    u = User.objects.get(username=username)

    separations = ProjectSeparation.objects.filter(session__in=sessions)

    form = ProjectSeparationForm()

    for session in sessions:
        session.form = ProjectSeparationForm(
            initial={
                "time_spend": session.get_not_assigned_duration(),
                "session": session.id  # hidden form
            }, )

    projects = dict()

    for separation in separations:
        if separation.project.name in projects.keys():
            projects[separation.project.
                     name] += separation.time_spend.seconds / 3600
        else:
            projects[
                separation.project.name] = separation.time_spend.seconds / 3600

    not_work_hours = Session.objects.get_not_work_hours_month(u, month, year)
    total_hours = Session.objects.get_hours_month(u.id, month, year)
    unassigned_hours = Session.objects.get_unassigned_hours_month(
        u.id, month, year)
    work_hours = total_hours - unassigned_hours - not_work_hours

    this_year = datetime.now().year
    chooseable_years = []
    for i in range(this_year, 2014, -1):
        chooseable_years.append(i)

    context = {
        "sessions":
        sessions,
        "year":
        year,
        "month":
        month,
        "total_hours":
        total_hours,
        "unassigned_hours":
        unassigned_hours,
        "work_hours":
        work_hours,
        "not_work_hours":
        not_work_hours,
        "list_of_projects":
        projects,
        "hours_quota":
        get_quota_work_hours(int(year), int(month), WORKHOURS_PER_DAY),
        "form":
        form,
        "chooseable_years":
        chooseable_years,
    }
    return render(request, "attendance/sessions.html", context)
示例#4
0
def user(request, username):
    if not user_check(request, username):
        return HttpResponse("Restricted to " + username)
    if request.method == "POST":
        if request.POST.get("IN"):
            Swipe.objects.create(
                swipe_type="IN",
                user=request.user,
                datetime=timezone.now(),
            )
        if request.POST.get("OUT"):
            Swipe.objects.create(
                swipe_type="OUT",
                user=request.user,
                datetime=timezone.now(),
            )
        if request.POST.get("OBR"):
            Swipe.objects.create(
                swipe_type="OBR",
                user=request.user,
                datetime=timezone.now(),
            )
        if request.POST.get("FBR"):
            Swipe.objects.create(
                swipe_type="FBR",
                user=request.user,
                datetime=timezone.now(),
            )
        if request.POST.get("OTR"):
            Swipe.objects.create(
                swipe_type="OTR",
                user=request.user,
                datetime=timezone.now(),
            )
        if request.POST.get("FTR"):
            Swipe.objects.create(
                swipe_type="FTR",
                user=request.user,
                datetime=timezone.now(),
            )
    u = User.objects.get(username=username)
    s = Session.objects.get_sessions_this_month(user=u)

    open_sessions = Session.objects.get_open_sessions()
    try:
        current_session = open_sessions.filter(user=u)[0]
        current_session_work_hours = current_session.session_duration(
        ).seconds / 3600
    except:
        current_session = None
        current_session_work_hours = 0

    latest_swipes = []

    for session in open_sessions:
        latest_swipes.append(
            Swipe.objects.filter(session=session).order_by("-datetime")[0])

    at_work_users, on_break_users, on_trip_users = [], [], []

    for swipe in latest_swipes:
        if swipe.swipe_type == "IN" or swipe.swipe_type == "FBR" or swipe.swipe_type == "FTR":
            at_work_users.append(swipe.user)
        elif swipe.swipe_type == "OBR":
            on_break_users.append(swipe.user)
        elif swipe.swipe_type == "OTR":
            on_trip_users.append(swipe.user)

    try:
        last_swipe = Swipe.objects.filter(user=u).order_by("-datetime")[0]
        next_swipes = last_swipe.get_next_allowed_types()
    except IndexError:
        last_swipe = None
        next_swipes = ('IN', )  # empty database - first swipe is IN

    hours_total_last_month = Session.objects.get_hours_month(
        u.id,
        datetime.now().month - 1)
    hours_unassigned_last_month = Session.objects.get_unassigned_hours_month(
        u.id,
        datetime.now().month - 1)
    hours_total_this_month = Session.objects.get_hours_this_month(u.id)
    hours_unassigned_this_month = Session.objects.get_unassigned_hours_month(
        u.id,
        datetime.now().month)
    hours_not_work_this_month = Session.objects.get_not_work_hours_month(
        u.id,
        datetime.now().month)
    hours_not_work_last_month = Session.objects.get_not_work_hours_month(
        u.id,
        datetime.now().month - 1)
    hours_work_last_month = hours_total_last_month - hours_unassigned_last_month - hours_not_work_last_month
    hours_work_this_month = hours_total_this_month - hours_unassigned_this_month - hours_not_work_this_month

    num_of_workdays = get_number_of_work_days(date.today().year,
                                              date.today().month)
    unassigned_closed_session_hours = hours_unassigned_this_month - current_session_work_hours
    hours_quota = get_quota_work_hours(datetime.now().year,
                                       datetime.now().month, WORKHOURS_PER_DAY)
    num_of_elapsed_workdays = get_num_of_elapsed_workdays_in_month(
        date.today())
    current_quota = num_of_elapsed_workdays * WORKHOURS_PER_DAY
    quota_difference = hours_work_this_month + unassigned_closed_session_hours - current_quota
    quota_difference_abs = abs(quota_difference)
    avg_work_hours_fullfill_quota = (
        hours_quota - unassigned_closed_session_hours -
        hours_work_this_month) / (num_of_workdays - num_of_elapsed_workdays)

    context = {
        "user": u,
        "session_list": s,
        "hours_total_this_month": hours_total_this_month,
        "hours_unassigned_this_month": hours_unassigned_this_month,
        "last_swipe": last_swipe,
        "next_swipes": next_swipes,
        "at_work_users": at_work_users,
        "on_break_users": on_break_users,
        "on_trip_users": on_trip_users,
        "hours_total_last_month": hours_total_last_month,
        "hours_unassigned_last_month": hours_unassigned_last_month,
        "hours_not_work_this_month": hours_not_work_this_month,
        "hours_not_work_last_month": hours_not_work_last_month,
        "hours_work_last_month": hours_work_last_month,
        "hours_work_this_month": hours_work_this_month,
        "hours_quota": hours_quota,
        "current_session_work_hours": current_session_work_hours,
        "current_session": current_session,
        "unassigned_closed_session_hours": unassigned_closed_session_hours,
        "num_of_elapsed_workdays": num_of_elapsed_workdays,
        "num_of_workdays": num_of_workdays,
        "current_quota": current_quota,
        "quota_difference": quota_difference,
        "quota_difference_abs": quota_difference_abs,
        "avg_work_hours_fullfill_qoota": avg_work_hours_fullfill_quota,
        "workhours_per_day": WORKHOURS_PER_DAY
    }
    return render(request, "attendance/user_page.html", context)