~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/extensions/answersbugs/browser.py

  • Committer: Francis J. Lacoste
  • Date: 2009-03-14 01:40:44 UTC
  • mto: This revision was merged to the branch mainline in revision 8007.
  • Revision ID: francis.lacoste@canonical.com-20090314014044-v2kj19v230gk4109
Moved ZCML, templates and adapters. Split out karma and notifications.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2006-2009 Canonical Ltd.  All rights reserved.
 
2
 
 
3
"""Views for linking bugs and questions."""
 
4
 
 
5
__metaclass__ = type
 
6
__all__ = []
 
7
 
 
8
 
 
9
from zope.event import notify
 
10
 
 
11
from canonical.launchpad import _
 
12
from zope.interface import providedBy
 
13
 
 
14
from lazr.lifecycle.event import ObjectModifiedEvent
 
15
from lazr.lifecycle.snapshot import Snapshot
 
16
 
 
17
from canonical.launchpad.interfaces.bug import CreateBugParams,  IBug
 
18
from canonical.launchpad.webapp.publisher import canonical_url
 
19
from canonical.launchpad.webapp.launchpadform import action, LaunchpadFormView
 
20
 
 
21
class QuestionMakeBugView(LaunchpadFormView):
 
22
    """Browser class for adding a bug from a question."""
 
23
 
 
24
    schema = IBug
 
25
 
 
26
    field_names = ['title', 'description']
 
27
 
 
28
    def initialize(self):
 
29
        """Initialize the view when a Bug may be reported for the Question."""
 
30
        question = self.context
 
31
        if question.bugs:
 
32
            # we can't make a bug when we have linked bugs
 
33
            self.request.response.addErrorNotification(
 
34
                _('You cannot create a bug report from a question'
 
35
                  'that already has bugs linked to it.'))
 
36
            self.request.response.redirect(canonical_url(question))
 
37
            return
 
38
        LaunchpadFormView.initialize(self)
 
39
 
 
40
    @property
 
41
    def initial_values(self):
 
42
        """Return the initial form values."""
 
43
        question = self.context
 
44
        return {'title': '',
 
45
                'description': question.description}
 
46
 
 
47
    @action(_('Create Bug Report'), name='create')
 
48
    def create_action(self, action, data):
 
49
        """Create a Bug from a Question."""
 
50
        question = self.context
 
51
 
 
52
        unmodifed_question = Snapshot(
 
53
            question, providing=providedBy(question))
 
54
        params = CreateBugParams(
 
55
            owner=self.user, title=data['title'], comment=data['description'])
 
56
        bug = question.target.createBug(params)
 
57
        question.linkBug(bug)
 
58
        bug.subscribe(question.owner, self.user)
 
59
        bug_added_event = ObjectModifiedEvent(
 
60
            question, unmodifed_question, ['bugs'])
 
61
        notify(bug_added_event)
 
62
        self.request.response.addNotification(
 
63
            _('Thank you! Bug #$bugid created.', mapping={'bugid': bug.id}))
 
64
        self.next_url = canonical_url(bug)
 
65
 
 
66
 
 
67