[5638] | 1 | ## |
---|
| 2 | ## interfaces.py |
---|
| 3 | ## Login : <uli@pu.smp.net> |
---|
| 4 | ## Started on Sun Jan 16 15:30:01 2011 Uli Fouquet |
---|
| 5 | ## $Id$ |
---|
[6076] | 6 | ## |
---|
[6087] | 7 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
[5638] | 8 | ## This program is free software; you can redistribute it and/or modify |
---|
| 9 | ## it under the terms of the GNU General Public License as published by |
---|
| 10 | ## the Free Software Foundation; either version 2 of the License, or |
---|
| 11 | ## (at your option) any later version. |
---|
[6076] | 12 | ## |
---|
[5638] | 13 | ## This program is distributed in the hope that it will be useful, |
---|
| 14 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | ## GNU General Public License for more details. |
---|
[6076] | 17 | ## |
---|
[5638] | 18 | ## You should have received a copy of the GNU General Public License |
---|
| 19 | ## along with this program; if not, write to the Free Software |
---|
| 20 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 21 | ## |
---|
[5676] | 22 | """Interfaces regarding student applicants and related components. |
---|
[5638] | 23 | """ |
---|
[5753] | 24 | import os |
---|
| 25 | import waeup.sirp.browser |
---|
[6256] | 26 | |
---|
[5866] | 27 | from grokcore.content.interfaces import IContainer |
---|
[6256] | 28 | |
---|
[5638] | 29 | from zope import schema |
---|
[6290] | 30 | from zope.interface import Interface, Attribute, provider |
---|
[6075] | 31 | from zope.component import getUtility, getUtilitiesFor |
---|
[5758] | 32 | from zope.pluggableauth.interfaces import IPrincipalInfo |
---|
| 33 | from zope.security.interfaces import IGroupClosureAwarePrincipal as IPrincipal |
---|
[6256] | 34 | from zc.sourcefactory.basic import BasicSourceFactory |
---|
[5753] | 35 | from waeup.sirp.image.schema import ImageFile |
---|
[5980] | 36 | from waeup.sirp.image.image import WAeUPImageFile |
---|
[6256] | 37 | from waeup.sirp.interfaces import IWAeUPObject |
---|
[6189] | 38 | from waeup.sirp.university.vocabularies import application_categories |
---|
[6256] | 39 | from waeup.sirp.applicants.vocabularies import ( |
---|
| 40 | year_range, application_types_vocab, application_pins_vocab, |
---|
| 41 | lgas_vocab, CertificateSource, AppCatCertificateSource, |
---|
| 42 | GenderSource, entry_session_vocab |
---|
| 43 | ) |
---|
[5638] | 44 | |
---|
[5753] | 45 | IMAGE_PATH = os.path.join( |
---|
| 46 | os.path.dirname(waeup.sirp.browser.__file__), |
---|
| 47 | 'static' |
---|
| 48 | ) |
---|
[6313] | 49 | DEFAULT_PASSPORT_IMAGE_MALE = open( |
---|
| 50 | os.path.join(IMAGE_PATH, 'placeholder_m.jpg')).read() |
---|
| 51 | DEFAULT_PASSPORT_IMAGE_FEMALE = open( |
---|
| 52 | os.path.join(IMAGE_PATH, 'placeholder_f.jpg')).read() |
---|
[5753] | 53 | |
---|
[6290] | 54 | @provider(schema.interfaces.IContextAwareDefaultFactory) |
---|
| 55 | def default_passport_image(context): |
---|
| 56 | """A default value factory for ImageFile fields. |
---|
| 57 | |
---|
| 58 | Returns some default image as WAeUPImageFile. We cannot set the |
---|
| 59 | default directly in ImageFile fields, as, if we want to set |
---|
| 60 | max_size or min_size as well, some utility lookups are needed |
---|
| 61 | which are not possible during startup. |
---|
| 62 | |
---|
| 63 | Developers which use IContextAwareDefaultFactories like this one |
---|
| 64 | always should make sure that the delivered default meets all |
---|
| 65 | constraints of the field that makes use of this default value |
---|
| 66 | provider. |
---|
| 67 | """ |
---|
[6313] | 68 | return WAeUPImageFile( |
---|
| 69 | 'placeholder_m.jpg', DEFAULT_PASSPORT_IMAGE_MALE) |
---|
[6290] | 70 | |
---|
[6069] | 71 | class ApplicantContainerProviderSource(BasicSourceFactory): |
---|
[6075] | 72 | """A source offering all available applicants container types. |
---|
| 73 | |
---|
| 74 | The values returned by this source are names of utilities that can |
---|
| 75 | create :class:`ApplicantContainer` instances. So, if you get a |
---|
| 76 | name like ``'myactype'`` from this source, then you can do: |
---|
| 77 | |
---|
| 78 | >>> from zope.component import getUtility |
---|
| 79 | >>> p = getUtility(IApplicantsContainerProvider, name=myactype) |
---|
| 80 | >>> my_applicants_container = p.factory() |
---|
| 81 | |
---|
| 82 | Or you can access class-attributes like |
---|
| 83 | |
---|
| 84 | >>> my_applicants_container.container_title |
---|
| 85 | 'Pretty' |
---|
[6076] | 86 | |
---|
[6069] | 87 | """ |
---|
| 88 | def getValues(self): |
---|
[6075] | 89 | """Returns a list of ``(<name>, <provider>)`` tuples. |
---|
[5753] | 90 | |
---|
[6075] | 91 | Here ``<name>`` is the name under which an |
---|
| 92 | :class:``ApplicantContainerProvider`` was registered as a |
---|
| 93 | utility and ``<provider>`` is the utility itself. |
---|
| 94 | """ |
---|
| 95 | return getUtilitiesFor(IApplicantsContainerProvider) |
---|
| 96 | |
---|
[6069] | 97 | def getToken(self, value): |
---|
[6075] | 98 | """Return the name of the ``(<name>, <provider>)`` tuple. |
---|
| 99 | """ |
---|
| 100 | return value[0] |
---|
| 101 | |
---|
[6069] | 102 | def getTitle(self, value): |
---|
[6075] | 103 | """Get a 'title - description' string for a container type. |
---|
| 104 | """ |
---|
| 105 | factory = value[1].factory |
---|
| 106 | return "%s - %s" % ( |
---|
| 107 | factory.container_title, factory.container_description) |
---|
[6069] | 108 | |
---|
[5753] | 109 | class IResultEntry(IWAeUPObject): |
---|
| 110 | subject = schema.TextLine( |
---|
| 111 | title = u'Subject', |
---|
| 112 | description = u'The subject', |
---|
| 113 | required=False, |
---|
| 114 | ) |
---|
| 115 | score = schema.TextLine( |
---|
| 116 | title = u'Score', |
---|
| 117 | description = u'The score', |
---|
| 118 | required=False, |
---|
| 119 | ) |
---|
| 120 | |
---|
[5866] | 121 | class IApplicantsRoot(IWAeUPObject, IContainer): |
---|
[5676] | 122 | """A container for university applicants containers. |
---|
[5645] | 123 | """ |
---|
[5866] | 124 | pass |
---|
[5638] | 125 | |
---|
[5676] | 126 | class IApplicantsContainer(IWAeUPObject): |
---|
| 127 | """An applicants container contains university applicants. |
---|
[5645] | 128 | |
---|
[5638] | 129 | """ |
---|
[6069] | 130 | |
---|
[6075] | 131 | container_title = Attribute( |
---|
| 132 | u'classattribute: title for type of container') |
---|
| 133 | container_description = Attribute( |
---|
| 134 | u'classattribute: description for type of container') |
---|
| 135 | |
---|
[6076] | 136 | |
---|
[6069] | 137 | code = schema.TextLine( |
---|
| 138 | title = u'Code', |
---|
[6087] | 139 | default = u'-', |
---|
[6069] | 140 | required = True, |
---|
| 141 | readonly = True, |
---|
[6076] | 142 | ) |
---|
[6096] | 143 | |
---|
[6087] | 144 | title = schema.TextLine( |
---|
| 145 | title = u'Title', |
---|
| 146 | required = True, |
---|
| 147 | default = u'-', |
---|
| 148 | readonly = True, |
---|
[6096] | 149 | ) |
---|
| 150 | |
---|
[6087] | 151 | prefix = schema.Choice( |
---|
| 152 | title = u'Application target', |
---|
| 153 | required = True, |
---|
| 154 | default = None, |
---|
| 155 | source = application_types_vocab, |
---|
| 156 | readonly = True, |
---|
| 157 | ) |
---|
[6076] | 158 | |
---|
[6087] | 159 | year = schema.Choice( |
---|
| 160 | title = u'Year of entrance', |
---|
| 161 | required = True, |
---|
| 162 | default = None, |
---|
[6158] | 163 | values = year_range(), |
---|
[6087] | 164 | readonly = True, |
---|
[6096] | 165 | ) |
---|
[6087] | 166 | |
---|
[6069] | 167 | provider = schema.Choice( |
---|
[6070] | 168 | title = u'Applicants container type', |
---|
[6069] | 169 | required = True, |
---|
| 170 | default = None, |
---|
| 171 | source = ApplicantContainerProviderSource(), |
---|
[6087] | 172 | readonly = True, |
---|
[6076] | 173 | ) |
---|
[6158] | 174 | |
---|
[6110] | 175 | ac_prefix = schema.Choice( |
---|
| 176 | title = u'Access code prefix', |
---|
| 177 | required = True, |
---|
| 178 | default = None, |
---|
[6111] | 179 | source = application_pins_vocab, |
---|
[6110] | 180 | ) |
---|
[6076] | 181 | |
---|
[6189] | 182 | application_category = schema.Choice( |
---|
| 183 | title = u'Category for the grouping of study courses', |
---|
| 184 | required = True, |
---|
| 185 | default = None, |
---|
| 186 | source = application_categories, |
---|
| 187 | ) |
---|
| 188 | |
---|
[5645] | 189 | description = schema.Text( |
---|
| 190 | title = u'Human readable description in reST format', |
---|
[5638] | 191 | required = False, |
---|
[6070] | 192 | default = u'No description yet.' |
---|
[5638] | 193 | ) |
---|
| 194 | |
---|
| 195 | startdate = schema.Date( |
---|
[5645] | 196 | title = u'Date when the application period starts', |
---|
[5638] | 197 | required = False, |
---|
| 198 | default = None, |
---|
| 199 | ) |
---|
| 200 | |
---|
| 201 | enddate = schema.Date( |
---|
[5645] | 202 | title = u'Date when the application period ends', |
---|
[5638] | 203 | required = False, |
---|
| 204 | default = None, |
---|
| 205 | ) |
---|
| 206 | |
---|
[5645] | 207 | strict_deadline = schema.Bool( |
---|
| 208 | title = u'Forbid additions after deadline (enddate)', |
---|
| 209 | required = True, |
---|
| 210 | default = True, |
---|
| 211 | ) |
---|
[5638] | 212 | |
---|
| 213 | def archive(id=None): |
---|
[5676] | 214 | """Create on-dist archive of applicants stored in this term. |
---|
[5638] | 215 | |
---|
[5676] | 216 | If id is `None`, all applicants are archived. |
---|
[5638] | 217 | |
---|
| 218 | If id contains a single id string, only the respective |
---|
[5676] | 219 | applicants are archived. |
---|
[5638] | 220 | |
---|
| 221 | If id contains a list of id strings all of the respective |
---|
[5676] | 222 | applicants types are saved to disk. |
---|
[5638] | 223 | """ |
---|
| 224 | |
---|
| 225 | def clear(id=None, archive=True): |
---|
[5676] | 226 | """Remove applicants of type given by 'id'. |
---|
[5638] | 227 | |
---|
[5676] | 228 | Optionally archive the applicants. |
---|
[6076] | 229 | |
---|
[5676] | 230 | If id is `None`, all applicants are archived. |
---|
[5638] | 231 | |
---|
| 232 | If id contains a single id string, only the respective |
---|
[5676] | 233 | applicants are archived. |
---|
[5638] | 234 | |
---|
| 235 | If id contains a list of id strings all of the respective |
---|
[5676] | 236 | applicant types are saved to disk. |
---|
[5638] | 237 | |
---|
| 238 | If `archive` is ``False`` none of the archive-handling is done |
---|
[5676] | 239 | and respective applicants are simply removed from the |
---|
[5638] | 240 | database. |
---|
| 241 | """ |
---|
[6073] | 242 | |
---|
[6069] | 243 | class IApplicantsContainerAdd(IApplicantsContainer): |
---|
| 244 | """An applicants container contains university applicants. |
---|
| 245 | """ |
---|
[6087] | 246 | prefix = schema.Choice( |
---|
| 247 | title = u'Application target', |
---|
[6069] | 248 | required = True, |
---|
[6087] | 249 | default = None, |
---|
| 250 | source = application_types_vocab, |
---|
[6069] | 251 | readonly = False, |
---|
[6076] | 252 | ) |
---|
[6073] | 253 | |
---|
[6087] | 254 | year = schema.Choice( |
---|
| 255 | title = u'Year of entrance', |
---|
| 256 | required = True, |
---|
| 257 | default = None, |
---|
[6158] | 258 | values = year_range(), |
---|
[6087] | 259 | readonly = False, |
---|
[6096] | 260 | ) |
---|
[6073] | 261 | |
---|
[6087] | 262 | provider = schema.Choice( |
---|
| 263 | title = u'Applicants container type', |
---|
| 264 | required = True, |
---|
| 265 | default = None, |
---|
| 266 | source = ApplicantContainerProviderSource(), |
---|
| 267 | readonly = False, |
---|
| 268 | ) |
---|
| 269 | |
---|
[6096] | 270 | IApplicantsContainerAdd[ |
---|
| 271 | 'prefix'].order = IApplicantsContainer['prefix'].order |
---|
| 272 | IApplicantsContainerAdd[ |
---|
| 273 | 'year'].order = IApplicantsContainer['year'].order |
---|
| 274 | IApplicantsContainerAdd[ |
---|
| 275 | 'provider'].order = IApplicantsContainer['provider'].order |
---|
[6087] | 276 | |
---|
[5753] | 277 | class IApplicantBaseData(IWAeUPObject): |
---|
| 278 | """The data for an applicant. |
---|
| 279 | |
---|
| 280 | This is a base interface with no field (except ``reg_no``) |
---|
| 281 | required. For use with importers, forms, etc., please use one of |
---|
| 282 | the derived interfaces below, which set more fields to required |
---|
| 283 | state, depending on use-case. |
---|
| 284 | """ |
---|
[6304] | 285 | messages = Attribute('Workflow transitions with date of transition.') |
---|
| 286 | |
---|
[5753] | 287 | reg_no = schema.TextLine( |
---|
| 288 | title = u'JAMB Registration Number', |
---|
[6195] | 289 | readonly = True, |
---|
[5753] | 290 | ) |
---|
| 291 | access_code = schema.TextLine( |
---|
| 292 | title = u'Access Code', |
---|
| 293 | required = False, |
---|
[6195] | 294 | readonly = True, |
---|
[5753] | 295 | ) |
---|
[6248] | 296 | course1 = schema.Choice( |
---|
[5753] | 297 | title = u'1st Choice Course of Study', |
---|
[6248] | 298 | source = AppCatCertificateSource(), |
---|
[5753] | 299 | required = False, |
---|
| 300 | ) |
---|
[6248] | 301 | course2 = schema.Choice( |
---|
[5753] | 302 | title = u'2nd Choice Course of Study', |
---|
[6248] | 303 | source = AppCatCertificateSource(), |
---|
[5753] | 304 | required = False, |
---|
| 305 | ) |
---|
| 306 | firstname = schema.TextLine( |
---|
| 307 | title = u'First Name', |
---|
| 308 | required = False, |
---|
| 309 | ) |
---|
| 310 | middlenames = schema.TextLine( |
---|
| 311 | title = u'Middle Names', |
---|
| 312 | required = False, |
---|
| 313 | ) |
---|
| 314 | lastname = schema.TextLine( |
---|
[6205] | 315 | title = u'Last Name (Surname)', |
---|
[5753] | 316 | required = False, |
---|
| 317 | ) |
---|
| 318 | date_of_birth = schema.Date( |
---|
| 319 | title = u'Date of Birth', |
---|
| 320 | required = False, |
---|
| 321 | ) |
---|
[6249] | 322 | lga = schema.Choice( |
---|
| 323 | source = lgas_vocab, |
---|
[6205] | 324 | title = u'State/LGA', |
---|
[6254] | 325 | default = 'foreigner', |
---|
| 326 | required = True, |
---|
[5753] | 327 | ) |
---|
| 328 | sex = schema.Choice( |
---|
| 329 | title = u'Sex', |
---|
| 330 | source = GenderSource(), |
---|
| 331 | default = u'm', |
---|
| 332 | required = False, |
---|
| 333 | ) |
---|
| 334 | email = schema.TextLine( |
---|
| 335 | title = u'Email', |
---|
| 336 | required = False, |
---|
| 337 | ) |
---|
| 338 | phone = schema.TextLine( |
---|
| 339 | title = u'Phone', |
---|
| 340 | required = False, |
---|
| 341 | ) |
---|
| 342 | passport = ImageFile( |
---|
| 343 | title = u'Passport Photograph', |
---|
[6290] | 344 | #default = DEFAULT_PASSPORT_IMAGE_MALE, |
---|
| 345 | defaultFactory = default_passport_image, |
---|
[5753] | 346 | required = True, |
---|
[6285] | 347 | max_size = 20480, |
---|
[5753] | 348 | ) |
---|
[6195] | 349 | confirm_passport = schema.Bool( |
---|
[6205] | 350 | title = u"Passport picture confirmed", |
---|
[6195] | 351 | default = False, |
---|
| 352 | required = True, |
---|
[5753] | 353 | ) |
---|
| 354 | # |
---|
[6195] | 355 | # Process Data |
---|
[5753] | 356 | # |
---|
| 357 | application_date = schema.Date( |
---|
| 358 | title = u'Application Date', |
---|
| 359 | required = False, |
---|
[6195] | 360 | readonly = True, |
---|
[5753] | 361 | ) |
---|
[6255] | 362 | screening_score = schema.Int( |
---|
[5753] | 363 | title = u'Screening Score', |
---|
| 364 | required = False, |
---|
| 365 | ) |
---|
| 366 | screening_venue = schema.TextLine( |
---|
| 367 | title = u'Screening Venue', |
---|
| 368 | required = False, |
---|
| 369 | ) |
---|
[6248] | 370 | course_admitted = schema.Choice( |
---|
[5753] | 371 | title = u'Admitted Course of Study', |
---|
[6248] | 372 | source = CertificateSource(), |
---|
[6254] | 373 | default = None, |
---|
[5753] | 374 | required = False, |
---|
| 375 | ) |
---|
[6255] | 376 | entry_session = schema.Choice( |
---|
| 377 | source = entry_session_vocab, |
---|
[5753] | 378 | title = u'Entry Session', |
---|
| 379 | required = False, |
---|
| 380 | ) |
---|
| 381 | notice = schema.Text( |
---|
| 382 | title = u'Notice', |
---|
| 383 | required = False, |
---|
| 384 | ) |
---|
| 385 | student_id = schema.TextLine( |
---|
| 386 | title = u'Student ID', |
---|
| 387 | required = False, |
---|
[6195] | 388 | readonly = True, |
---|
[5753] | 389 | ) |
---|
[6302] | 390 | locked = schema.Bool( |
---|
| 391 | title = u'Form locked', |
---|
| 392 | default = False, |
---|
| 393 | ) |
---|
[5753] | 394 | |
---|
| 395 | class IApplicant(IApplicantBaseData): |
---|
| 396 | """An applicant. |
---|
| 397 | |
---|
| 398 | This is basically the applicant base data. Here we repeat the |
---|
[6195] | 399 | fields from base data if we have to set the `required` attribute |
---|
| 400 | to True (which is the default). |
---|
[5753] | 401 | """ |
---|
| 402 | |
---|
[6195] | 403 | class IApplicantEdit(IApplicantBaseData): |
---|
| 404 | """An applicant. |
---|
[5753] | 405 | |
---|
[6195] | 406 | Here we can repeat the fields from base data and set the `required` and |
---|
| 407 | `readonly` attributes to True to further restrict the data access. We cannot |
---|
| 408 | omit fields. This has to be done in the respective form page. |
---|
| 409 | """ |
---|
[6301] | 410 | application_state = Attribute("The workflow state of an applicant object.") |
---|
| 411 | |
---|
[6255] | 412 | screening_score = schema.Int( |
---|
[5753] | 413 | title = u'Screening Score', |
---|
[6195] | 414 | required = False, |
---|
[5941] | 415 | readonly = True, |
---|
| 416 | ) |
---|
[6195] | 417 | screening_venue = schema.TextLine( |
---|
| 418 | title = u'Screening Venue', |
---|
[5753] | 419 | required = False, |
---|
| 420 | readonly = True, |
---|
| 421 | ) |
---|
[6301] | 422 | course_admitted = schema.Choice( |
---|
[6195] | 423 | title = u'Admitted Course of Study', |
---|
[6301] | 424 | source = CertificateSource(), |
---|
| 425 | default = None, |
---|
[5753] | 426 | required = False, |
---|
[6195] | 427 | readonly = True, |
---|
[5753] | 428 | ) |
---|
[6195] | 429 | entry_session = schema.TextLine( |
---|
[5753] | 430 | # XXX: should be choice |
---|
[6195] | 431 | title = u'Entry Session', |
---|
[5753] | 432 | required = False, |
---|
| 433 | readonly = True, |
---|
| 434 | ) |
---|
[6195] | 435 | notice = schema.Text( |
---|
| 436 | title = u'Notice', |
---|
[5753] | 437 | required = False, |
---|
| 438 | readonly = True, |
---|
| 439 | ) |
---|
[5952] | 440 | confirm_passport = schema.Bool( |
---|
[6195] | 441 | title = u"I confirm that the Passport Photograph uploaded on this form is a true picture of me.", |
---|
[5952] | 442 | default = False, |
---|
| 443 | required = True, |
---|
| 444 | ) |
---|
[5758] | 445 | |
---|
| 446 | class IApplicantPrincipalInfo(IPrincipalInfo): |
---|
| 447 | """Infos about principals that are applicants. |
---|
| 448 | """ |
---|
| 449 | access_code = Attribute("The Access Code the user purchased") |
---|
| 450 | |
---|
| 451 | class IApplicantPrincipal(IPrincipal): |
---|
| 452 | """A principal that is an applicant. |
---|
| 453 | |
---|
| 454 | This interface extends zope.security.interfaces.IPrincipal and |
---|
| 455 | requires also an `id` and other attributes defined there. |
---|
| 456 | """ |
---|
| 457 | access_code = schema.TextLine( |
---|
| 458 | title = u'Access Code', |
---|
| 459 | description = u'The access code purchased by the user.', |
---|
| 460 | required = True, |
---|
| 461 | readonly = True) |
---|
| 462 | |
---|
| 463 | class IApplicantsFormChallenger(Interface): |
---|
| 464 | """A challenger that uses a browser form to collect applicant |
---|
| 465 | credentials. |
---|
| 466 | """ |
---|
| 467 | loginpagename = schema.TextLine( |
---|
| 468 | title = u'Loginpagename', |
---|
| 469 | description = u"""Name of the login form used by challenger. |
---|
| 470 | |
---|
| 471 | The form must provide an ``access_code`` input field. |
---|
| 472 | """) |
---|
| 473 | |
---|
| 474 | accesscode_field = schema.TextLine( |
---|
| 475 | title = u'Access code field', |
---|
| 476 | description = u'''Field of the login page which is looked up for |
---|
| 477 | access_code''', |
---|
| 478 | default = u'access_code', |
---|
| 479 | ) |
---|
| 480 | |
---|
| 481 | |
---|
| 482 | class IApplicantSessionCredentials(Interface): |
---|
| 483 | """Interface for storing and accessing applicant credentials in a |
---|
| 484 | session. |
---|
| 485 | """ |
---|
| 486 | |
---|
| 487 | def __init__(access_code): |
---|
| 488 | """Create applicant session credentials.""" |
---|
| 489 | |
---|
| 490 | def getAccessCode(): |
---|
| 491 | """Return the access code.""" |
---|
| 492 | |
---|
| 493 | |
---|
[5846] | 494 | class IApplicantsContainerProvider(Interface): |
---|
[5820] | 495 | """A provider for applicants containers. |
---|
| 496 | |
---|
| 497 | Applicants container providers are meant to be looked up as |
---|
| 498 | utilities. This way we can find all applicant container types |
---|
| 499 | defined somewhere. |
---|
| 500 | |
---|
| 501 | Each applicants container provider registered as utility provides |
---|
| 502 | one container type and one should be able to call the `factory` |
---|
| 503 | attribute to create an instance of the requested container type. |
---|
| 504 | |
---|
| 505 | .. THE FOLLOWING SHOULD GO INTO SPHINX DOCS (and be tested) |
---|
[6076] | 506 | |
---|
[5820] | 507 | Samples |
---|
| 508 | ******* |
---|
[6076] | 509 | |
---|
[5820] | 510 | Given, you had an IApplicantsContainer implementation somewhere |
---|
| 511 | and you would like to make it findable on request, then you would |
---|
| 512 | normally create an appropriate provider utility like this:: |
---|
| 513 | |
---|
| 514 | import grok |
---|
[5846] | 515 | from waeup.sirp.applicants.interfaces import IApplicantsContainerProvider |
---|
[5820] | 516 | |
---|
| 517 | class MyContainerProvider(grok.GlobalUtility): |
---|
[5846] | 518 | grok.implements(IApplicantsContainerProvider) |
---|
[5820] | 519 | grok.name('MyContainerProvider') # Must be unique |
---|
| 520 | factory = MyContainer # A class implementing IApplicantsContainer |
---|
| 521 | # or derivations thereof. |
---|
| 522 | |
---|
| 523 | This utility would be registered on startup and could then be used |
---|
| 524 | like this: |
---|
| 525 | |
---|
| 526 | >>> from zope.component import getAllUtilitiesRegisteredFor |
---|
| 527 | >>> from waeup.sirp.applicants.interfaces import ( |
---|
[5846] | 528 | ... IApplicantsContainerProvider) |
---|
[5820] | 529 | >>> all_providers = getAllUtilitiesRegisteredFor( |
---|
[5846] | 530 | ... IApplicantsContainerProvider) |
---|
[5820] | 531 | >>> all_providers |
---|
| 532 | [<MyContainerProvider object at 0x...>] |
---|
| 533 | |
---|
| 534 | You could look up this specific provider by name: |
---|
| 535 | |
---|
| 536 | >>> from zope.component import getUtility |
---|
[5846] | 537 | >>> p = getUtility(IApplicantsContainerProvider, name='MyProvider') |
---|
[5820] | 538 | >>> p |
---|
| 539 | <MyContainerProvider object at 0x...> |
---|
[6076] | 540 | |
---|
[5820] | 541 | An applicants container would then be created like this: |
---|
| 542 | |
---|
| 543 | >>> provider = all_providers[0] |
---|
| 544 | >>> container = provider.factory() |
---|
| 545 | >>> container |
---|
| 546 | <MyContainer object at 0x...> |
---|
| 547 | |
---|
| 548 | """ |
---|
| 549 | factory = Attribute("A class that can create instances of the " |
---|
| 550 | "requested container type") |
---|