<commit_before># -*- coding: utf-8 -*-

from django.conf import settings
from django.http import HttpResponseRedirect, HttpResponse, HttpResponseBadRequest, Http404
from django.core.cache import cache
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.core.urlresolvers import reverse
from django.core.mail import EmailMessage
from django.shortcuts import render_to_response, get_object_or_404
from django.template.loader import render_to_string
from django.template import RequestContext, loader
from django.contrib.auth.models import User
from django.contrib import messages
from django.db.utils import IntegrityError
from django.db import transaction
from django.db.models import Q
from django.utils.decorators import method_decorator
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import ugettext
from django.utils.timezone import now

from django.views.decorators.csrf import ensure_csrf_cookie

from greenmine.core.generic import GenericView
from greenmine.core.decorators import login_required, staff_required

# Temporal imports
from greenmine.base.models import *
from greenmine.scrum.models import *

from greenmine.forms.base import *
from greenmine.questions.forms import *
from greenmine.scrum.forms.project import *
from greenmine.scrum.forms.milestone import *
from greenmine.core.utils import iter_points
from greenmine.core import signals

import os
import re

from datetime import timedelta

class UserStoryView(GenericView):
    template_name = "user-story-view.html"

    @login_required
    def get(self, request, pslug, iref):
        """ View US Detail """
        project = get_object_or_404(Project, slug=pslug)
        user_story = get_object_or_404(project.user_stories, ref=iref)

        self.check_role(request.user, project, [
            ('project', 'view'),
            ('milestone', 'view'),
            ('userstory', 'view'),
        ])

        context = {
            'user_story':user_story,
            'milestone':user_story.milestone,
            'project': project,
        }
        return self.render_to_response(self.template_name, context)


class UserStoryCreateView(GenericView):
    template_name = "user-story-create.html"

    @login_required
    def get(self, request, pslug, mid=None):
        project = get_object_or_404(Project, slug=pslug)

        self.check_role(request.user, project, [
            ('project', 'view'),
            ('milestone', ('view', 'edit')),
            ('userstory', ('view', 'create')),
        ])

        if mid is not None:
            milestone = get_object_or_404(project.milestones, pk=mid)
        else:
            milestone = None

        form = UserStoryForm(initial={'milestone': milestone})
        context = {
            'form':form,
            'project':project,
        }
        return self.render_to_response(self.template_name, context)

    @login_required
    def post(self, request, pslug, mid=None):
        project = get_object_or_404(Project, slug=pslug)

        self.check_role(request.user, project, [
            ('project', 'view'),
            ('milestone', ('view', 'edit')),
            ('userstory', ('view', 'create')),
        ])

        if mid is not None:
            milestone = get_object_or_404(project.milestones, pk=mid)
        else:
            milestone = None

        form = UserStoryForm(request.POST, initial={'milestone': milestone})

        if form.is_valid():
            instance = form.save(commit=False)
            instance.milestone = milestone
            instance.owner = request.user
            instance.project = project
            instance.save()

            #TODO: review this save_m2m
            form.save_m2m()

            signals.mail_userstory_created.send(sender=self, us=instance, user=request.user)
            self.create_asociated_tasks(project, instance)

            messages.info(request, _(u'The user story was created correctly'))
            return self.render_redirect(project.get_backlog_url())

        context = {
            'form':form,
            'project':project,
        }
        return self.render_to_response(self.template_name, context)

    def create_asociated_tasks(self, project, user_story):
        texts = list(project.get_extras().parse_ustext(user_story.description))
        tasks = []

        for text in texts:
            task = Task(
                user_story=user_story,
                ref = ref_uniquely(project, user_story.__class__),
                description = "",
                project = project,
                milestone = user_story.milestone,
                owner = self.request.user,
                subject = text
            )
            tasks.append(task)
<commit_msg>Fixing user story creating.<commit_after># -*- coding: utf-8 -*-

