<commit_before>from django.views.generic import TemplateView, RedirectView
from django.views.generic.edit import UpdateView
from django.shortcuts import redirect
from django.contrib import auth, messages
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.core.urlresolvers import reverse_lazy
from django.forms.models import modelform_factory

from opensubmit.forms import SettingsForm
from opensubmit.models import UserProfile


class IndexView(TemplateView):
    template_name = 'index.html'

    def get(self, request):
        if request.user.is_authenticated():
            return redirect('dashboard')
        else:
            return super(IndexView, self).get(request)


@method_decorator(login_required, name='dispatch')
class LogoutView(RedirectView):
    permanent = False
    pattern_name = 'index'

    def get(self, request):
        auth.logout(request)
        return super().get(request)


@method_decorator(login_required, name='dispatch')
class SettingsView(UpdateView):
    template_name = 'settings.html'
    form_class = SettingsForm
    success_url = reverse_lazy('dashboard')

    def form_valid(self, form):
        messages.info(self.request, 'User settings saved.')
        return super().form_valid(form)

    def get_object(self, queryset=None):
        return self.request.user


@method_decorator(login_required, name='dispatch')
class CoursesView(UpdateView):
    '''
    TODO: Weitermachen.
    '''
    template_name = 'courses.html'
    form_class = modelform_factory(UserProfile, fields=['courses'])
    success_url = reverse_lazy('dashboard')

    def form_valid(self, form):
        messages.info(self.request, 'Your choice of courses was saved.')
        return super().form_valid(form)

    def get_object(self, queryset=None):
        return self.request.user

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['courses'] = self.request.user.profile.user_courses()
        return context<commit_msg>Fix CBV for courses (frontend)<commit_after>from django.views.generic import TemplateView, RedirectView
from django.views.generic.edit import UpdateView
from django.shortcuts import redirect
from django.contrib import auth, messages
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.core.urlresolvers import reverse_lazy
from django.forms.models import modelform_factory

from opensubmit.forms import SettingsForm
from opensubmit.models import UserProfile


class IndexView(TemplateView):
    template_name = 'index.html'

    def get(self, request):
        if request.user.is_authenticated():
            return redirect('dashboard')
        else:
            return super(IndexView, self).get(request)


@method_decorator(login_required, name='dispatch')
class LogoutView(RedirectView):
    permanent = False
    pattern_name = 'index'

    def get(self, request):
        auth.logout(request)
        return super().get(request)


@method_decorator(login_required, name='dispatch')
class SettingsView(UpdateView):
    template_name = 'settings.html'
    form_class = SettingsForm
    success_url = reverse_lazy('dashboard')

    def form_valid(self, form):
        messages.info(self.request, 'User settings saved.')
        return super().form_valid(form)

    def get_object(self, queryset=None):
        return self.request.user


@method_decorator(login_required, name='dispatch')
class CoursesView(UpdateView):
    template_name = 'courses.html'
    form_class = modelform_factory(UserProfile, fields=['courses'])
    success_url = reverse_lazy('dashboard')

    def form_valid(self, form):
        messages.info(self.request, 'Your choice of courses was saved.')
        return super().form_valid(form)

    def get_object(self, queryset=None):
        return self.request.user.profile

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['courses'] = self.request.user.profile.user_courses()
        return context
{'fraction_non_alphanumeric': 0.09808670380237346, 'fraction_numerical': 0.0, 'mean_word_length': 4.1050679851668725, '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': '7462866', 'n_tokens_mistral': 1176, 'n_tokens_neox': 1154, 'n_words': 269}