<commit_before>import json

from django.db.models import F
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required, permission_required
from django.contrib.auth.models import User
from django.views.decorators.csrf import csrf_exempt
from django.views.generic.edit import UpdateView
from django.core.urlresolvers import reverse

from unicoremc.models import Project, Localisation
from unicoremc.forms import ProjectForm
from unicoremc.states import ProjectWorkflow
from unicoremc import constants
from unicoremc import tasks


import requests


def get_all_repos(request):
    url = ('https://api.github.com/orgs/universalcore/'
           'repos?type=public&per_page=100&page=%s')
    pageNum = 1
    repos = []
    while True:
        response = requests.get(url % pageNum)
        data = response.json()
        if not data:
            break
        repos.extend(data)
        pageNum += 1

    return HttpResponse(json.dumps(repos), mimetype='application/json')


@login_required
@permission_required('project.can_change')
def new_project_view(request, *args, **kwargs):
    social = request.user.social_auth.get(provider='github')
    access_token = social.extra_data['access_token']
    context = {
        'countries': constants.COUNTRY_CHOICES,
        'languages': Localisation.objects.all(),
        'app_types': Project.APP_TYPES,
        'access_token': access_token,
    }
    return render(request, 'unicoremc/new_project.html', context)


class ProjectEditView(UpdateView):
    model = Project
    form_class = ProjectForm
    template_name = 'unicoremc/advanced.html'

    def get_success_url(self):
        return reverse("home")

    def get_object(self, queryset=None):
        return get_object_or_404(Project, pk=self.kwargs['project_id'])

    def form_valid(self, form):
        response = super(ProjectEditView, self).form_valid(form)
        project = self.get_object()
        Project.objects.filter(
            pk=project.pk).update(project_version=F('project_version') + 1)

        project = self.get_object()
        project.create_pyramid_settings()
        return response


@csrf_exempt
@login_required
@permission_required('project.can_change')
def start_new_project(request, *args, **kwargs):
    if request.method == 'POST':

        app_type = request.POST.get('app_type')
        base_repo = request.POST.get('base_repo')
        country = request.POST.get('country')
        access_token = request.POST.get('access_token')
        user_id = request.POST.get('user_id')
        team_id = request.POST.get('team_id')

        user = User.objects.get(pk=user_id)
        project, created = Project.objects.get_or_create(
            app_type=app_type,
            base_repo_url=base_repo,
            country=country,
            team_id=int(team_id),
            owner=user)

        if created:
            tasks.start_new_project.delay(project.id, access_token)

    return HttpResponse(json.dumps({'success': True}),
                        mimetype='application/json')


@login_required
def projects_progress(request, *args, **kwargs):
    projects = Project.objects.all()
    return HttpResponse(json.dumps(
        [{
            'app_type': p.get_app_type_display(),
            'base_repo': p.base_repo_url,
            'state': ProjectWorkflow(instance=p).get_state(),
            'country': p.get_country_display(),
            'repo_url': p.repo_url or '',<commit_msg>mymetype -> content_type<commit_after>import json

from django.db.models import F
from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required, permission_required
from django.contrib.auth.models import User
from django.views.decorators.csrf import csrf_exempt
from django.views.generic.edit import UpdateView
from django.core.urlresolvers import reverse

from unicoremc.models import Project, Localisation
from unicoremc.forms import ProjectForm
from unicoremc.states import ProjectWorkflow
from unicoremc import constants
from unicoremc import tasks


import requests


def get_all_repos(request):
    url = ('https://api.github.com/orgs/universalcore/'
           'repos?type=public&per_page=100&page=%s')
    pageNum = 1
    repos = []
    while True:
        response = requests.get(url % pageNum)
        data = response.json()
        if not data:
            break
        repos.extend(data)
        pageNum += 1

    return HttpResponse(json.dumps(repos), content_type='application/json')


@login_required
@permission_required('project.can_change')
def new_project_view(request, *args, **kwargs):
    social = request.user.social_auth.get(provider='github')
    access_token = social.extra_data['access_token']
    context = {
        'countries': constants.COUNTRY_CHOICES,
        'languages': Localisation.objects.all(),
        'app_types': Project.APP_TYPES,
        'access_token': access_token,
    }
    return render(request, 'unicoremc/new_project.html', context)


class ProjectEditView(UpdateView):
    model = Project
    form_class = ProjectForm
    template_name = 'unicoremc/advanced.html'

    def get_success_url(self):
        return reverse("home")

    def get_object(self, queryset=None):
        return get_object_or_404(Project, pk=self.kwargs['project_id'])

    def form_valid(self, form):
        response = super(ProjectEditView, self).form_valid(form)
        project = self.get_object()
        Project.objects.filter(
            pk=project.pk).update(project_version=F('project_version') + 1)

        project = self.get_object()
        project.create_pyramid_settings()
        return response


@csrf_exempt
@login_required
@permission_required('project.can_change')
def start_new_project(request, *args, **kwargs):
    if request.method == 'POST':

        app_type = request.POST.get('app_type')
        base_repo = request.POST.get('base_repo')
        country = request.POST.get('country')
        access_token = request.POST.get('access_token')
        user_id = request.POST.get('user_id')
        team_id = request.POST.get('team_id')

        user = User.objects.get(pk=user_id)
        project, created = Project.objects.get_or_create(
            app_type=app_type,
            base_repo_url=base_repo,
            country=country,
            team_id=int(team_id),
            owner=user)

        if created:
            tasks.start_new_project.delay(project.id, access_token)

    return HttpResponse(json.dumps({'success': True}),
                        content_type='application/json')


@login_required
def projects_progress(request, *args, **kwargs):
    projects = Project.objects.all()
    return HttpResponse(json.dumps(
        [{
            'app_type': p.get_app_type_display(),
            'base_repo': p.base_repo_url,
            'state': ProjectWorkflow(instance=p).get_state(),
            'country': p.get_country_display(),
            'repo_url': p.repo_url or '',
{'fraction_non_alphanumeric': 0.10737111205794632, 'fraction_numerical': 0.0034086067319982955, 'mean_word_length': 3.242168674698795, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 2, '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': '3580462', 'n_tokens_mistral': 2142, 'n_tokens_neox': 2079, 'n_words': 403}