8687.15.11
by Karl Fogel
Add the copyright header block to files under lib/lp/answers/. |
1 |
# Copyright 2009 Canonical Ltd. This software is licensed under the
|
2 |
# GNU Affero General Public License version 3 (see the file LICENSE).
|
|
7944.3.11
by Francis J. Lacoste
Moved ZCML, templates and adapters. Split out karma and notifications. |
3 |
|
4 |
""" Karma for the Answer Tracker. """
|
|
5 |
||
6 |
__metaclass__ = type |
|
7 |
__all__ = [ |
|
8 |
'assignKarmaUsingQuestionContext', |
|
9 |
]
|
|
10 |
||
11 |
from canonical.database.sqlbase import block_implicit_flushes |
|
12915.5.1
by Curtis Hovey
Rename questionenums.py to the new standard or enums.py |
12 |
from lp.answers.enums import QuestionAction |
11403.1.4
by Henning Eggers
Reformatted imports using format-imports script r32. |
13 |
from lp.registry.interfaces.distribution import IDistribution |
7675.110.3
by Curtis Hovey
Ran the migration script to move registry code to lp.registry. |
14 |
from lp.registry.interfaces.person import IPerson |
10409.5.95
by Curtis Hovey
restored missing import. |
15 |
from lp.registry.interfaces.product import IProduct |
10409.5.55
by Curtis Hovey
Removed glob imports from answers. |
16 |
|
7944.3.11
by Francis J. Lacoste
Moved ZCML, templates and adapters. Split out karma and notifications. |
17 |
|
18 |
def assignKarmaUsingQuestionContext(person, question, actionname): |
|
19 |
"""Assign Karma with the given actionname to the given person.
|
|
20 |
||
21 |
Use the given question's context as the karma context.
|
|
22 |
"""
|
|
23 |
person.assignKarma( |
|
24 |
actionname, product=question.product, |
|
25 |
distribution=question.distribution, |
|
26 |
sourcepackagename=question.sourcepackagename) |
|
27 |
||
28 |
||
29 |
@block_implicit_flushes
|
|
30 |
def question_created(question, event): |
|
31 |
"""Assign karma to the user which created <question>."""
|
|
32 |
assignKarmaUsingQuestionContext( |
|
33 |
question.owner, question, 'questionasked') |
|
34 |
||
35 |
||
36 |
@block_implicit_flushes
|
|
37 |
def question_modified(question, event): |
|
38 |
"""Check changes made to <question> and assign karma to user if needed."""
|
|
39 |
user = IPerson(event.user) |
|
40 |
old_question = event.object_before_modification |
|
41 |
||
42 |
if old_question.description != question.description: |
|
43 |
assignKarmaUsingQuestionContext( |
|
44 |
user, question, 'questiondescriptionchanged') |
|
45 |
||
46 |
if old_question.title != question.title: |
|
47 |
assignKarmaUsingQuestionContext( |
|
48 |
user, question, 'questiontitlechanged') |
|
49 |
||
50 |
||
51 |
QuestionAction2KarmaAction = { |
|
52 |
QuestionAction.REQUESTINFO: 'questionrequestedinfo', |
|
53 |
QuestionAction.GIVEINFO: 'questiongaveinfo', |
|
54 |
QuestionAction.SETSTATUS: None, |
|
55 |
QuestionAction.COMMENT: 'questioncommentadded', |
|
56 |
QuestionAction.ANSWER: 'questiongaveanswer', |
|
12915.5.5
by Curtis Hovey
Hush lint. |
57 |
QuestionAction.CONFIRM: None, |
7944.3.11
by Francis J. Lacoste
Moved ZCML, templates and adapters. Split out karma and notifications. |
58 |
QuestionAction.EXPIRE: None, |
59 |
QuestionAction.REJECT: 'questionrejected', |
|
60 |
QuestionAction.REOPEN: 'questionreopened', |
|
61 |
}
|
|
62 |
||
63 |
||
64 |
@block_implicit_flushes
|
|
65 |
def question_comment_added(questionmessage, event): |
|
66 |
"""Assign karma to the user which added <questionmessage>."""
|
|
67 |
question = questionmessage.question |
|
68 |
karma_action = QuestionAction2KarmaAction.get(questionmessage.action) |
|
69 |
if karma_action: |
|
70 |
assignKarmaUsingQuestionContext( |
|
71 |
questionmessage.owner, question, karma_action) |
|
72 |
||
73 |
||
74 |
def get_karma_context_parameters(context): |
|
75 |
"""Return the proper karma context parameters based on the object."""
|
|
11235.5.4
by Curtis Hovey
hush lint. |
76 |
# XXX flacoste 2007-07-13 bug=125849:
|
77 |
# This should go away once bug #125849 is fixed.
|
|
7944.3.11
by Francis J. Lacoste
Moved ZCML, templates and adapters. Split out karma and notifications. |
78 |
params = dict(product=None, distribution=None) |
79 |
if IProduct.providedBy(context): |
|
80 |
params['product'] = context |
|
81 |
elif IDistribution.providedBy(context): |
|
82 |
params['distribution'] = context |
|
83 |
else: |
|
84 |
raise AssertionError('Unknown karma context: %r' % context) |
|
85 |
return params |
|
86 |
||
87 |
||
88 |
@block_implicit_flushes
|
|
89 |
def faq_created(faq, event): |
|
90 |
"""Assign karma to the user who created the FAQ."""
|
|
91 |
context = get_karma_context_parameters(faq.target) |
|
92 |
faq.owner.assignKarma('faqcreated', **context) |
|
93 |
||
94 |
||
95 |
@block_implicit_flushes
|
|
96 |
def faq_edited(faq, event): |
|
97 |
"""Assign karma to user who edited a FAQ."""
|
|
98 |
user = IPerson(event.user) |
|
99 |
old_faq = event.object_before_modification |
|
100 |
||
101 |
context = get_karma_context_parameters(faq.target) |
|
102 |
if old_faq.content != faq.content or old_faq.title != faq.title: |
|
103 |
user.assignKarma('faqedited', **context) |