<commit_before>from rest_framework.views import APIView
from rest_framework.generics import ListCreateAPIView
from rest_framework.response import Response
from django.contrib.auth.models import User, Group
from rest_framework import authentication, permissions
from django.contrib.auth import models
from apiv1.models import *
from apiv1.serializers import *
from django.core import serializers
from rest_framework import viewsets
from apiv1.utils import *
from django.http import Http404
from django.http import JsonResponse
from rest_framework.exceptions import APIException
import traceback
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
from django.contrib.auth import authenticate
from django.db.models import Q
from rest_framework.authentication import BasicAuthentication
import os
import uuid
import shutil
import threading
from django.db.models import Max

# from rest_framework.decorators import parser_classes
# from rest_framework.parsers import FormParser


def http404(request):
    return JsonResponse({
        'status': "404",
        'error': 'URI not found: {}'.format(request.path)
    })

class PageNotFound(APIException):
    status_code = 404
    default_detail = 'URI not found'
    def __init__(self, path):
        if path:
             self.detail = self.default_detail + ": " + path
        else:
             self.detail = self.default_detail

#@parser_classes((FormParser,))
class LoginView(APIView):

    def post(self, request):
        auth_user = request.data
        user = authenticate(username=auth_user.get("username"), password=auth_user.get("password"))
        if user is not None:
            # the password verified for the user
            if user.is_active:
                user_response = {'username': user.username, 'email': user.email, 'first_name': user.first_name, 'last_name': user.last_name}
                msg = {"status": "success", "message": "login success", "user": user_response}
            else:
                msg = {"status": "error", "message": "The password is valid, but the account has been disabled!"}
        else:
            # the authentication system was unable to verify from rest_framework.decorators import parser_classes
            msg = {"status": "error", "message": "The username or password is not valid"}
        return JsonResponse(msg)

#@parser_classes((FormParser,))
class RegistrationView(APIView):

    def post(self, request):
        try:
            user = request.data
            user_avalible = User.objects.filter(email=user.get("email")).first()
            if user_avalible:
                msg = {"status": "error", "message": "Email already existed"}
            else:
                user_avalible = User.objects.filter(username=user.get("username")).first()
                if user_avalible:
                    msg = {"status": "error", "message": "Username already existed"}
                else:
                    new_user = User.objects.create_user(user.get("username"), user.get("email"), user.get("password"))
                    new_user.first_name = user.get("first_name")
                    new_user.last_name = user.get("last_name")
                    new_user.save()
                    msg = {"status": "success", "message": "Create user success"}
        except Exception as e:
            msg = {"status": "error", "message": str(e)}
        return JsonResponse(msg)


class WebAppView(APIView):
    authentication_classes = (BasicAuthentication,)
    permission_classes = (IsAuthenticated,)

    def get(self, request, app_name):
        try:
            marathon_client = get_marathon_client()

            if app_name:
                app = request.user.webapp_set.get(name=app_name)
                serializer = WebAppSerializer(app)
            else:
                apps = request.user.webapp_set.all()
                serializer = WebAppSerializer(apps, many=True)
        except WebApp.DoesNotExist as e:
            traceback.print_exc()
            msg = {"status": "error", "message": "app {} does not exist".format(app_name)}
            return JsonResponse(msg)
        except Exception as e:
            traceback.print_exc()
            msg = {"status": "error", "message": str(e)}
            return JsonResponse(msg)

        return Response(serializer.data)

    def post(self, request, app_name):
        """Create new application"""

        if app_name:
            raise PageNotFound(request.path)
        new_app = request.data
        try:
            app = WebApp()
            app_name = new_app["name"]

            if request.user.webapp_set.filter(name=app_name):
                data = {"status": "error", "message": "app name {} already existed".format(app_name)}
            else:
                app.name = new_app["name"]
                app.github_url = new_app["github_url"]
                app.min_instances = new_app["min_instances"]
                app.max_instances = new_app["max_instances"]
                app.cpus = new_app["cpus"]
                app.mem = new_app["mem"]
                app.env_hostname = get_setting("env_hostname", "10.10.10.51")

                max_port = WebApp.objects.all().aggregate(Max('env_port'))["env_port__max"]
                if max_port and max_port >= int(get_setting("min_app_port", "31300")):
                    new_port = max_port + 1
                    if new_port > int(get_setting("max_app_port", "31399")):
                        msg = {"status": "error", "message": "Unavalible resource"}
                        return JsonResponse(msg)
                else:
                    new_port = int(get_setting("min_app_port", "31300"))

                app.env_port = new_port
                app.env_db_hostname = new_app["env_db_hostname"]
                app.env_db_port = new_app["env_db_port"]
                app.env_db_name = new_app["env_db_name"]
                app.env_db_username = new_app["env_db_username"]
                app.env_db_password = new_app["env_db_password"]


                cloning = threading.Thread(target=self.__cloning, args=(app,), daemon=True)
                cloning.start()
                app.user = request.user
                app.status = "cloning"
                app.uuid = str(uuid.uuid4())
                app.save()<commit_msg>fix bugs save app affter cloning app<commit_after>from rest_framework.views import APIView
