<commit_before>from rest_framework import viewsets
import requests
from rest_framework import status
import jwt
import json
from troposphere import settings
from Crypto.Hash import SHA256
from rest_framework.response import Response
from django.contrib.auth.models import User
from api.models import UserPreferences
from .serializers import UserSerializer, UserPreferenceSerializer


class UserViewSet(viewsets.ReadOnlyModelViewSet):
    """
    API endpoint that allows users to be viewed.
    """
    queryset = User.objects.all()
    serializer_class = UserSerializer
    filter_fields = ('email',)
    http_method_names = ['get', 'head', 'options', 'trace']

    def get_queryset(self):
        """
        Filter users to return only current user
        """
        user = self.request.user
        return User.objects.filter(username=user.username)


class UserPreferenceViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed.
    """
    queryset = UserPreferences.objects.all()
    serializer_class = UserPreferenceSerializer
    http_method_names = ['get', 'put', 'patch', 'head', 'options', 'trace']

    def get_queryset(self):
        """
        Filter users to return only current user
        """
        user = self.request.user
        return UserPreferences.objects.filter(user=user)


class BadgeViewSet(viewsets.GenericViewSet):
    queryset = UserPreferences.objects.none()
    serializer_class = UserPreferenceSerializer
    http_method_names = ['get', 'post', 'head', 'options', 'trace']

    def create(self, request, *args, **kwargs):
        url = settings.BADGE_API_HOST
        email_address = str(User.objects.get(username=self.request.user).email)
        slug = settings.BADGE_SYSTEM_SLUG
        secret = settings.BADGE_SECRET
        badge = str(self.request.data['badgeSlug'])
        path = '/systems/' + slug + '/badges/' + badge + '/instances'
        header = {"typ": "JWT", "alg": 'HS256'}
        body = json.dumps({"email": email_address})

        computed_hash = SHA256.new()
        computed_hash.update(body)

        payload = {'key': "master", 'method': "POST", 'path': path, "body": {"alg": "sha256", "hash": computed_hash.hexdigest()}}
        token = jwt.encode(payload, secret, headers=header)

        options = {
            'method': 'POST',
            'url': url + path,
            'headers': {
                'Authorization': 'JWT token="' + token + '"',
                'Content-Type': 'application/json'
            }
        }
        try:
            r = requests.post(url + path, data=body, headers=options['headers'], verify=False)
            data = r.json()
        except: 
            data = "Error"
        return Response(data=data, status=status.HTTP_201_CREATED)

    def retrieve(self, request, *args, **kwargs):
        url = settings.BADGE_API_HOST
        email = User.objects.get(username=self.request.user).email
        slug = settings.BADGE_SYSTEM_SLUG
        name = settings.BADGE_SYSTEM_NAME
        secret = settings.BADGE_SECRET

        path = '/systems/' + slug + '/instances/' + email
        header = {"typ": "JWT", "alg": 'HS256'}
        body = str({"slug": slug, "name": name, "url": url + path})

        computed_hash = SHA256.new()
        computed_hash.update(body)

        payload = {'key': "master", 'method': "GET", 'path': path, "body": {"alg": "sha256", "hash": computed_hash.hexdigest()}}
        token = jwt.encode(payload, secret, headers=header)

        options = {
            'method': 'GET',
            'url': url + path,
            'headers': {
                'Authorization': 'JWT token="' + token + '"',
                'Content-Type': 'application/json'
            }
        }
        
        try:
            r = requests.get(url + path, headers=options['headers'], verify=False)
            data = r.json()
        except:
            data = "Error"
        return Response(data=data, status=status.HTTP_200_OK)

    def list(self, request, *args, **kwargs):
        url = settings.BADGE_API_HOST
        slug = settings.BADGE_SYSTEM_SLUG
        name = settings.BADGE_SYSTEM_NAME
        secret = settings.BADGE_SECRET

        path = '/systems/' + slug + '/badges'
        header = {"typ": "JWT", "alg": 'HS256'}
        body = str({"slug": slug, "name": name, "url": url + path})

        computed_hash = SHA256.new()
        computed_hash.update(body)

        payload = {'key': "master", 'method': "GET", 'path': path, "body": {"alg": "sha256", "hash": computed_hash.hexdigest()}}
        token = jwt.encode(payload, secret, headers=header)

        options = {
            'method': 'GET',
            'url': url + path,
            'headers': {
                'Authorization': 'JWT token="' + token + '"',
                'Content-Type': 'application/json'
            }
        }

        try:
            r = requests.get(url + path, headers=options['headers'], verify=False)
            data=r.json()<commit_msg>change badge query granularity based on settings<commit_after>from rest_framework import viewsets
import requests
from rest_framework import status
import jwt
import json
from troposphere import settings
from Crypto.Hash import SHA256
from rest_framework.response import Response
from django.contrib.auth.models import User
from api.models import UserPreferences
from .serializers import UserSerializer, UserPreferenceSerializer


class UserViewSet(viewsets.ReadOnlyModelViewSet):
    """
    API endpoint that allows users to be viewed.
    """
    queryset = User.objects.all()
    serializer_class = UserSerializer
    filter_fields = ('email',)
    http_method_names = ['get', 'head', 'options', 'trace']

    def get_queryset(self):
        """
        Filter users to return only current user
        """
        user = self.request.user
        return User.objects.filter(username=user.username)


class UserPreferenceViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed.
    """
    queryset = UserPreferences.objects.all()
    serializer_class = UserPreferenceSerializer
    http_method_names = ['get', 'put', 'patch', 'head', 'options', 'trace']

    def get_queryset(self):
        """
        Filter users to return only current user
        """
        user = self.request.user
        return UserPreferences.objects.filter(user=user)


