<commit_before>import json

from rest_framework.response import Response
from rest_framework import viewsets, filters
from rest_framework_json_api.views import RelationshipView

from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from submissions.permissions import SubmissionPermissions

from submissions.serializers import SubmissionSerializer

from submissions.models import Submission
from approvals.models import Approval
from allauth.socialaccount.models import SocialToken
from allauth.socialaccount.models import SocialAccount

import requests
from api.apps import OsfOauth2AdapterConfig


class SubmissionViewSet(viewsets.ModelViewSet):
    resource_name = 'submissions'
    serializer_class = SubmissionSerializer
    lookup_url_kwarg = 'submission_id'
    lookup_field = 'pk'
    permission_classes = (SubmissionPermissions,)
    filter_backends = (filters.DjangoObjectPermissionsFilter,)

    base_url = '{}oauth2/{}'.format(OsfOauth2AdapterConfig.osf_accounts_url, '{}')
    access_token_url = base_url.format('token')
    profile_url = '{}v2/users/me/'.format(OsfOauth2AdapterConfig.osf_api_url)
    node_url = '{}v2/nodes/'.format(OsfOauth2AdapterConfig.osf_api_url)

    def get_queryset(self):
        conference_id = self.kwargs.get('conference_id')
        return Submission.objects.filter(conference_id=conference_id)

    @method_decorator(login_required)
    def create(self, request, *args, **kwargs):
        serializer = SubmissionSerializer(data=request.data, context={'request': request})
        new_approval = Approval.objects.create()
        contributor = request.user

        current_user = request.user.username
        account = SocialAccount.objects.get(uid=current_user)
        osf_token = SocialToken.objects.get(account=account)

        if not request.user.has_perm('submissions.can_set_contributor'):
            if serializer.is_valid():
                node = {
                    'data': {
                        'attributes': {
                            'category': 'project',
                            'description': request.data['description'],
                            'title': request.data['title']
                            },
                        'type': 'nodes'
                        }
                    }

                response = requests.post(
                        self.node_url,
                        data=json.dumps(node),
                        headers={
                            'Authorization': 'Bearer {}'.format(osf_token),
                            'Content-Type': 'application/json; charset=UTF-8',
                            'Accept': 'application/json, text/*'
                            }
                        )

                obj = response.json()
                serializer.save(
                    contributor=contributor, approval=new_approval, node_id=obj['data']['id'])

                return Response(serializer.data)<commit_msg>Fix indents<commit_after>import json

from rest_framework.response import Response
from rest_framework import viewsets, filters
from rest_framework_json_api.views import RelationshipView

from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from submissions.permissions import SubmissionPermissions

from submissions.serializers import SubmissionSerializer

from submissions.models import Submission
from approvals.models import Approval
from allauth.socialaccount.models import SocialToken
from allauth.socialaccount.models import SocialAccount

import requests
from api.apps import OsfOauth2AdapterConfig


class SubmissionViewSet(viewsets.ModelViewSet):
    resource_name = 'submissions'
    serializer_class = SubmissionSerializer
    lookup_url_kwarg = 'submission_id'
    lookup_field = 'pk'
    permission_classes = (SubmissionPermissions,)
    filter_backends = (filters.DjangoObjectPermissionsFilter,)

    base_url = '{}oauth2/{}'.format(OsfOauth2AdapterConfig.osf_accounts_url, '{}')
    access_token_url = base_url.format('token')
    profile_url = '{}v2/users/me/'.format(OsfOauth2AdapterConfig.osf_api_url)
    node_url = '{}v2/nodes/'.format(OsfOauth2AdapterConfig.osf_api_url)

    def get_queryset(self):
        conference_id = self.kwargs.get('conference_id')
        return Submission.objects.filter(conference_id=conference_id)

    @method_decorator(login_required)
    def create(self, request, *args, **kwargs):
        serializer = SubmissionSerializer(data=request.data, context={'request': request})
        new_approval = Approval.objects.create()
        contributor = request.user

        current_user = request.user.username
        account = SocialAccount.objects.get(uid=current_user)
        osf_token = SocialToken.objects.get(account=account)

        if not request.user.has_perm('submissions.can_set_contributor'):
            if serializer.is_valid():
                node = {
                    'data': {
                        'attributes': {
                            'category': 'project',
                            'description': request.data['description'],
                            'title': request.data['title']
                            },
                        'type': 'nodes'
                        }
                    }

                response = requests.post(
                    self.node_url,
                    data=json.dumps(node),
                    headers={
                        'Authorization': 'Bearer {}'.format(osf_token),
                        'Content-Type': 'application/json; charset=UTF-8',
                        'Accept': 'application/json, text/*'
                    }
                )

                obj = response.json()
                serializer.save(
                    contributor=contributor, approval=new_approval, node_id=obj['data']['id'])

                return Response(serializer.data)
{'fraction_non_alphanumeric': 0.09121395036887994, 'fraction_numerical': 0.0026827632461435278, 'mean_word_length': 2.336129753914989, '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': '5874274', 'n_tokens_mistral': 1581, 'n_tokens_neox': 1521, 'n_words': 278}