<commit_before>from django.views.generic import TemplateView, CreateView
from django.shortcuts import redirect
from django.core.urlresolvers import reverse
from django.contrib import messages
from django.utils.html import strip_tags
from django.contrib.auth.views import password_reset_confirm as reset_view

from braces.views import LoginRequiredMixin

from allauth.account.models import EmailAddress
from allauth.account.forms import SignupForm
from allauth.account.utils import send_email_confirmation

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status

from applications.models import Application

from core.decorators import (
    handle_exceptions_for_ajax, handle_exceptions_for_admin
)
from projects.models import Project, Admins
from projects.base import STATUS
from datagroupings.models import Grouping

from .serializers import (
    UserSerializer, UserGroupSerializer, GroupingUserGroupSerializer
)
from .models import User, GroupingUserGroup
from .forms import (
    UsergroupCreateForm,
    CustomPasswordChangeForm
)


# ############################################################################
#
# ADMIN VIEWS
#
# ############################################################################


class Index(TemplateView):
    """
    Displays the splash page. Redirects to dashboard if a user is looged in.
    """
    template_name = 'index.html'

    def get(self, request, *args, **kwargs):
        if request.user.is_anonymous():
            return self.render_to_response(self.get_context_data)
        else:
            return redirect('admin:dashboard')


class Dashboard(LoginRequiredMixin, TemplateView):
    """
    Displays the dashboard.
    """
    template_name = 'dashboard.html'

    def get_context_data(self):
        projects = Project.objects.get_list(self.request.user)

        from extensions.base import extensions
        ext = []
        for ext_id in extensions.keys():
            extension = extensions.get(ext_id)

            if extension.get('display_admin') and (
                    not extension.get('superuser') or
                    self.request.user.is_superuser):
                ext.append(extension)

        return {
            'admin_projects': projects.filter(admins=self.request.user),
            'involved_projects': projects.exclude(
                admins=self.request.user).exists(),
            'status_types': STATUS,
            'extensions': ext
        }


class CreateUserMixin(object):
    def create_user(self, data):
        user = User.objects.create_user(
            data.get('email'),
            data.get('username'),
            password=data.get('<PASSWORD>')
        )
        user.save()
        return user


class UserAPIView(CreateUserMixin, APIView):
    def get(self, request):
        user = request.user
        if not user.is_anonymous():
            serializer = UserSerializer(user)
            return Response(
                serializer.data,
                status=status.HTTP_200_OK
            )
        else:
            return Response(
                {'error': 'You have to be signed in to get user information'},
                status=status.HTTP_401_UNAUTHORIZED
            )

    def patch(self, request):
        user = request.user

        if not user.is_anonymous():
            data = request.DATA
            serializer = UserSerializer(user, data=data, partial=True)
            if serializer.is_valid():
                serializer.save()

                if data.get('password') is not None:
                    user.reset_password(data.get('password'))
<commit_msg>Remove update password from API View<commit_after>from django.views.generic import TemplateView, CreateView
from django.shortcuts import redirect
from django.core.urlresolvers import reverse
from django.contrib import messages
from django.utils.html import strip_tags
from django.contrib.auth.views import password_reset_confirm as reset_view

from braces.views import LoginRequiredMixin

from allauth.account.models import EmailAddress
from allauth.account.forms import SignupForm
from allauth.account.utils import send_email_confirmation

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status

from applications.models import Application

from core.decorators import (
    handle_exceptions_for_ajax, handle_exceptions_for_admin
)
from projects.models import Project, Admins
from projects.base import STATUS
from datagroupings.models import Grouping

from .serializers import (
    UserSerializer, UserGroupSerializer, GroupingUserGroupSerializer
)
from .models import User, GroupingUserGroup
from .forms import (
    UsergroupCreateForm,
    CustomPasswordChangeForm
)


# ############################################################################
#
# ADMIN VIEWS
#
# ############################################################################


class Index(TemplateView):
    """
    Displays the splash page. Redirects to dashboard if a user is looged in.
    """
    template_name = 'index.html'

    def get(self, request, *args, **kwargs):
        if request.user.is_anonymous():
            return self.render_to_response(self.get_context_data)
        else:
            return redirect('admin:dashboard')


class Dashboard(LoginRequiredMixin, TemplateView):
    """
    Displays the dashboard.
    """
    template_name = 'dashboard.html'

    def get_context_data(self):
        projects = Project.objects.get_list(self.request.user)

        from extensions.base import extensions
        ext = []
        for ext_id in extensions.keys():
            extension = extensions.get(ext_id)

            if extension.get('display_admin') and (
                    not extension.get('superuser') or
                    self.request.user.is_superuser):
                ext.append(extension)

        return {
            'admin_projects': projects.filter(admins=self.request.user),
            'involved_projects': projects.exclude(
                admins=self.request.user).exists(),
            'status_types': STATUS,
            'extensions': ext
        }


class CreateUserMixin(object):
    def create_user(self, data):
        user = User.objects.create_user(
            data.get('email'),
            data.get('username'),
            password=data.get('<PASSWORD>')
        )
        user.save()
        return user


class UserAPIView(CreateUserMixin, APIView):
    def get(self, request):
        user = request.user
        if not user.is_anonymous():
            serializer = UserSerializer(user)
            return Response(
                serializer.data,
                status=status.HTTP_200_OK
            )
        else:
            return Response(
                {'error': 'You have to be signed in to get user information'},
                status=status.HTTP_401_UNAUTHORIZED
            )

    def patch(self, request):
        user = request.user

        if not user.is_anonymous():
            data = request.DATA
            serializer = UserSerializer(user, data=data, partial=True)
            if serializer.is_valid():
                serializer.save()

{'fraction_non_alphanumeric': 0.11968416678210278, 'fraction_numerical': 0.001662280094195872, 'mean_word_length': 2.729338842975207, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5124972', 'n_tokens_mistral': 1898, 'n_tokens_neox': 1824, 'n_words': 465}