<commit_before>from django.views.generic import DetailView, ListView, CreateView, UpdateView, DeleteView
from django.core.urlresolvers import reverse_lazy

from braces.views import LoginRequiredMixin

from femstats.fertility.models import Period, Fertility
from femstats.fertility.forms import PeriodForm, FertilityForm

class PeriodsList(LoginRequiredMixin, ListView):
    model = Period
    template_name = "fertility/period_list.html"
    context_object_name = 'period_list'

    def get_queryset(self):
        '''
        Filter data to display entries from the currently logged in user
        '''
        return Period.objects.filter(user = self.request.user)

# Form submits, a model instance is saved.
# However it redirects to fertility/periods/add/fertility/periods
class PeriodCreate(LoginRequiredMixin, CreateView):
    model = Period
    template_name = "fertility/period_form.html"
    form_class = PeriodForm
    success_url = "fertility/periods/"

    def form_valid(self, form):
        form.instance.user = self.request.user
        return super(PeriodCreate, self).form_valid(form)

# absolute_url on period model now works as success_url
class PeriodUpdate(LoginRequiredMixin, UpdateView):
    model = Period
    template_name = "fertility/period_form.html"
    form_class = PeriodForm

class PeriodDetail(LoginRequiredMixin, DetailView):
    model = Period
    template_name = "fertility/period_detail.html"
    context_object_name = "period"

class PeriodDelete(LoginRequiredMixin, DeleteView):
    model = Period
    success_url = reverse_lazy('fertility:periods')

class FertilityCreate(LoginRequiredMixin, CreateView):
    form_class = FertilityForm
    template_name = "fertility/fertility_form.html"

    def form_valid(self, form):
        form.instance.user = self.request.user
        return super(FertilityCreate, self).form_valid(form)

class FertilityUpdate(LoginRequiredMixin, UpdateView):
    model = Fertility
    form_class = FertilityForm
    template_name = "fertility/fertility_form.html"

class FertilityDetail(LoginRequiredMixin, DetailView):<commit_msg>raise PermissionDenied for viewing or updating an object that does not belong to currently logged in user<commit_after>from django.views.generic import DetailView, ListView, CreateView, UpdateView, DeleteView
from django.core.urlresolvers import reverse_lazy
from django.core.exceptions import PermissionDenied

from braces.views import LoginRequiredMixin

from femstats.fertility.models import Period, Fertility
from femstats.fertility.forms import PeriodForm, FertilityForm

class PeriodsList(LoginRequiredMixin, ListView):
    model = Period
    template_name = "fertility/period_list.html"
    context_object_name = 'period_list'

    def get_queryset(self):
        '''
        Filter data to display entries from the currently logged in user
        '''
        return Period.objects.filter(user = self.request.user)

# Form submits, a model instance is saved.
# However it redirects to fertility/periods/add/fertility/periods
class PeriodCreate(LoginRequiredMixin, CreateView):
    model = Period
    template_name = "fertility/period_form.html"
    form_class = PeriodForm
    success_url = "fertility/periods/"

    def form_valid(self, form):
        form.instance.user = self.request.user
        return super(PeriodCreate, self).form_valid(form)

# absolute_url on period model now works as success_url
class PeriodUpdate(LoginRequiredMixin, UpdateView):
    model = Period
    template_name = "fertility/period_form.html"
    form_class = PeriodForm

    def get(self, request, *args, **kwargs):
        self.object = self.get_object()
    # Ensure that users can not view or update an object
    # that does not match the currently logged in user
    # and display the forbidden page
        if self.object.user != request.user:
            raise PermissionDenied
        else:
            return super(PeriodUpdate, self).get(request, *args, **kwargs)

class PeriodDetail(LoginRequiredMixin, DetailView):
    model = Period
    template_name = "fertility/period_detail.html"
    context_object_name = "period"

class PeriodDelete(LoginRequiredMixin, DeleteView):
    model = Period
    success_url = reverse_lazy('fertility:periods')

class FertilityCreate(LoginRequiredMixin, CreateView):
    form_class = FertilityForm
    template_name = "fertility/fertility_form.html"

    def form_valid(self, form):
        form.instance.user = self.request.user
        return super(FertilityCreate, self).form_valid(form)

class FertilityUpdate(LoginRequiredMixin, UpdateView):
    model = Fertility
    form_class = FertilityForm
    template_name = "fertility/fertility_form.html"

    def get(self, request, *args, **kwargs):
        self.object = self.get_object()
    # Ensure that users can not view or update an object
    # that does not match the currently logged in user
    # and display the forbidden page
        if self.object.user != request.user:
            raise PermissionDenied
        else:
            return super(FertilityUpdate, self).get(request, *args, **kwargs)


class FertilityDetail(LoginRequiredMixin, DetailView):
{'fraction_non_alphanumeric': 0.07689320388349515, 'fraction_numerical': 0.0, 'mean_word_length': 4.05, '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': '4010001', 'n_tokens_mistral': 1490, 'n_tokens_neox': 1429, 'n_words': 387}