<commit_before>import json
from django.conf import settings
from django.contrib.auth.models import User
from django.core.exceptions import PermissionDenied
from django.http import HttpResponse
from django.views.generic import View
from django.shortcuts import get_object_or_404

from authmethods import auth_login, METHODS, auth_register, auth_validate, auth_census
from utils import genhmac, paginate
from utils import check_authmethod, check_pipeline, check_metadata
from .decorators import login_required
from .models import AuthEvent, ACL, CreditsAction
from .models import User, UserData
from django.db.models import Q


def permission_required(user, object_type, permission, object_id=None):
    if object_id and user.userdata.has_perms(object_type, permission, None):
        return
    if not user.userdata.has_perms(object_type, permission, object_id):
        raise PermissionDenied('Permission required: ' + permission)


class Test(View):
    ''' Test view that returns the response data '''

    def get(self, request):
        req = request.GET
        data = {'status': 'ok', 'method': 'GET'}
        data['get'] = req
        jsondata = json.dumps(data)
        return HttpResponse(jsondata, content_type='application/json')

    def post(self, request):
        req = json.loads(request.body.decode('utf-8'))
        data = {'status': 'ok', 'method': 'POST'}
        data['post'] = req
        jsondata = json.dumps(data)
        return HttpResponse(jsondata, content_type='application/json')
test = Test.as_view()


class Census(View):
    ''' Add census in the auth-event '''

    def post(self, request, pk):
        e = get_object_or_404(AuthEvent, pk=pk)
        data = auth_census(e, request)
        status = 200 if data['status'] == 'ok' else 400
        jsondata = json.dumps(data)
        return HttpResponse(jsondata, status=status, content_type='application/json')
census = login_required(Census.as_view())


class Login(View):
    ''' Login into the authapi '''

    def post(self, request, pk):
        req = json.loads(request.body.decode('utf-8'))
        if int(pk) == 0:
            e = 0
        else:
            e = get_object_or_404(AuthEvent, pk=pk)
        data = auth_login(e, req)
        status = 200 if data['status'] == 'ok' else 400
        jsondata = json.dumps(data)
        return HttpResponse(jsondata, status=status, content_type='application/json')
login = Login.as_view()


class Register(View):
    ''' Register into the authapi '''

    def post(self, request, pk):
        e = get_object_or_404(AuthEvent, pk=pk)
        #if (e.census == 'close'):
        #    permission_required(request.user, 'UserEvent', 'edit', pk)
        data = auth_register(e, request)
        status = 200 if data['status'] == 'ok' else 400
        jsondata = json.dumps(data)
        return HttpResponse(jsondata, status=status, content_type='application/json')
register = Register.as_view()


class Validate(View):
    ''' Validate into the authapi '''

    def post(self, request, pk):
        e = get_object_or_404(AuthEvent, pk=pk)
        data = auth_validate(e, request)
        status = 200 if data['status'] == 'ok' else 400
        jsondata = json.dumps(data)
        return HttpResponse(jsondata, status=status, content_type='application/json')
validate = Validate.as_view()


class AuthEventStatus(View):
    ''' Change the status of auth-event '''

    def get(self, request, pk, status):
        permission_required(request.user, 'AuthEvent', 'edit', pk)
        e = get_object_or_404(AuthEvent, pk=pk)
        if e.status != status:
            e.status = status
            e.save()
            st = 200
        else:
            st = 400
        jsondata = json.dumps({})
        return HttpResponse(jsondata, status=st, content_type='application/json')
ae_status = login_required(AuthEventStatus.as_view())


class GetPerms(View):
    ''' Returns the permission token if the user has this perm '''

    def post(self, request):
        data = {'status': 'ok'}
        req = json.loads(request.body.decode('utf-8'))

        if 'permission' not in req or 'object_type' not in req:
            jsondata = json.dumps(data)
            return HttpResponse(jsondata, status=400, content_type='application/json')

        object_type = req['object_type']
        perm = req['permission']
        obj_id = req.get('object_id', None)

        if not request.user.userdata.has_perms(object_type, perm, obj_id):
            jsondata = json.dumps(data)
            return HttpResponse(jsondata, status=400, content_type='application/json')

        if obj_id is None:
            msg = ':'.join((request.user.username, object_type, perm))
        else:
            msg = ':'.join((request.user.username, object_type, obj_id, perm))

        data['permission-token'] = genhmac(settings.SHARED_SECRET, msg)
        jsondata = json.dumps(data)
        return HttpResponse(jsondata, content_type='application/json')
getperms = login_required(GetPerms.as_view())

<commit_msg>Fixed problem with getperm when used object_id.<commit_after>import json
from django.conf import settings
from django.contrib.auth.models import User
from django.core.exceptions import PermissionDenied
from django.http import HttpResponse
from django.views.generic import View
from django.shortcuts import get_object_or_404

