from __future__ import absolute_import
from __future__ import print_function
from __future__ import division
from __future__ import unicode_literals

from django.http import HttpResponse, HttpResponseRedirect, JsonResponse, QueryDict #, HttpResponseForbidden, Http404, , JsonResponse
from django.shortcuts import get_object_or_404, render, redirect
from django.urls import reverse
from django.contrib.admin.views.decorators import staff_member_required, user_passes_test

from rules.contrib.views import permission_required, objectgetter

from isisdata.models import *
from isisdata.utils import strip_punctuation, normalize
from isisdata import operations
from isisdata.filters import *
from isisdata import tasks as data_tasks
from curation import p3_port_utils
from curation.forms import *

from curation.contrib.views import check_rules


@user_passes_test(lambda u: u.is_superuser or u.is_staff)
@check_rules('can_access_view_edit', fn=objectgetter(Authority, 'authority_id'))
def create_acrelation_for_authority(request, authority_id):
    authority = get_object_or_404(Authority, pk=authority_id)
    search_key = request.GET.get('search', request.POST.get('search'))
    current_index = request.GET.get('current', request.POST.get('current'))

    context = {
        'curation_section': 'datasets',
        'curation_subsection': 'authorities',
        'instance': authority,
        'search_key': search_key,
        'current_index': current_index

    }
    if request.method == 'GET':
        initial = {
            'authority': authority.id,
            'name_for_display_in_citation': authority.name
        }
        type_controlled = request.GET.get('type_controlled', None)
        if type_controlled:
            initial.update({'type_controlled': type_controlled.upper()})
        form = ACRelationForm(prefix='acrelation', initial=initial)

    elif request.method == 'POST':
        form = ACRelationForm(request.POST, prefix='acrelation')
        if form.is_valid():
            form.save()

            target = reverse('curation:curate_authority', args=(authority.id,)) + '?tab=acrelations'
            if search_key and current_index:
                target += '&search=%s&current=%s' % (search_key, current_index)
            return HttpResponseRedirect(target)

    context.update({
        'form': form,
    })
    template = 'curation/authority_acrelation_changeview.html'
    return render(request, template, context)

@user_passes_test(lambda u: u.is_superuser or u.is_staff)
@check_rules('can_access_view_edit', fn=objectgetter(Authority, 'authority_id'))
def create_aarelation_for_authority(request, authority_id):
    authority = get_object_or_404(Authority, pk=authority_id)
    search_key = request.GET.get('search', request.POST.get('search'))
    current_index = request.GET.get('current', request.POST.get('current'))

    context = {
        'curation_section': 'datasets',
        'curation_subsection': 'authorities',
        'instance': authority,
        'search_key': search_key,
        'current_index': current_index

    }
    if request.method == 'GET':
        initial = {
            'subject': authority.id
        }
        aarelation=AARelation()
        aarelation.subject = authority
        type_controlled = request.GET.get('type_controlled', None)
        if type_controlled:
            aarelation = dict(AARelation.TYPE_CHOICES)[type_controlled]
        form = AARelationForm(prefix='aarelation', instance=aarelation)
    elif request.method == 'POST':
        form = AARelationForm(request.POST, prefix='aarelation')
        if form.is_valid():
            form.save()

            target = reverse('curation:curate_authority', args=(authority.id,)) + '?tab=aarelations'
            if search_key and current_index:
                target += '&search=%s&current=%s' % (search_key, current_index)
            return HttpResponseRedirect(target)

    context.update({
        'form': form,
    })
    template = 'curation/authority_aarelation_changeview.html'
    return render(request, template, context)


