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

# Dissemin: open access policy enforcement tool
# Copyright (C) 2014 <NAME>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#

from __future__ import unicode_literals

from django.conf.urls import patterns, include, url
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, HttpResponseNotFound, HttpResponseForbidden
from django.contrib.auth.decorators import user_passes_test
from django.core.validators import validate_email
from django.forms import ValidationError
from django.db import IntegrityError
from django.utils.translation import ugettext as __
import json, requests

from django.views.decorators.csrf import csrf_exempt
from celery.execute import send_task

from dissemin.settings import URL_DEPOSIT_DOWNLOAD_TIMEOUT, DEPOSIT_MAX_FILE_SIZE, DEPOSIT_CONTENT_TYPES, MEDIA_ROOT

from papers.models import *
from papers.user import *
from papers.forms import AddResearcherForm
from papers.utils import iunaccent, sanitize_html

from time import sleep # TODO delete me
import os.path

# General function used to change a CharField in a model with ajax
def process_ajax_change(request, model, allowedFields):
    response = dict()
    try:
        instance = model.objects.get(pk=request.POST.get('pk'))
        field = request.POST.get('name')
        if field in allowedFields:
            val = request.POST.get('value')
            val = sanitize_html(val)
            setattr(instance, field, val)
            instance.save(update_fields=[field])
            if hasattr(instance, "invalidate_cache"):
                instance.invalidate_cache()
            if model == Paper:
                merged = instance.recompute_fingerprint_and_merge_if_needed()
                response['merged'] = ''
                if merged:
                    response['merged'] = merged.pk
                    response['merged_title'] = merged.title
            response['status'] = 'OK'
            response['value'] = val
            return HttpResponse(json.dumps(response), content_type='text/plain')
        else:
            raise ObjectDoesNotExist
    except ObjectDoesNotExist:
        return HttpResponseNotFound(json.dumps(response), content_type='text/plain')

# Researcher management
@user_passes_test(is_admin)
def deleteResearcher(request, pk):
    researcher = get_object_or_404(Researcher, pk=pk)
    researcher.delete()
    return HttpResponse('OK', content_type='text/plain')

@user_passes_test(is_admin)
def addResearcher(request):
    form = AddResearcherForm(request.POST)
    if form.is_valid():
        try:
            dept = form.cleaned_data['department']
            email = form.cleaned_data['email']
            first = form.cleaned_data['first']
            last = form.cleaned_data['last']
            role = form.cleaned_data['role']
            homepage = form.cleaned_data['homepage']

            try:
                researcher = Researcher.create_from_scratch(first, last, dept, email, role, homepage)
            except ValueError:
                return HttpResponseForbidden('Researcher already present', content_type='text/plain')

            return HttpResponse(json.dumps({
                'id':researcher.id,
                'name':first+' '+last}), content_type='application/javascript')
        except IntegrityError as e:
            print "integrity error"
            print e
            return HttpResponseForbidden('Invalid input, something went wrong', content_type='text/plain')
    else:
        print json.dumps(form.errors)
        return HttpResponseForbidden(json.dumps(form.errors), content_type='application/javascript')

# Paper management
@user_passes_test(is_authenticated)
def annotatePaper(request, pk, status):
    paper = get_object_or_404(Paper, pk=pk)
    try:
        status = int(status)
        if not status in range(len(VISIBILITY_CHOICES)):
            raise ValueError
    except ValueError:
        return HttpResponseForbidden('Invalid visibility status', content_type='text/plain')

    visibility = VISIBILITY_CHOICES[status][0]
    Annotation.create(paper, visibility, request.user)
    return HttpResponse('OK', content_type='text/plain')

@user_passes_test(is_authenticated)
def changePaper(request):
    allowedFields = ['title']
    return process_ajax_change(request, Paper, allowedFields)

# Department management
@user_passes_test(is_authenticated)
def changeDepartment(request):
    allowedFields = ['name']
    return process_ajax_change(request, Department, allowedFields)

# Researcher management
@user_passes_test(is_authenticated)
def changeResearcher(request):
    allowedFields = ['role']
    return process_ajax_change(request, Researcher, allowedFields)

@user_passes_test(is_authenticated)
def harvestingStatus(request, pk): 
    researcher = get_object_or_404(Researcher, pk=pk)
    resp = {}
    if researcher.current_task:
        resp['status'] = researcher.current_task
        resp['display'] = researcher.get_current_task_display()
    else:
        resp = None
    return HttpResponse(json.dumps(resp), content_type='text/json')

# Author management
@user_passes_test(is_authenticated)
def changeAuthor(request):
    response = dict()
    try:
        author = Author.objects.get(pk=request.POST.get('pk'))
        first = request.POST.get('value[first]')
        if first:
            first = sanitize_html(first)<commit_msg>Fix harvesting status update: the ajax method should not require authentication<commit_after># -*- encoding: utf-8 -*-

# Dissemin: open access policy enforcement tool
# Copyright (C) 2014 <NAME>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#