from authmethods import auth_login, METHODS, auth_register, auth_validate, auth_census
from utils import genhmac, paginate
from utils import check_authmethod, check_pipeline, check_metadata
from .decorators import login_required
from .models import AuthEvent, ACL, CreditsAction
from .models import User, UserData
from django.db.models import Q


def permission_required(user, object_type, permission, object_id=None):
    if object_id and user.userdata.has_perms(object_type, permission, None):
        return
    if not user.userdata.has_perms(object_type, permission, object_id):
        raise PermissionDenied('Permission required: ' + permission)


class Test(View):
    ''' Test view that returns the response data '''

    def get(self, request):
        req = request.GET
        data = {'status': 'ok', 'method': 'GET'}
        data['get'] = req
        jsondata = json.dumps(data)
        return HttpResponse(jsondata, content_type='application/json')

    def post(self, request):
        req = json.loads(request.body.decode('utf-8'))
        data = {'status': 'ok', 'method': 'POST'}
        data['post'] = req
        jsondata = json.dumps(data)
        return HttpResponse(jsondata, content_type='application/json')
test = Test.as_view()


class Census(View):
    ''' Add census in the auth-event '''

    def post(self, request, pk):
        e = get_object_or_404(AuthEvent, pk=pk)
        data = auth_census(e, request)
        status = 200 if data['status'] == 'ok' else 400
        jsondata = json.dumps(data)
        return HttpResponse(jsondata, status=status, content_type='application/json')
census = login_required(Census.as_view())


class Login(View):
    ''' Login into the authapi '''

    def post(self, request, pk):
        req = json.loads(request.body.decode('utf-8'))
        if int(pk) == 0:
            e = 0
        else:
            e = get_object_or_404(AuthEvent, pk=pk)
        data = auth_login(e, req)
        status = 200 if data['status'] == 'ok' else 400
        jsondata = json.dumps(data)
        return HttpResponse(jsondata, status=status, content_type='application/json')
login = Login.as_view()


class Register(View):
    ''' Register into the authapi '''

    def post(self, request, pk):
        e = get_object_or_404(AuthEvent, pk=pk)
        #if (e.census == 'close'):
        #    permission_required(request.user, 'UserEvent', 'edit', pk)
        data = auth_register(e, request)
        status = 200 if data['status'] == 'ok' else 400
        jsondata = json.dumps(data)
        return HttpResponse(jsondata, status=status, content_type='application/json')
register = Register.as_view()


class Validate(View):
    ''' Validate into the authapi '''

    def post(self, request, pk):
        e = get_object_or_404(AuthEvent, pk=pk)
        data = auth_validate(e, request)
        status = 200 if data['status'] == 'ok' else 400
        jsondata = json.dumps(data)
        return HttpResponse(jsondata, status=status, content_type='application/json')
validate = Validate.as_view()


class AuthEventStatus(View):
    ''' Change the status of auth-event '''

    def get(self, request, pk, status):
        permission_required(request.user, 'AuthEvent', 'edit', pk)
        e = get_object_or_404(AuthEvent, pk=pk)
        if e.status != status:
            e.status = status
            e.save()
            st = 200
        else:
            st = 400
        jsondata = json.dumps({})
        return HttpResponse(jsondata, status=st, content_type='application/json')
ae_status = login_required(AuthEventStatus.as_view())


class GetPerms(View):
    ''' Returns the permission token if the user has this perm '''

    def post(self, request):
        data = {'status': 'ok'}
        req = json.loads(request.body.decode('utf-8'))

        if 'permission' not in req or 'object_type' not in req:
            jsondata = json.dumps(data)
            return HttpResponse(jsondata, status=400, content_type='application/json')

        object_type = req['object_type']
        perm = req['permission']
        obj_id = req.get('object_id', None)

        if not request.user.userdata.has_perms(object_type, perm, obj_id):
            jsondata = json.dumps(data)
            return HttpResponse(jsondata, status=400, content_type='application/json')

        if obj_id is None:
            msg = ':'.join((request.user.username, object_type, perm))
        else:
            msg = ':'.join((request.user.username, object_type, str(obj_id), perm))

        data['permission-token'] = genhmac(settings.SHARED_SECRET, msg)
        jsondata = json.dumps(data)
        return HttpResponse(jsondata, content_type='application/json')
getperms = login_required(GetPerms.as_view())


{'fraction_non_alphanumeric': 0.11837789484146705, 'fraction_numerical': 0.011728456415863234, 'mean_word_length': 3.1237704918032785, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '616855', 'n_tokens_mistral': 3116, 'n_tokens_neox': 3014, 'n_words': 778}