source: products/ploneorg.kudobounty/trunk/ploneorg/kudobounty/content/bountyprogramsubmission.py @ 3201

Last change on this file since 3201 was 3201, checked in by mylan, 13 years ago

Move title calculation into separate function, use in in buntysubscription content type and in content type auto cration view

  • Property svn:eol-style set to native
File size: 5.7 KB
Line 
1"""Definition of the Bounty Program Submission content type
2"""
3
4from zope.interface import implements
5
6from Products.Archetypes import atapi
7from Products.ATContentTypes.content import base
8from Products.ATContentTypes.content import schemata
9from Products.ATContentTypes.configuration import zconf
10from Products.validation import V_REQUIRED
11
12# -*- Message Factory Imported Here -*-
13from ploneorg.kudobounty import kudobountyMessageFactory as _
14
15from ploneorg.kudobounty.interfaces import IBountyProgramSubmission
16from ploneorg.kudobounty.config import PROJECTNAME
17
18def calcTitle(first, last, organization):
19    """
20    Calculate title with space ceparated first and last name
21    and add organization name by comma, if it exist.
22    """
23    fst, lst, org = map(str.strip,[first, last, organization])
24    fst_lst = ' '.join(filter(None, [fst, lst]))
25    comma = org and (fst or lst) and ', ' or ''
26    return fst_lst + comma + org
27
28BountyProgramSubmissionSchema = schemata.ATContentTypeSchema.copy() + atapi.Schema((
29
30    # -*- Your Archetypes field definitions here ... -*-
31    atapi.ComputedField(
32        name='title',
33        required=1,
34        searchable=1,
35        default='',
36        accessor='Title',
37        storage=atapi.AnnotationStorage(),
38        widget=atapi.ComputedWidget(
39            label=_(u'Name'),
40        ),
41        expression="context.getCalcTitle()"
42    ),
43
44    atapi.ImageField(
45        'image',
46        languageIndependent=True,
47        storage=atapi.AnnotationStorage(),
48        swallowResizeExceptions = zconf.swallowImageResizeExceptions.enable,
49        pil_quality = zconf.pil_config.quality,
50        pil_resize_algo = zconf.pil_config.resize_algo,
51        max_size = zconf.ATImage.max_image_dimension,
52        sizes= {'bounty': (150,100),},
53        widget=atapi.ImageWidget(
54            label=_(u"Image"),
55            description=_(u"Image or Logo"),
56        ),
57        required=True,
58        validators = (('isNonEmptyFile', V_REQUIRED),
59                      ('checkImageMaxSize', V_REQUIRED)),
60    ),
61
62    atapi.StringField(
63        'remoteUrl', # for use getRemoteUrl metadata from catalog
64        storage=atapi.AnnotationStorage(),
65        widget=atapi.StringWidget(
66            label=_(u"URL"),
67            maxlength = '511',
68        ),
69        required=True,
70        validators=('isURL'),
71        default = "http://",
72    ),
73
74    atapi.StringField(
75        'description',
76        accessor='Description',
77        storage=atapi.AnnotationStorage(),
78        widget=atapi.StringWidget(
79            label=_(u"alt text"),
80        ),
81    ),
82
83    atapi.StringField(
84        'firstName',
85        storage=atapi.AnnotationStorage(),
86        widget=atapi.StringWidget(
87            label=_(u"First name"),
88            description=_(u"Field description"),
89        ),
90    ),
91
92    atapi.StringField(
93        'lastName',
94        storage=atapi.AnnotationStorage(),
95        widget=atapi.StringWidget(
96            label=_(u"Last name"),
97            description=_(u"Field description"),
98        ),
99    ),
100
101    atapi.StringField(
102        'organization',
103        storage=atapi.AnnotationStorage(),
104        widget=atapi.StringWidget(
105            label=_(u"Organization name"),
106            description=_(u"Field description"),
107        ),
108    ),
109
110    atapi.StringField(
111        'email',
112        storage=atapi.AnnotationStorage(),
113        widget=atapi.StringWidget(
114            label=_(u"Email address"),
115        ),
116        validators=('isEmail'),
117    ),
118
119    atapi.TextField(
120        'mission',
121        storage=atapi.AnnotationStorage(),
122        default_content_type = 'text/plain',
123        allowable_content_types = ('text/plain',),
124        widget=atapi.TextAreaWidget(
125            label=_(u"Bounty mission"),
126            description=_(u"Bounty mission trac ticket #"),
127        ),
128        required=True,
129    ),
130
131))
132
133# Set storage on fields copied from ATContentTypeSchema, making sure
134# they work well with the python bridge properties.
135
136BountyProgramSubmissionSchema = schemata.finalizeATCTSchema(
137    BountyProgramSubmissionSchema, moveDiscussion=False)
138BountyProgramSubmissionSchema.moveField('description', after='remoteUrl')
139
140
141class BountyProgramSubmission(base.ATCTContent):
142    """Information for Bounty Program Submission"""
143    implements(IBountyProgramSubmission)
144
145    meta_type = "BountyProgramSubmission"
146    schema = BountyProgramSubmissionSchema
147
148    title = atapi.ATFieldProperty('title')
149    description = atapi.ATFieldProperty('description')
150
151    # -*- Your ATSchema to Python Property Bridges Here ... -*-
152    image = atapi.ATFieldProperty('image')
153    remoteUrl = atapi.ATFieldProperty('remoteUrl')
154    firstName = atapi.ATFieldProperty('firstName')
155    lastName = atapi.ATFieldProperty('lastName')
156    organization = atapi.ATFieldProperty('organization')
157    email = atapi.ATFieldProperty('email')
158    mission = atapi.ATFieldProperty('mission')
159
160    def __bobo_traverse__(self, REQUEST, name):
161        """Transparent access to image scales
162        """
163        if name.startswith('image'):
164            field = self.getField('image')
165            image = None
166            if name == 'image':
167                image = field.getScale(self)
168            else:
169                scalename = name[len('image_'):]
170                if scalename in field.getAvailableSizes(self):
171                    image = field.getScale(self, scale=scalename)
172            if image is not None and not isinstance(image, basestring):
173                # image might be None or '' for empty images
174                return image
175
176        return base.ATCTContent.__bobo_traverse__(self, REQUEST, name)
177
178    def getCalcTitle(self):
179        return calcTitle(self.getFirstName(), self.getLastName(),
180                         self.getOrganization())
181
182   
183atapi.registerType(BountyProgramSubmission, PROJECTNAME)
Note: See TracBrowser for help on using the repository browser.