from __future__ import unicode_literals

from django.conf.urls import patterns, include, url
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, HttpResponseNotFound, HttpResponseForbidden
from django.contrib.auth.decorators import user_passes_test
from django.core.validators import validate_email
from django.forms import ValidationError
from django.db import IntegrityError
from django.utils.translation import ugettext as __
import json, requests

from django.views.decorators.csrf import csrf_exempt
from celery.execute import send_task

from dissemin.settings import URL_DEPOSIT_DOWNLOAD_TIMEOUT, DEPOSIT_MAX_FILE_SIZE, DEPOSIT_CONTENT_TYPES, MEDIA_ROOT

from papers.models import *
from papers.user import *
from papers.forms import AddResearcherForm
from papers.utils import iunaccent, sanitize_html

from time import sleep # TODO delete me
import os.path

# General function used to change a CharField in a model with ajax
def process_ajax_change(request, model, allowedFields):
    response = dict()
    try:
        instance = model.objects.get(pk=request.POST.get('pk'))
        field = request.POST.get('name')
        if field in allowedFields:
            val = request.POST.get('value')
            val = sanitize_html(val)
            setattr(instance, field, val)
            instance.save(update_fields=[field])
            if hasattr(instance, "invalidate_cache"):
                instance.invalidate_cache()
            if model == Paper:
                merged = instance.recompute_fingerprint_and_merge_if_needed()
                response['merged'] = ''
                if merged:
                    response['merged'] = merged.pk
                    response['merged_title'] = merged.title
            response['status'] = 'OK'
            response['value'] = val
            return HttpResponse(json.dumps(response), content_type='text/plain')
        else:
            raise ObjectDoesNotExist
    except ObjectDoesNotExist:
        return HttpResponseNotFound(json.dumps(response), content_type='text/plain')

# Researcher management
@user_passes_test(is_admin)
def deleteResearcher(request, pk):
    researcher = get_object_or_404(Researcher, pk=pk)
    researcher.delete()
    return HttpResponse('OK', content_type='text/plain')

@user_passes_test(is_admin)
def addResearcher(request):
    form = AddResearcherForm(request.POST)
    if form.is_valid():
        try:
            dept = form.cleaned_data['department']
            email = form.cleaned_data['email']
            first = form.cleaned_data['first']
            last = form.cleaned_data['last']
            role = form.cleaned_data['role']
            homepage = form.cleaned_data['homepage']

            try:
                researcher = Researcher.create_from_scratch(first, last, dept, email, role, homepage)
            except ValueError:
                return HttpResponseForbidden('Researcher already present', content_type='text/plain')

            return HttpResponse(json.dumps({
                'id':researcher.id,
                'name':first+' '+last}), content_type='application/javascript')
        except IntegrityError as e:
            print "integrity error"
            print e
            return HttpResponseForbidden('Invalid input, something went wrong', content_type='text/plain')
    else:
        print json.dumps(form.errors)
        return HttpResponseForbidden(json.dumps(form.errors), content_type='application/javascript')

# Paper management
@user_passes_test(is_authenticated)
def annotatePaper(request, pk, status):
    paper = get_object_or_404(Paper, pk=pk)
    try:
        status = int(status)
        if not status in range(len(VISIBILITY_CHOICES)):
            raise ValueError
    except ValueError:
        return HttpResponseForbidden('Invalid visibility status', content_type='text/plain')

    visibility = VISIBILITY_CHOICES[status][0]
    Annotation.create(paper, visibility, request.user)
    return HttpResponse('OK', content_type='text/plain')

@user_passes_test(is_authenticated)
def changePaper(request):
    allowedFields = ['title']
    return process_ajax_change(request, Paper, allowedFields)

# Department management
@user_passes_test(is_authenticated)
def changeDepartment(request):
    allowedFields = ['name']
    return process_ajax_change(request, Department, allowedFields)

# Researcher management
@user_passes_test(is_authenticated)
def changeResearcher(request):
    allowedFields = ['role']
    return process_ajax_change(request, Researcher, allowedFields)

def harvestingStatus(request, pk): 
    researcher = get_object_or_404(Researcher, pk=pk)
    resp = {}
    if researcher.current_task:
        resp['status'] = researcher.current_task
        resp['display'] = researcher.get_current_task_display()
    else:
        resp = None
    return HttpResponse(json.dumps(resp), content_type='text/json')

# Author management
@user_passes_test(is_authenticated)
def changeAuthor(request):
    response = dict()
    try:
        author = Author.objects.get(pk=request.POST.get('pk'))
        first = request.POST.get('value[first]')
        if first:
            first = sanitize_html(first)
{'fraction_non_alphanumeric': 0.08823047962023245, 'fraction_numerical': 0.004910787362907186, 'mean_word_length': 3.518860946745562, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6667750', 'n_tokens_mistral': 3455, 'n_tokens_neox': 3354, 'n_words': 922}