@user_passes_test(lambda u: u.is_superuser or u.is_staff)
@check_rules('can_access_view_edit', fn=objectgetter(Authority, 'authority_id'))
def acrelation_for_authority(request, authority_id, acrelation_id):
    authority = get_object_or_404(Authority, pk=authority_id)
    acrelation = get_object_or_404(ACRelation, pk=acrelation_id)

    search_key = request.GET.get('search', request.POST.get('search'))
    current_index = request.GET.get('current', request.POST.get('current'))

    context = {
        'curation_section': 'datasets',
        'curation_subsection': 'authorities',
        'instance': authority,
        'acrelation': acrelation,
        'search_key': search_key,
        'current_index': current_index
    }
    if request.method == 'GET':
        form = ACRelationForm(instance=acrelation, prefix='acrelation')

    elif request.method == 'POST':
        form = ACRelationForm(request.POST, instance=acrelation, prefix='acrelation')
        if form.is_valid():
            form.save()
            target = reverse('curation:curate_authority', args=(authority.id,)) + '?tab=acrelations'
            if search_key and current_index:
                target += '&search=%s&current=%s' % (search_key, current_index)
            return HttpResponseRedirect(target)

    context.update({
        'form': form,
    })
    template = 'curation/authority_acrelation_changeview.html'
    return render(request, template, context)

@user_passes_test(lambda u: u.is_superuser or u.is_staff)
@check_rules('can_access_view_edit', fn=objectgetter(Authority, 'authority_id'))
def aarelation_for_authority(request, authority_id, aarelation_id):
    authority = get_object_or_404(Authority, pk=authority_id)
    aarelation = get_object_or_404(AARelation, pk=aarelation_id)

    search_key = request.GET.get('search', request.POST.get('search'))
    current_index = request.GET.get('current', request.POST.get('current'))

    context = {
        'curation_section': 'datasets',
        'curation_subsection': 'authorities',
        'instance': authority,
        'aarelation': aarelation,
        'search_key': search_key,
        'current_index': current_index
    }
    if request.method == 'GET':
        form = AARelationForm(instance=aarelation, prefix='aarelation')

    elif request.method == 'POST':
        form = AARelationForm(request.POST, instance=aarelation, prefix='aarelation')
        if form.is_valid():
            form.save()
            target = reverse('curation:curate_authority', args=(authority.id,)) + '?tab=aarelations'
            if search_key and current_index:
                target += '&search=%s&current=%s' % (search_key, current_index)
            return HttpResponseRedirect(target)

    context.update({
        'form': form,
    })
    template = 'curation/authority_aarelation_changeview.html'
    return render(request, template, context)

@user_passes_test(lambda u: u.is_superuser or u.is_staff)
@check_rules('can_access_view_edit', fn=objectgetter(Authority, 'authority_id'))
def delete_aarelation_for_authority(request, authority_id, aarelation_id, format=None):
    authority = get_object_or_404(Authority, pk=authority_id)
    aarelation = get_object_or_404(AARelation, pk=aarelation_id)
    search_key = request.GET.get('search', request.POST.get('search'))
    current_index = request.GET.get('current', request.POST.get('current'))

    context = {
        'curation_section': 'datasets',
        'curation_subsection': 'authorities',
        'instance': authority,
        'aarelation': aarelation,
        'search_key': search_key,
        'current_index': current_index
    }

    if request.POST.get('confirm', False) == 'true':
        if not aarelation.modified_on:
            aarelation.modified_on = datetime.datetime.now()
        aarelation.delete()
        if format == 'json':
            return JsonResponse({'result': True})

        target = reverse('curation:curate_authority', args=(authority.id,)) + '?tab=aarelations'
        if search_key and current_index:
            target += '&search=%s&current=%s' % (search_key, current_index)
        return HttpResponseRedirect(target)

    if format == 'json':
        return JsonResponse({'result': False})

    template = 'curation/authority_aarelation_delete.html'
    return render(request, template, context)

