<commit_before>from django.contrib import messages
from django.core.exceptions import PermissionDenied
from django.http import HttpResponseRedirect
from django.views.generic import (
    CreateView,
    DeleteView,
    DetailView,
    ListView,
    UpdateView
)

from braces.views import (
    LoginRequiredMixin,
    StaffuserRequiredMixin,
)

from .forms import (
    StoryAnonForm,
    StoryTrustForm,
)
from .models import Story
from base.view_utils import BaseMixin


def check_perm(user, story):
    """user must be a member of staff or have created the story"""
    if user.is_staff:
        pass
    elif not story.user == user:
        # the user did not create the story
        raise PermissionDenied()


class CheckPermMixin(object):

    def _check_perm(self, story):
        check_perm(self.request.user, story)


class StoryAnonCreateView(BaseMixin, CreateView):

    model = Story
    form_class = StoryAnonForm
    template_name = 'story/story_create_form.html'

    def get_context_data(self, **kwargs):
        """
        If a user is logged in (and active), they shouldn't be using this
        view!
        We don't do anything with the context... but it is a good place to
        check some stuff.
        """
        context = super(StoryAnonCreateView, self).get_context_data(**kwargs)
        if self.request.user and self.request.user.is_active:
            raise PermissionDenied()
        return context


class StoryTrustCreateView(
        LoginRequiredMixin, BaseMixin, CreateView):

    model = Story
    form_class = StoryTrustForm
    template_name = 'story/story_create_form.html'

    def get_context_data(self, **kwargs):
        """
        This view is for users who are logged in and active.<commit_msg>anon return to home page<commit_after>from django.contrib import messages
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.views.generic import (
    CreateView,
    DeleteView,
    DetailView,
    ListView,
    UpdateView,
)

from braces.views import (
    LoginRequiredMixin,
    StaffuserRequiredMixin,
)

from .forms import (
    StoryAnonForm,
    StoryTrustForm,
)
from .models import Story
from base.view_utils import BaseMixin


def check_perm(user, story):
    """user must be a member of staff or have created the story"""
    if user.is_staff:
        pass
    elif not story.user == user:
        # the user did not create the story
        raise PermissionDenied()


class CheckPermMixin(object):

    def _check_perm(self, story):
        check_perm(self.request.user, story)


class StoryAnonCreateView(BaseMixin, CreateView):

    model = Story
    form_class = StoryAnonForm
    template_name = 'story/story_create_form.html'

    def get_context_data(self, **kwargs):
        """
        If a user is logged in (and active), they shouldn't be using this
        view!
        We don't do anything with the context... but it is a good place to
        check some stuff.
        """
        context = super(StoryAnonCreateView, self).get_context_data(**kwargs)
        if self.request.user and self.request.user.is_active:
            raise PermissionDenied()
        return context

    def get_success_url(self):
        return reverse('project.home')


class StoryTrustCreateView(
        LoginRequiredMixin, BaseMixin, CreateView):

    model = Story
    form_class = StoryTrustForm
    template_name = 'story/story_create_form.html'

    def get_context_data(self, **kwargs):
        """
        This view is for users who are logged in and active.
{'fraction_non_alphanumeric': 0.07486187845303867, 'fraction_numerical': 0.0, 'mean_word_length': 2.9834983498349836, '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': '1147807', 'n_tokens_mistral': 1052, 'n_tokens_neox': 1008, 'n_words': 321}