from django.conf import settings
from django.http import HttpResponseRedirect, HttpResponse, HttpResponseBadRequest, Http404
from django.core.cache import cache
from django.core.paginator import Paginator, InvalidPage, EmptyPage
from django.core.urlresolvers import reverse
from django.core.mail import EmailMessage
from django.shortcuts import render_to_response, get_object_or_404
from django.template.loader import render_to_string
from django.template import RequestContext, loader
from django.contrib.auth.models import User
from django.contrib import messages
from django.db.utils import IntegrityError
from django.db import transaction
from django.db.models import Q
from django.utils.decorators import method_decorator
from django.utils.translation import ugettext_lazy as _
from django.utils.translation import ugettext
from django.utils.timezone import now

from django.views.decorators.csrf import ensure_csrf_cookie

from greenmine.core.generic import GenericView
from greenmine.core.decorators import login_required, staff_required

# Temporal imports
from greenmine.base.models import *
from greenmine.scrum.models import *

from greenmine.forms.base import *
from greenmine.questions.forms import *
from greenmine.scrum.forms.project import *
from greenmine.scrum.forms.milestone import *
from greenmine.core.utils import iter_points
from greenmine.core import signals

import os
import re

from datetime import timedelta

class UserStoryView(GenericView):
    template_name = "user-story-view.html"

    @login_required
    def get(self, request, pslug, iref):
        """ View US Detail """
        project = get_object_or_404(Project, slug=pslug)
        user_story = get_object_or_404(project.user_stories, ref=iref)

        self.check_role(request.user, project, [
            ('project', 'view'),
            ('milestone', 'view'),
            ('userstory', 'view'),
        ])

        context = {
            'user_story':user_story,
            'milestone':user_story.milestone,
            'project': project,
        }
        return self.render_to_response(self.template_name, context)


class UserStoryCreateView(GenericView):
    template_name = "user-story-create.html"

    @login_required
    def get(self, request, pslug, mid=None):
        project = get_object_or_404(Project, slug=pslug)

        self.check_role(request.user, project, [
            ('project', 'view'),
            ('milestone', ('view', 'edit')),
            ('userstory', ('view', 'create')),
        ])

        if mid is not None:
            milestone = get_object_or_404(project.milestones, pk=mid)
        else:
            milestone = None

        form = UserStoryForm(initial={'milestone': milestone})
        context = {
            'form':form,
            'project':project,
        }
        return self.render_to_response(self.template_name, context)

    @login_required
    def post(self, request, pslug, mid=None):
        project = get_object_or_404(Project, slug=pslug)

        self.check_role(request.user, project, [
            ('project', 'view'),
            ('milestone', ('view', 'edit')),
            ('userstory', ('view', 'create')),
        ])

        if mid is not None:
            milestone = get_object_or_404(project.milestones, pk=mid)
        else:
            milestone = None

        form = UserStoryForm(request.POST, initial={'milestone': milestone})

        if not form.is_valid():
            return self.render_json_error(form.errors)

        instance = form.save(commit=False)
        instance.milestone = milestone
        instance.owner = request.user
        instance.project = project
        instance.save()

        #TODO: review this save_m2m
        form.save_m2m()

        signals.mail_userstory_created.send(sender=self, us=instance, user=request.user)
        self.create_asociated_tasks(project, instance)

        #messages.info(request, _(u'The user story was created correctly'))
        return self.render_json({"redirect_to": project.get_backlog_url()})

    def create_asociated_tasks(self, project, user_story):
        texts = list(project.get_extras().parse_ustext(user_story.description))
        tasks = []

        for text in texts:
            task = Task(
                user_story=user_story,
                ref = ref_uniquely(project, user_story.__class__),
                description = "",
                project = project,
                milestone = user_story.milestone,
                owner = self.request.user,
                subject = text
            )
            tasks.append(task)

{'fraction_non_alphanumeric': 0.09639706663832501, 'fraction_numerical': 0.00573918588585397, 'mean_word_length': 2.7579872204472844, 'pattern_counts': {'":': 1, '<': 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': '5986693', 'n_tokens_mistral': 2697, 'n_tokens_neox': 2590, 'n_words': 615}