from rest_framework.generics import ListCreateAPIView
from rest_framework.response import Response
from django.contrib.auth.models import User, Group
from rest_framework import authentication, permissions
from django.contrib.auth import models
from apiv1.models import *
from apiv1.serializers import *
from django.core import serializers
from rest_framework import viewsets
from apiv1.utils import *
from django.http import Http404
from django.http import JsonResponse
from rest_framework.exceptions import APIException
import traceback
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
from django.contrib.auth import authenticate
from django.db.models import Q
from rest_framework.authentication import BasicAuthentication
import os
import uuid
import shutil
import threading
from django.db.models import Max

# from rest_framework.decorators import parser_classes
# from rest_framework.parsers import FormParser


def http404(request):
    return JsonResponse({
        'status': "404",
        'error': 'URI not found: {}'.format(request.path)
    })

class PageNotFound(APIException):
    status_code = 404
    default_detail = 'URI not found'
    def __init__(self, path):
        if path:
             self.detail = self.default_detail + ": " + path
        else:
             self.detail = self.default_detail

#@parser_classes((FormParser,))
class LoginView(APIView):

    def post(self, request):
        auth_user = request.data
        user = authenticate(username=auth_user.get("username"), password=auth_user.get("password"))
        if user is not None:
            # the password verified for the user
            if user.is_active:
                user_response = {'username': user.username, 'email': user.email, 'first_name': user.first_name, 'last_name': user.last_name}
                msg = {"status": "success", "message": "login success", "user": user_response}
            else:
                msg = {"status": "error", "message": "The password is valid, but the account has been disabled!"}
        else:
            # the authentication system was unable to verify from rest_framework.decorators import parser_classes
            msg = {"status": "error", "message": "The username or password is not valid"}
        return JsonResponse(msg)

#@parser_classes((FormParser,))
class RegistrationView(APIView):

    def post(self, request):
        try:
            user = request.data
            user_avalible = User.objects.filter(email=user.get("email")).first()
            if user_avalible:
                msg = {"status": "error", "message": "Email already existed"}
            else:
                user_avalible = User.objects.filter(username=user.get("username")).first()
                if user_avalible:
                    msg = {"status": "error", "message": "Username already existed"}
                else:
                    new_user = User.objects.create_user(user.get("username"), user.get("email"), user.get("password"))
                    new_user.first_name = user.get("first_name")
                    new_user.last_name = user.get("last_name")
                    new_user.save()
                    msg = {"status": "success", "message": "Create user success"}
        except Exception as e:
            msg = {"status": "error", "message": str(e)}
        return JsonResponse(msg)


class WebAppView(APIView):
    authentication_classes = (BasicAuthentication,)
    permission_classes = (IsAuthenticated,)

    def get(self, request, app_name):
        try:
            marathon_client = get_marathon_client()

            if app_name:
                app = request.user.webapp_set.get(name=app_name)
                serializer = WebAppSerializer(app)
            else:
                apps = request.user.webapp_set.all()
                serializer = WebAppSerializer(apps, many=True)
        except WebApp.DoesNotExist as e:
            traceback.print_exc()
            msg = {"status": "error", "message": "app {} does not exist".format(app_name)}
            return JsonResponse(msg)
        except Exception as e:
            traceback.print_exc()
            msg = {"status": "error", "message": str(e)}
            return JsonResponse(msg)

        return Response(serializer.data)

    def post(self, request, app_name):
        """Create new application"""

        if app_name:
            raise PageNotFound(request.path)
        new_app = request.data
        try:
            app = WebApp()
            app_name = new_app["name"]

            if request.user.webapp_set.filter(name=app_name):
                data = {"status": "error", "message": "app name {} already existed".format(app_name)}
            else:
                app.name = new_app["name"]
                app.github_url = new_app["github_url"]
                app.min_instances = new_app["min_instances"]
                app.max_instances = new_app["max_instances"]
                app.cpus = new_app["cpus"]
                app.mem = new_app["mem"]
                app.env_hostname = get_setting("env_hostname", "10.10.10.51")

                max_port = WebApp.objects.all().aggregate(Max('env_port'))["env_port__max"]
                if max_port and max_port >= int(get_setting("min_app_port", "31300")):
                    new_port = max_port + 1
                    if new_port > int(get_setting("max_app_port", "31399")):
                        msg = {"status": "error", "message": "Unavalible resource"}
                        return JsonResponse(msg)
                else:
                    new_port = int(get_setting("min_app_port", "31300"))

                app.env_port = new_port
                app.env_db_hostname = new_app["env_db_hostname"]
                app.env_db_port = new_app["env_db_port"]
                app.env_db_name = new_app["env_db_name"]
                app.env_db_username = new_app["env_db_username"]
                app.env_db_password = new_app["env_db_password"]
                app.user = request.user
                app.status = "cloning"
                app.uuid = str(uuid.uuid4())
                app.save()

                cloning = threading.Thread(target=self.__cloning, args=(app,), daemon=True)
                cloning.start()

{'fraction_non_alphanumeric': 0.10534578852343197, 'fraction_numerical': 0.00627992778083052, 'mean_word_length': 2.3412011539470234, 'pattern_counts': {'":': 48, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '5915476', 'n_tokens_mistral': 3567, 'n_tokens_neox': 3404, 'n_words': 841}