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)

[((844, 900), 'django.contrib.admin.views.decorators.user_passes_test', 'user_passes_test', (['(lambda u: u.is_superuser or u.is_staff)'], {}), '(lambda u: u.is_superuser or u.is_staff)\n', (860, 900), False, 'from django.contrib.admin.views.decorators import staff_member_required, user_passes_test\n'), ((2451, 2507), 'django.contrib.admin.views.decorators.user_passes_test', 'user_passes_test', (['(lambda u: u.is_superuser or u.is_staff)'], {}), '(lambda u: u.is_superuser or u.is_staff)\n', (2467, 2507), False, 'from django.contrib.admin.views.decorators import staff_member_required, user_passes_test\n'), ((4070, 4126), 'django.contrib.admin.views.decorators.user_passes_test', 'user_passes_test', (['(lambda u: u.is_superuser or u.is_staff)'], {}), '(lambda u: u.is_superuser or u.is_staff)\n', (4086, 4126), False, 'from django.contrib.admin.views.decorators import staff_member_required, user_passes_test\n'), ((5512, 5568), 'django.contrib.admin.views.decorators.user_passes_test', 'user_passes_test', (['(lambda u: u.is_superuser or u.is_staff)'], {}), '(lambda u: u.is_superuser or u.is_staff)\n', (5528, 5568), False, 'from django.contrib.admin.views.decorators import staff_member_required, user_passes_test\n'), ((6954, 7010), 'django.contrib.admin.views.decorators.user_passes_test', 'user_passes_test', (['(lambda u: u.is_superuser or u.is_staff)'], {}), '(lambda u: u.is_superuser or u.is_staff)\n', (6970, 7010), False, 'from django.contrib.admin.views.decorators import staff_member_required, user_passes_test\n'), ((1058, 1103), 'django.shortcuts.get_object_or_404', 'get_object_or_404', (['Authority'], {'pk': 'authority_id'}), '(Authority, pk=authority_id)\n', (1075, 1103), False, 'from django.shortcuts import get_object_or_404, render, redirect\n'), ((2414, 2448), 'django.shortcuts.render', 'render', (['request', 'template', 'context'], {}), '(request, template, context)\n', (2420, 2448), False, 'from django.shortcuts import get_object_or_404, render, redirect\n'), ((2665, 2710), 'django.shortcuts.get_object_or_404', 'get_object_or_404', (['Authority'], {'pk': 'authority_id'}), '(Authority, pk=authority_id)\n', (2682, 2710), False, 'from django.shortcuts import get_object_or_404, render, redirect\n'), ((4032, 4066), 'django.shortcuts.render', 'render', (['request', 'template', 'context'], {}), '(request, template, context)\n', (4038, 4066), False, 'from django.shortcuts import get_object_or_404, render, redirect\n'), ((4292, 4337), 'django.shortcuts.get_object_or_404', 'get_object_or_404', (['Authority'], {'pk': 'authority_id'}), '(Authority, pk=authority_id)\n', (4309, 4337), False, 'from django.shortcuts import get_object_or_404, render, redirect\n'), ((4355, 4402), 'django.shortcuts.get_object_or_404', 'get_object_or_404', (['ACRelation'], {'pk': 'acrelation_id'}), '(ACRelation, pk=acrelation_id)\n', (4372, 4402), False, 'from django.shortcuts import get_object_or_404, render, redirect\n'), ((5475, 5509), 'django.shortcuts.render', 'render', (['request', 'template', 'context'], {}), '(request, template, context)\n', (5481, 5509), False, 'from django.shortcuts import get_object_or_404, render, redirect\n'), ((5734, 5779), 'django.shortcuts.get_object_or_404', 'get_object_or_404', (['Authority'], {'pk': 'authority_id'}), '(Authority, pk=authority_id)\n', (5751, 5779), False, 'from django.shortcuts import get_object_or_404, render, redirect\n'), ((5797, 5844), 'django.shortcuts.get_object_or_404', 'get_object_or_404', (['AARelation'], {'pk': 'aarelation_id'}), '(AARelation, pk=aarelation_id)\n', (5814, 5844), False, 'from django.shortcuts import get_object_or_404, render, redirect\n'), ((6917, 6951), 'django.shortcuts.render', 'render', (['request', 'template', 'context'], {}), '(request, template, context)\n', (6923, 6951), False, 'from django.shortcuts import get_object_or_404, render, redirect\n'), ((7196, 7241), 'django.shortcuts.get_object_or_404', 'get_object_or_404', (['Authority'], {'pk': 'authority_id'}), '(Authority, pk=authority_id)\n', (7213, 7241), False, 'from django.shortcuts import get_object_or_404, render, redirect\n'), ((7259, 7306), 'django.shortcuts.get_object_or_404', 'get_object_or_404', (['AARelation'], {'pk': 'aarelation_id'}), '(AARelation, pk=aarelation_id)\n', (7276, 7306), False, 'from django.shortcuts import get_object_or_404, render, redirect\n'), ((8365, 8399), 'django.shortcuts.render', 'render', (['request', 'template', 'context'], {}), '(request, template, context)\n', (8371, 8399), False, 'from django.shortcuts import get_object_or_404, render, redirect\n'), ((941, 980), 'rules.contrib.views.objectgetter', 'objectgetter', (['Authority', '"""authority_id"""'], {}), "(Authority, 'authority_id')\n", (953, 980), False, 'from rules.contrib.views import permission_required, objectgetter\n'), ((2548, 2587), 'rules.contrib.views.objectgetter', 'objectgetter', (['Authority', '"""authority_id"""'], {}), "(Authority, 'authority_id')\n", (2560, 2587), False, 'from rules.contrib.views import permission_required, objectgetter\n'), ((4167, 4206), 'rules.contrib.views.objectgetter', 'objectgetter', (['Authority', '"""authority_id"""'], {}), "(Authority, 'authority_id')\n", (4179, 4206), False, 'from rules.contrib.views import permission_required, objectgetter\n'), ((5609, 5648), 'rules.contrib.views.objectgetter', 'objectgetter', (['Authority', '"""authority_id"""'], {}), "(Authority, 'authority_id')\n", (5621, 5648), False, 'from rules.contrib.views import permission_required, objectgetter\n'), ((8192, 8220), 'django.http.HttpResponseRedirect', 'HttpResponseRedirect', (['target'], {}), '(target)\n', (8212, 8220), False, 'from django.http import HttpResponse, HttpResponseRedirect, JsonResponse, QueryDict\n'), ((8262, 8293), 'django.http.JsonResponse', 'JsonResponse', (["{'result': False}"], {}), "({'result': False})\n", (8274, 8293), False, 'from django.http import HttpResponse, HttpResponseRedirect, JsonResponse, QueryDict\n'), ((7051, 7090), 'rules.contrib.views.objectgetter', 'objectgetter', (['Authority', '"""authority_id"""'], {}), "(Authority, 'authority_id')\n", (7063, 7090), False, 'from rules.contrib.views import permission_required, objectgetter\n'), ((7931, 7961), 'django.http.JsonResponse', 'JsonResponse', (["{'result': True}"], {}), "({'result': True})\n", (7943, 7961), False, 'from django.http import HttpResponse, HttpResponseRedirect, JsonResponse, QueryDict\n'), ((7980, 8038), 'django.urls.reverse', 'reverse', (['"""curation:curate_authority"""'], {'args': '(authority.id,)'}), "('curation:curate_authority', args=(authority.id,))\n", (7987, 8038), False, 'from django.urls import reverse\n'), ((2260, 2288), 'django.http.HttpResponseRedirect', 'HttpResponseRedirect', (['target'], {}), '(target)\n', (2280, 2288), False, 'from django.http import HttpResponse, HttpResponseRedirect, JsonResponse, QueryDict\n'), ((3878, 3906), 'django.http.HttpResponseRedirect', 'HttpResponseRedirect', (['target'], {}), '(target)\n', (3898, 3906), False, 'from django.http import HttpResponse, HttpResponseRedirect, JsonResponse, QueryDict\n'), ((5321, 5349), 'django.http.HttpResponseRedirect', 'HttpResponseRedirect', (['target'], {}), '(target)\n', (5341, 5349), False, 'from django.http import HttpResponse, HttpResponseRedirect, JsonResponse, QueryDict\n'), ((6763, 6791), 'django.http.HttpResponseRedirect', 'HttpResponseRedirect', (['target'], {}), '(target)\n', (6783, 6791), False, 'from django.http import HttpResponse, HttpResponseRedirect, JsonResponse, QueryDict\n'), ((2036, 2094), 'django.urls.reverse', 'reverse', (['"""curation:curate_authority"""'], {'args': '(authority.id,)'}), "('curation:curate_authority', args=(authority.id,))\n", (2043, 2094), False, 'from django.urls import reverse\n'), ((3654, 3712), 'django.urls.reverse', 'reverse', (['"""curation:curate_authority"""'], {'args': '(authority.id,)'}), "('curation:curate_authority', args=(authority.id,))\n", (3661, 3712), False, 'from django.urls import reverse\n'), ((5097, 5155), 'django.urls.reverse', 'reverse', (['"""curation:curate_authority"""'], {'args': '(authority.id,)'}), "('curation:curate_authority', args=(authority.id,))\n", (5104, 5155), False, 'from django.urls import reverse\n'), ((6539, 6597), 'django.urls.reverse', 'reverse', (['"""curation:curate_authority"""'], {'args': '(authority.id,)'}), "('curation:curate_authority', args=(authority.id,))\n", (6546, 6597), False, 'from django.urls import reverse\n')]