84 .. sourcecode:: python |
84 .. sourcecode:: python |
85 |
85 |
86 from yams.buildobjs import EntityType, String, SubjectRelation, Date |
86 from yams.buildobjs import EntityType, String, SubjectRelation, Date |
87 |
87 |
88 class Blog(EntityType): |
88 class Blog(EntityType): |
89 title = String(maxsize=50,._cwuired=True) |
89 title = String(maxsize=50, required=True) |
90 description = String() |
90 description = String() |
91 |
91 |
92 class BlogEntry(EntityType): |
92 class BlogEntry(EntityType): |
93 title = String._cwuired=True, fulltextindexed=True, maxsize=256) |
93 title = String(required=True, fulltextindexed=True, maxsize=256) |
94 publish_date = Date(default='TODAY') |
94 publish_date = Date(default='TODAY') |
95 content = String._cwuired=True, fulltextindexed=True) |
95 content = String(required=True, fulltextindexed=True) |
96 entry_of = SubjectRelation('Blog', cardinality='?*') |
96 entry_of = SubjectRelation('Blog', cardinality='?*') |
97 |
97 |
98 The first step is the import of the EntityType (generic class for entity and |
98 The first step is the import of the EntityType (generic class for entity and |
99 attributes that will be used in both Blog and BlogEntry entities. |
99 attributes that will be used in both Blog and BlogEntry entities. |
100 |
100 |
101 A Blog has a title and a description. The title is a string that is |
101 A Blog has a title and a description. The title is a string that is |
102 required and must be less than 50 characters. The |
102 required and must be less than 50 characters. The |
103 description is a string that is not constrained. |
103 description is a string that is not constrained. |
104 |
104 |
105 A BlogEntry has a title, a publish_date and a content. The title is a |
105 A BlogEntry has a title, a publish_date and a content. The title is a |
106 string that is._cwuired and must be less than 100 characters. The |
106 string that is required and must be less than 100 characters. The |
107 publish_date is a Date with a default value of TODAY, meaning that |
107 publish_date is a Date with a default value of TODAY, meaning that |
108 when a BlogEntry is created, its publish_date will be the current day |
108 when a BlogEntry is created, its publish_date will be the current day |
109 unless it is modified. The content is a string that will be indexed in |
109 unless it is modified. The content is a string that will be indexed in |
110 the database full-text index and has no constraint. |
110 the database full-text index and has no constraint. |
111 |
111 |
319 self.w(u'<p>published on %s</p>' % |
319 self.w(u'<p>published on %s</p>' % |
320 entity.publish_date.strftime('%Y-%m-%d')) |
320 entity.publish_date.strftime('%Y-%m-%d')) |
321 super(BlogEntryPrimaryView, self).render_entity_attributes(entity) |
321 super(BlogEntryPrimaryView, self).render_entity_attributes(entity) |
322 |
322 |
323 .. note:: |
323 .. note:: |
324 When a view is modified, it is not._cwuired to restart the instance |
324 When a view is modified, it is not required to restart the instance |
325 server. Save the Python file and reload the page in your web browser |
325 server. Save the Python file and reload the page in your web browser |
326 to view the changes. |
326 to view the changes. |
327 |
327 |
328 You can now see that the publication date has a prefix. |
328 You can now see that the publication date has a prefix. |
329 |
329 |
365 if 'CW' in self.title: |
365 if 'CW' in self.title: |
366 return True |
366 return True |
367 else: |
367 else: |
368 return False |
368 return False |
369 |
369 |
370 Customizing an entity._cwuires that your entity: |
370 Customizing an entity requires that your entity: |
371 - inherits from ``cubicweb.entities`` or any subclass |
371 - inherits from ``cubicweb.entities`` or any subclass |
372 - defines a ``__regid__`` linked to the corresponding data type of your schema |
372 - defines a ``__regid__`` linked to the corresponding data type of your schema |
373 - implements the base class by explicitly using ``__implements__``. |
373 - implements the base class by explicitly using ``__implements__``. |
374 |
374 |
375 We implemented here a function ``display_cw_logo`` which tests if the blog entry title contains 'CW'. |
375 We implemented here a function ``display_cw_logo`` which tests if the blog entry title contains 'CW'. |
395 -------------------------------------------------- |
395 -------------------------------------------------- |
396 |
396 |
397 While developping your cube, you may want to update your data model. Let's say you |
397 While developping your cube, you may want to update your data model. Let's say you |
398 want to add a ``category`` attribute in the ``Blog`` data type. This is called a migration. |
398 want to add a ``category`` attribute in the ``Blog`` data type. This is called a migration. |
399 |
399 |
400 The._cwuired steps are: |
400 The required steps are: |
401 1. modify the file ``schema.py``. The ``Blog`` class looks now like this: |
401 1. modify the file ``schema.py``. The ``Blog`` class looks now like this: |
402 |
402 |
403 .. sourcecode:: python |
403 .. sourcecode:: python |
404 |
404 |
405 class Blog(EntityType): |
405 class Blog(EntityType): |
406 title = String(maxsize=50,._cwuired=True) |
406 title = String(maxsize=50, required=True) |
407 description = String() |
407 description = String() |
408 category = String._cwuired=True, vocabulary=(_('Professional'), _('Personal')), default='Personal') |
408 category = String(required=True, vocabulary=(_('Professional'), _('Personal')), default='Personal') |
409 |
409 |
410 2. stop your ``blogdemo`` instance |
410 2. stop your ``blogdemo`` instance |
411 |
411 |
412 3. start the cubicweb shell for your instance by running the following command: |
412 3. start the cubicweb shell for your instance by running the following command: |
413 |
413 |