1 | ## $Id: product.py 12670 2015-03-06 18:37:06Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2014 Uli Fouquet & Henrik Bettermann |
---|
4 | ## This program is free software; you can redistribute it and/or modify |
---|
5 | ## it under the terms of the GNU General Public License as published by |
---|
6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
7 | ## (at your option) any later version. |
---|
8 | ## |
---|
9 | ## This program is distributed in the hope that it will be useful, |
---|
10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | ## GNU General Public License for more details. |
---|
13 | ## |
---|
14 | ## You should have received a copy of the GNU General Public License |
---|
15 | ## along with this program; if not, write to the Free Software |
---|
16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
17 | ## |
---|
18 | """ |
---|
19 | These are the products (e.g. licenses). |
---|
20 | """ |
---|
21 | import grok |
---|
22 | from time import time |
---|
23 | from grok import index |
---|
24 | from datetime import date |
---|
25 | from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState |
---|
26 | from zope.event import notify |
---|
27 | from zope.catalog.interfaces import ICatalog |
---|
28 | from zope.component import getUtility |
---|
29 | from zope.component.interfaces import IFactory, ComponentLookupError |
---|
30 | from zope.interface import implementedBy |
---|
31 | from zope.i18n import translate |
---|
32 | from waeup.ikoba.interfaces import IIkobaUtils |
---|
33 | from waeup.ikoba.interfaces import MessageFactory as _ |
---|
34 | from waeup.ikoba.utils.helpers import attrs_to_fields, get_current_principal |
---|
35 | from waeup.ikoba.products.interfaces import ( |
---|
36 | IProduct, IProduct,) |
---|
37 | |
---|
38 | class Product(grok.Container): |
---|
39 | """This is a product. |
---|
40 | """ |
---|
41 | grok.implements(IProduct) |
---|
42 | grok.provides(IProduct) |
---|
43 | #grok.baseclass() |
---|
44 | |
---|
45 | form_fields_interface = IProduct |
---|
46 | |
---|
47 | local_roles = [ |
---|
48 | 'waeup.local.ProductManager', |
---|
49 | ] |
---|
50 | |
---|
51 | def __init__(self, *args, **kw): |
---|
52 | super(Product, self).__init__(*args, **kw) |
---|
53 | self.tc_dict = {} |
---|
54 | self.description_dict = {} |
---|
55 | |
---|
56 | @property |
---|
57 | def contract_category_title(self): |
---|
58 | cc_dict = getUtility(IIkobaUtils).CON_CATS_DICT |
---|
59 | return cc_dict[self.contract_category] |
---|
60 | |
---|
61 | @property |
---|
62 | def contract_autotitle(self): |
---|
63 | if self.contract_title: |
---|
64 | return self.contract_title |
---|
65 | return self.title |
---|
66 | |
---|
67 | @property |
---|
68 | def active(self): |
---|
69 | now = date.today() |
---|
70 | return (not self.valid_from or self.valid_from <= now) and \ |
---|
71 | (not self.valid_to or self.valid_to >= now) |
---|
72 | |
---|
73 | Product = attrs_to_fields(Product) |
---|
74 | |
---|
75 | |
---|
76 | class ProductFactory(grok.GlobalUtility): |
---|
77 | """A factory for products. |
---|
78 | """ |
---|
79 | grok.implements(IFactory) |
---|
80 | grok.name(u'waeup.Product') |
---|
81 | title = u"Create a new product.", |
---|
82 | description = u"This factory instantiates new products." |
---|
83 | |
---|
84 | def __call__(self, *args, **kw): |
---|
85 | return Product(*args, **kw) |
---|
86 | |
---|
87 | def getInterfaces(self): |
---|
88 | return implementedBy(Product) |
---|
89 | |
---|
90 | @grok.subscribe(IProduct, grok.IObjectRemovedEvent) |
---|
91 | def handle_product_removed(product, event): |
---|
92 | """If a product is deleted, we make sure that also referrers to |
---|
93 | customer contract objects are removed. |
---|
94 | """ |
---|
95 | prodid = product.product_id |
---|
96 | |
---|
97 | # Find all customer contracts that refer to given product... |
---|
98 | try: |
---|
99 | cat = getUtility(ICatalog, name='contracts_catalog') |
---|
100 | except ComponentLookupError: |
---|
101 | # catalog not available. This might happen during tests. |
---|
102 | return |
---|
103 | results = cat.searchResults(last_product_id=(prodid, prodid)) |
---|
104 | for contract in results: |
---|
105 | # Remove that referrer... |
---|
106 | contract.product_object = None |
---|
107 | notify(grok.ObjectModifiedEvent(contract)) |
---|
108 | contract.customer.__parent__.logger.info( |
---|
109 | 'ObjectRemovedEvent - %s - %s - removed: %s' % ( |
---|
110 | contract.customer.customer_id, |
---|
111 | contract.contract_id, |
---|
112 | prodid)) |
---|
113 | return |
---|
114 | |
---|