class BadgeViewSet(viewsets.GenericViewSet):
    queryset = UserPreferences.objects.none()
    serializer_class = UserPreferenceSerializer
    http_method_names = ['get', 'post', 'head', 'options', 'trace']

    def create(self, request, *args, **kwargs):
        url = settings.BADGE_API_HOST
        email_address = str(User.objects.get(username=self.request.user).email)
        secret = settings.BADGE_SECRET
        badge = str(self.request.data['badgeSlug'])
        
        if settings.BADGE_PROGRAM_SLUG:
            path = '/systems/' + settings.BADGE_SYSTEM_SLUG + '/issuers/' + settings.BADGE_ISSUER_SLUG + '/programs/' + settings.BADGE_PROGRAM_SLUG + '/badges/' + badge + '/instances'
            
        elif settings.BADGE_ISSUER_SLUG:
            path = '/systems/' + settings.BADGE_SYSTEM_SLUG + '/issuers/' + settings.BADGE_ISSUER_SLUG + '/badges/' + badge + '/instances'
        
        else:
            path = '/systems/' + settings.BADGE_SYSTEM_SLUG + '/badges/' + badge + '/instances'

        path = '/systems/' + settings.BADGE_SYSTEM_SLUG + '/badges/' + badge + '/instances'
        header = {"typ": "JWT", "alg": 'HS256'}
        body = json.dumps({"email": email_address})

        computed_hash = SHA256.new()
        computed_hash.update(body)

        payload = {'key': "master", 'method': "POST", 'path': path, "body": {"alg": "sha256", "hash": computed_hash.hexdigest()}}
        token = jwt.encode(payload, secret, headers=header)

        options = {
            'method': 'POST',
            'url': url + path,
            'headers': {
                'Authorization': 'JWT token="' + token + '"',
                'Content-Type': 'application/json'
            }
        }
        try:
            r = requests.post(url + path, data=body, headers=options['headers'], verify=False)
            data = r.json()
        except: 
            data = "Error"
        return Response(data=data, status=status.HTTP_201_CREATED)

    def retrieve(self, request, *args, **kwargs):
        url = settings.BADGE_API_HOST
        email = User.objects.get(username=self.request.user).email
        name = settings.BADGE_SYSTEM_NAME
        secret = settings.BADGE_SECRET
        
        path = '/systems/' + settings.BADGE_SYSTEM_SLUG + '/instances/' + email
        
        header = {"typ": "JWT", "alg": 'HS256'}
        body = str({"slug": settings.BADGE_SYSTEM_SLUG, "name": name, "url": url + path})

        computed_hash = SHA256.new()
        computed_hash.update(body)

        payload = {'key': "master", 'method': "GET", 'path': path, "body": {"alg": "sha256", "hash": computed_hash.hexdigest()}}
        token = jwt.encode(payload, secret, headers=header)

        options = {
            'method': 'GET',
            'url': url + path,
            'headers': {
                'Authorization': 'JWT token="' + token + '"',
                'Content-Type': 'application/json'
            }
        }
        
        try:
            r = requests.get(url + path, headers=options['headers'], verify=False)
            data = r.json()
        except:
            data = "Error" 
        
        if settings.BADGE_PROGRAM_SLUG:
            to_return = {'instances': []}
            for instance in data['instances']:
                if 'program' in instance['badge']:
                    to_return['instances'].append(instance)
            return Response(data=to_return, status=status.HTTP_200_OK)

        elif settings.BADGE_ISSUER_SLUG:
            to_return = {'instances': []}
            for instance in data['instances']:
                if 'issuer' in instance['badge']:
                    to_return['instances'].append(instance)
            return Response(data=to_return, status=status.HTTP_200_OK)

        else:
            return Response(data=data, status=status.HTTP_200_OK)


    def list(self, request, *args, **kwargs):
        url = settings.BADGE_API_HOST
        name = settings.BADGE_SYSTEM_NAME
        secret = settings.BADGE_SECRET

        if not settings.BADGE_SYSTEM_SLUG:
            return "Error"
 
        if settings.BADGE_PROGRAM_SLUG:
            path = '/systems/' + settings.BADGE_SYSTEM_SLUG + '/issuers/' + settings.BADGE_ISSUER_SLUG + '/programs/' + settings.BADGE_PROGRAM_SLUG + '/badges'
        
        elif settings.BADGE_ISSUER_SLUG:
            path = '/systems/' + settings.BADGE_SYSTEM_SLUG + '/issuers/' + settings.BADGE_ISSUER_SLUG + '/badges'
        
        else:
            path = '/systems/' + settings.BADGE_SYSTEM_SLUG + '/badges'

        header = {"typ": "JWT", "alg": 'HS256'}
        body = str({"slug": settings.BADGE_SYSTEM_SLUG, "name": name, "url": url + path})

        computed_hash = SHA256.new()
        computed_hash.update(body)

        payload = {'key': "master", 'method': "GET", 'path': path, "body": {"alg": "sha256", "hash": computed_hash.hexdigest()}}
        token = jwt.encode(payload, secret, headers=header)

        options = {
            'method': 'GET',
            'url': url + path,
            'headers': {
                'Authorization': 'JWT token="' + token + '"',
                'Content-Type': 'application/json'
            }
        }

        try:
            r = requests.get(url + path, headers=options['headers'], verify=False)
            data=r.json()
{'fraction_non_alphanumeric': 0.13561502830674216, 'fraction_numerical': 0.0066906845084920225, 'mean_word_length': 2.3922025021821356, 'pattern_counts': {'":': 44, '<': 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': '2298453', 'n_tokens_mistral': 3509, 'n_tokens_neox': 3370, 'n_words': 771}