| 47 | | |
| 48 | | class ParentChoiceField(forms.IntegerField): |
| 49 | | def clean(self, parent_id): |
| 50 | | """ |
| 51 | | returns the parent page instance. |
| 52 | | Note: |
| 53 | | In PyLucid.models.Page.save() it would be checkt if the selected |
| 54 | | parent page is logical valid. Here we check only, if the page with |
| 55 | | the given ID exists. |
| 56 | | """ |
| 57 | | # let convert the string into a integer: |
| 58 | | parent_id = super(ParentChoiceField, self).clean(parent_id) |
| 59 | | assert isinstance(parent_id, int) |
| 60 | | |
| 61 | | if parent_id == 0: |
| 62 | | # assigned to the tree root. |
| 63 | | return None |
| 64 | | |
| 65 | | try: |
| 66 | | #parent_id = 999999999 # Not exists test |
| 67 | | page = Page.objects.get(id=parent_id) |
| 68 | | return page |
| 69 | | except Exception, msg: |
| 70 | | raise ValidationError(_(u"Wrong parent POST data: %s" % msg)) |
| 71 | | |
| 72 | | |
| 73 | | def get_parent_choices(): |
| 74 | | """ |
| 75 | | generate a verbose page name tree for the parent choice field. |
| 76 | | """ |
| 77 | | page_list = flat_tree_list() |
| 78 | | choices = [(0, "---[root]---")] |
| 79 | | for page in page_list: |
| 80 | | choices.append((page["id"], page["level_name"])) |
| 81 | | return choices |
| | 47 | from PyLucid.db.page import PageChoiceField, get_page_choices |
| | 48 | |