[((24, 1, 24, 57), 'django.contrib.admin.views.decorators.user_passes_test', 'user_passes_test', ({(24, 18, 24, 56): '(lambda u: u.is_superuser or u.is_staff)'}, {}), '(lambda u: u.is_superuser or u.is_staff)', False, 'from django.contrib.admin.views.decorators import staff_member_required, user_passes_test\n'), ((65, 1, 65, 57), 'django.contrib.admin.views.decorators.user_passes_test', 'user_passes_test', ({(65, 18, 65, 56): '(lambda u: u.is_superuser or u.is_staff)'}, {}), '(lambda u: u.is_superuser or u.is_staff)', False, 'from django.contrib.admin.views.decorators import staff_member_required, user_passes_test\n'), ((107, 1, 107, 57), 'django.contrib.admin.views.decorators.user_passes_test', 'user_passes_test', ({(107, 18, 107, 56): '(lambda u: u.is_superuser or u.is_staff)'}, {}), '(lambda u: u.is_superuser or u.is_staff)', False, 'from django.contrib.admin.views.decorators import staff_member_required, user_passes_test\n'), ((142, 1, 142, 57), 'django.contrib.admin.views.decorators.user_passes_test', 'user_passes_test', ({(142, 18, 142, 56): '(lambda u: u.is_superuser or u.is_staff)'}, {}), '(lambda u: u.is_superuser or u.is_staff)', False, 'from django.contrib.admin.views.decorators import staff_member_required, user_passes_test\n'), ((177, 1, 177, 57), 'django.contrib.admin.views.decorators.user_passes_test', 'user_passes_test', ({(177, 18, 177, 56): '(lambda u: u.is_superuser or u.is_staff)'}, {}), '(lambda u: u.is_superuser or u.is_staff)', False, 'from django.contrib.admin.views.decorators import staff_member_required, user_passes_test\n'), ((27, 16, 27, 61), 'django.shortcuts.get_object_or_404', 'get_object_or_404', (), '', False, 'from django.shortcuts import get_object_or_404, render, redirect\n'), ((63, 11, 63, 45), 'django.shortcuts.render', 'render', ({(63, 18, 63, 25): 'request', (63, 27, 63, 35): 'template', (63, 37, 63, 44): 'context'}, {}), '(request, template, context)', False, 'from django.shortcuts import get_object_or_404, render, redirect\n'), ((68, 16, 68, 61), 'django.shortcuts.get_object_or_404', 'get_object_or_404', (), '', False, 'from django.shortcuts import get_object_or_404, render, redirect\n'), ((104, 11, 104, 45), 'django.shortcuts.render', 'render', ({(104, 18, 104, 25): 'request', (104, 27, 104, 35): 'template', (104, 37, 104, 44): 'context'}, {}), '(request, template, context)', False, 'from django.shortcuts import get_object_or_404, render, redirect\n'), ((110, 16, 110, 61), 'django.shortcuts.get_object_or_404', 'get_object_or_404', (), '', False, 'from django.shortcuts import get_object_or_404, render, redirect\n'), ((111, 17, 111, 64), 'django.shortcuts.get_object_or_404', 'get_object_or_404', (), '', False, 'from django.shortcuts import get_object_or_404, render, redirect\n'), ((140, 11, 140, 45), 'django.shortcuts.render', 'render', ({(140, 18, 140, 25): 'request', (140, 27, 140, 35): 'template', (140, 37, 140, 44): 'context'}, {}), '(request, template, context)', False, 'from django.shortcuts import get_object_or_404, render, redirect\n'), ((145, 16, 145, 61), 'django.shortcuts.get_object_or_404', 'get_object_or_404', (), '', False, 'from django.shortcuts import get_object_or_404, render, redirect\n'), ((146, 17, 146, 64), 'django.shortcuts.get_object_or_404', 'get_object_or_404', (), '', False, 'from django.shortcuts import get_object_or_404, render, redirect\n'), ((175, 11, 175, 45), 'django.shortcuts.render', 'render', ({(175, 18, 175, 25): 'request', (175, 27, 175, 35): 'template', (175, 37, 175, 44): 'context'}, {}), '(request, template, context)', False, 'from django.shortcuts import get_object_or_404, render, redirect\n'), ((180, 16, 180, 61), 'django.shortcuts.get_object_or_404', 'get_object_or_404', (), '', False, 'from django.shortcuts import get_object_or_404, render, redirect\n'), ((181, 17, 181, 64), 'django.shortcuts.get_object_or_404', 'get_object_or_404', (), '', False, 'from django.shortcuts import get_object_or_404, render, redirect\n'), ((210, 11, 210, 45), 'django.shortcuts.render', 'render', ({(210, 18, 210, 25): 'request', (210, 27, 210, 35): 'template', (210, 37, 210, 44): 'context'}, {}), '(request, template, context)', False, 'from django.shortcuts import get_object_or_404, render, redirect\n'), ((25, 40, 25, 79), 'rules.contrib.views.objectgetter', 'objectgetter', ({(25, 53, 25, 62): 'Authority', (25, 64, 25, 78): '"""authority_id"""'}, {}), "(Authority, 'authority_id')", False, 'from rules.contrib.views import permission_required, objectgetter\n'), ((66, 40, 66, 79), 'rules.contrib.views.objectgetter', 'objectgetter', ({(66, 53, 66, 62): 'Authority', (66, 64, 66, 78): '"""authority_id"""'}, {}), "(Authority, 'authority_id')", False, 'from rules.contrib.views import permission_required, objectgetter\n'), ((108, 40, 108, 79), 'rules.contrib.views.objectgetter', 'objectgetter', ({(108, 53, 108, 62): 'Authority', (108, 64, 108, 78): '"""authority_id"""'}, {}), "(Authority, 'authority_id')", False, 'from rules.contrib.views import permission_required, objectgetter\n'), ((143, 40, 143, 79), 'rules.contrib.views.objectgetter', 'objectgetter', ({(143, 53, 143, 62): 'Authority', (143, 64, 143, 78): '"""authority_id"""'}, {}), "(Authority, 'authority_id')", False, 'from rules.contrib.views import permission_required, objectgetter\n'), ((204, 15, 204, 43), 'django.http.HttpResponseRedirect', 'HttpResponseRedirect', ({(204, 36, 204, 42): 'target'}, {}), '(target)', False, 'from django.http import HttpResponse, HttpResponseRedirect, JsonResponse, QueryDict\n'), ((207, 15, 207, 46), 'django.http.JsonResponse', 'JsonResponse', ({(207, 28, 207, 45): "{'result': False}"}, {}), "({'result': False})", False, 'from django.http import HttpResponse, HttpResponseRedirect, JsonResponse, QueryDict\n'), ((178, 40, 178, 79), 'rules.contrib.views.objectgetter', 'objectgetter', ({(178, 53, 178, 62): 'Authority', (178, 64, 178, 78): '"""authority_id"""'}, {}), "(Authority, 'authority_id')", False, 'from rules.contrib.views import permission_required, objectgetter\n'), ((199, 19, 199, 49), 'django.http.JsonResponse', 'JsonResponse', ({(199, 32, 199, 48): "{'result': True}"}, {}), "({'result': True})", False, 'from django.http import HttpResponse, HttpResponseRedirect, JsonResponse, QueryDict\n'), ((201, 17, 201, 75), 'django.urls.reverse', 'reverse', (), '', False, 'from django.urls import reverse\n'), ((57, 19, 57, 47), 'django.http.HttpResponseRedirect', 'HttpResponseRedirect', ({(57, 40, 57, 46): 'target'}, {}), '(target)', False, 'from django.http import HttpResponse, HttpResponseRedirect, JsonResponse, QueryDict\n'), ((98, 19, 98, 47), 'django.http.HttpResponseRedirect', 'HttpResponseRedirect', ({(98, 40, 98, 46): 'target'}, {}), '(target)', False, 'from django.http import HttpResponse, HttpResponseRedirect, JsonResponse, QueryDict\n'), ((134, 19, 134, 47), 'django.http.HttpResponseRedirect', 'HttpResponseRedirect', ({(134, 40, 134, 46): 'target'}, {}), '(target)', False, 'from django.http import HttpResponse, HttpResponseRedirect, JsonResponse, QueryDict\n'), ((169, 19, 169, 47), 'django.http.HttpResponseRedirect', 'HttpResponseRedirect', ({(169, 40, 169, 46): 'target'}, {}), '(target)', False, 'from django.http import HttpResponse, HttpResponseRedirect, JsonResponse, QueryDict\n'), ((54, 21, 54, 79), 'django.urls.reverse', 'reverse', (), '', False, 'from django.urls import reverse\n'), ((95, 21, 95, 79), 'django.urls.reverse', 'reverse', (), '', False, 'from django.urls import reverse\n'), ((131, 21, 131, 79), 'django.urls.reverse', 'reverse', (), '', False, 'from django.urls import reverse\n'), ((166, 21, 166, 79), 'django.urls.reverse', 'reverse', (), '', False, 'from django.urls import reverse\n')]