# -*- coding: utf-8 -*-
from __future__ import unicode_literals, print_function

from django.contrib.auth import get_user_model
from django.core import signing
from django.http import Http404
from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.views import generic
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_exempt
from django_auth_lti.decorators import lti_role_required
from django.views.decorators.clickjacking import xframe_options_exempt

import logs.ops
from action.models import Action
from dataops import pandas_db, ops
from action import settings
from ontask.permissions import UserIsInstructor


class HomePage(generic.TemplateView):
    template_name = "home.html"


class AboutPage(generic.TemplateView):
    template_name = "about.html"


class ToBeDone(UserIsInstructor, generic.TemplateView):
    template_name = "base.html"


@login_required
def entry(request):
    return redirect('workflow:index')


@csrf_exempt
@xframe_options_exempt
@lti_role_required(['Instructor', 'Student'])
def lti_entry(request):
    return redirect('workflow:index')


# No permissions in this URL as it is supposed to be wide open to track email
#  reads.
def trck(request):
    """
    Receive a request with a token from email read tracking
    :param request: Request object
    :return: Reflects in the DB the reception and (optionally) in the data 
    table of the workflow
    """
    if request.method != 'GET':
        raise Http404

    # Detected attempt to track event
    track_id = request.GET.get('v', None)
    if not track_id:
        raise Http404

    # If the track_id is not correctly signed, out.
    try:
        track_id = signing.loads(track_id)
    except signing.BadSignature:
        raise Http404

    # The request is legit and the value has been verified. Track_id has now
    # the dictionary with the information included in the tracking

    # Get the objects related to the ping
    try:
        user = get_user_model().objects.get(email=track_id['sender'])
        action = Action.objects.get(pk=track_id['action'])
    except Exception:
        raise Http404

    # If the track comes with column_dst, the event needs to be reflected
    # back in the data frame
    column_dst = track_id.get('column_dst', '')

    if column_dst:
        # Load the dataframe
        data_frame = pandas_db.load_from_db(action.workflow.id)

        # Extract the relevant fields from the track_id
        column_to = track_id['column_to']
        msg_to = track_id['to']
        track_col_name = track_id['column_dst']
        # New val in DF: df.loc[df['a']==1,'b'] = VAL
        data_frame.loc[data_frame[column_to] == msg_to, track_col_name] += 1

        # Save DF
        ops.store_dataframe_in_db(data_frame, action.workflow.id)

    # Record the event
    logs.ops.put(
        user,
        'action_email_read',
        action.workflow,
        {'to': track_id['to'],  # The destination of the email
         'email_column': track_id['column_to'],  # The column used to get the
         'column_dst': column_dst
         }
    )

    return HttpResponse(settings.PIXEL, content_type='image/png')


def ontask_handler400(request):
    return render(request, '400.html', {})


def ontask_handler403(request):
    return render(request, '403.html', {})


def ontask_handler404(request):
    return render(request, '404.html', {})


def ontask_handler500(request):
    return render(request, '500.html', {})

# -*- coding: utf-8 -*-
from __future__ import unicode_literals, print_function

from django.contrib.auth import get_user_model
from django.core import signing
from django.http import Http404
from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.views import generic
from django.contrib.auth.decorators import login_required
from django.views.decorators.csrf import csrf_exempt
from django_auth_lti.decorators import lti_role_required
from django.views.decorators.clickjacking import xframe_options_exempt

import logs.ops
from action.models import Action
from dataops import pandas_db, ops
from action import settings
from ontask.permissions import UserIsInstructor


class HomePage(generic.TemplateView):
    template_name = "home.html"


class AboutPage(generic.TemplateView):
    template_name = "about.html"


class ToBeDone(UserIsInstructor, generic.TemplateView):
    template_name = "base.html"


@login_required
def entry(request):
    return redirect('workflow:index')


@csrf_exempt
@xframe_options_exempt
@lti_role_required(['Instructor', 'Student'])
def lti_entry(request):
    return redirect('workflow:index')


# No permissions in this URL as it is supposed to be wide open to track email
#  reads.
def trck(request):
    """
    Receive a request with a token from email read tracking
    :param request: Request object
    :return: Reflects in the DB the reception and (optionally) in the data 
    table of the workflow
    """
    if request.method != 'GET':
        raise Http404

    # Detected attempt to track event
    track_id = request.GET.get('v', None)
    if not track_id:
        raise Http404

    # If the track_id is not correctly signed, out.
    try:
        track_id = signing.loads(track_id)
    except signing.BadSignature:
        raise Http404

    # The request is legit and the value has been verified. Track_id has now
    # the dictionary with the information included in the tracking

    # Get the objects related to the ping
    try:
        user = get_user_model().objects.get(email=track_id['sender'])
        action = Action.objects.get(pk=track_id['action'])
    except Exception:
        raise Http404

    # If the track comes with column_dst, the event needs to be reflected
    # back in the data frame
    column_dst = track_id.get('column_dst', '')

    if column_dst:
        # Load the dataframe
        data_frame = pandas_db.load_from_db(action.workflow.id)

        # Extract the relevant fields from the track_id
        column_to = track_id['column_to']
        msg_to = track_id['to']
        track_col_name = track_id['column_dst']
        # New val in DF: df.loc[df['a']==1,'b'] = VAL
        data_frame.loc[data_frame[column_to] == msg_to, track_col_name] += 1

        # Save DF
        ops.store_dataframe_in_db(data_frame, action.workflow.id)

    # Record the event
    logs.ops.put(
        user,
        'action_email_read',
        action.workflow,
        {'to': track_id['to'],  # The destination of the email
         'email_column': track_id['column_to'],  # The column used to get the
         'column_dst': column_dst
         }
    )

    return HttpResponse(settings.PIXEL, content_type='image/png')


def ontask_handler400(request):
    return render(request, '400.html', {})


def ontask_handler403(request):
    return render(request, '403.html', {})


def ontask_handler404(request):
    return render(request, '404.html', {})


def ontask_handler500(request):
    return render(request, '500.html', {})

# -*- coding: utf-8 -*- # No permissions in this URL as it is supposed to be wide open to track email #  reads. Receive a request with a token from email read tracking     :param request: Request object     :return: Reflects in the DB the reception and (optionally) in the data      table of the workflow # Detected attempt to track event # If the track_id is not correctly signed, out. # The request is legit and the value has been verified. Track_id has now # the dictionary with the information included in the tracking # Get the objects related to the ping # If the track comes with column_dst, the event needs to be reflected # back in the data frame # Load the dataframe # Extract the relevant fields from the track_id # New val in DF: df.loc[df['a']==1,'b'] = VAL # Save DF # Record the event # The destination of the email # The column used to get the