| | 542 | |
| | 543 | def init_pref_form(self, pref_form): |
| | 544 | """ |
| | 545 | Set self.pref_data_string from the given newforms form and his initial |
| | 546 | values. |
| | 547 | """ |
| | 548 | init_dict = get_init_dict(pref_form) |
| | 549 | preference_cache[self.plugin_name] = init_dict |
| | 550 | self.set_pref_data_string(init_dict) |
| | 551 | |
| | 552 | def set_pref_data_string(self, data_dict): |
| | 553 | """ |
| | 554 | set the dict via pformat |
| | 555 | """ |
| | 556 | preference_cache[self.plugin_name] = data_dict |
| | 557 | self.pref_data_string = pformat(data_dict) |
| | 558 | |
| | 559 | def get_preferences(self): |
| | 560 | """ |
| | 561 | evaluate the pformat string into a dict and return it. |
| | 562 | """ |
| | 563 | data_dict = data_eval(self.pref_data_string) |
| | 564 | preference_cache[self.plugin_name] = data_dict |
| | 565 | return data_dict |
| | 566 | |
| | 567 | def get_pref_form(self, debug): |
| | 568 | """ |
| | 569 | Get the 'PreferencesForm' newform class from the plugin modul, insert |
| | 570 | initial information into the help text and return the form. |
| | 571 | """ |
| | 572 | plugin_module = get_plugin_module( |
| | 573 | self.package_name, self.plugin_name, debug |
| | 574 | ) |
| | 575 | form = getattr(plugin_module, "PreferencesForm") |
| | 576 | setup_help_text(form) |
| | 577 | return form |
| 546 | | |
| 547 | | #______________________________________________________________________________ |
| 548 | | # Preference |
| 549 | | |
| 550 | | class Preference(models.Model): |
| 551 | | """ |
| 552 | | Stores preferences |
| 553 | | |
| 554 | | Any pickleable Python object can be stored. |
| 555 | | |
| 556 | | Use a small cache, so the pickle.loads() method would only be used on the |
| 557 | | first access. |
| 558 | | |
| 559 | | Note: |
| 560 | | - This model has no Admin class. Because it makes no sense to edit |
| 561 | | pickled data strings in the django admin panel ;) |
| 562 | | """ |
| 563 | | plugin = models.ForeignKey("Plugin", help_text="The associated plugin") |
| 564 | | repr_string = models.TextField( |
| 565 | | help_text="printable representation of the newform data dictionary" |
| 566 | | ) |
| 567 | | |
| 568 | | lastupdatetime = models.DateTimeField(auto_now=True, editable=False) |
| 569 | | lastupdateby = models.ForeignKey( |
| 570 | | User, null=True, blank=True, editable=False, |
| 571 | | ) |
| 572 | | |
| 573 | | #__________________________________________________________________________ |
| 574 | | |
| 575 | | def __unicode__(self): |
| 576 | | return u"Preference object for '%s'" % self.plugin |
| 577 | | |
| 578 | | class Admin: |
| 579 | | pass |
| 580 | | |
| 581 | | class Meta: |
| 582 | | # Use a new table |
| 583 | | db_table = u'PyLucid_preference2' |
| 584 | | |
| 585 | | |
| 586 | | class PreferenceOld(models.Model): |
| 587 | | """ |
| 588 | | Stores preferences for the PyLucid system and all Plugins. |
| 589 | | |
| 590 | | Any pickleable Python object can be stored. |
| 591 | | Use a small cache, so the pickle.loads() method would only be used on the |
| 592 | | first get_value access. The default_value used no cache. |
| 593 | | |
| 594 | | Usage: |
| 595 | | --------------------------------------------------------------------------- |
| 596 | | Preference(name = "value name", value = my_python_data_object).save() |
| 597 | | |
| 598 | | p = Preference.objects.get(name = "value name") |
| 599 | | print p.value |
| 600 | | --------------------------------------------------------------------------- |
| 601 | | |
| 602 | | Note: |
| 603 | | - This model has no Admin class. Because it makes no sense to edit |
| 604 | | pickled data strings in the django admin panel ;) |
| 605 | | - value and default_value are properties |
| 606 | | """ |
| 607 | | __cache = {} # Cache the pickleable Python object |
| 608 | | |
| 609 | | #__________________________________________________________________________ |
| 610 | | |
| 611 | | def _get_cache_key(self): |
| 612 | | """ returns plugin name + preferences name as a string """ |
| 613 | | return "%s-%s" % (self.plugin, self.name) |
| 614 | | |
| 615 | | #__________________________________________________________________________ |
| 616 | | # get and set methods for the value property of the entry |
| 617 | | |
| 618 | | def __get_value(self): |
| 619 | | """ |
| 620 | | returns the pickleable Python object back. Use the cache first. |
| 621 | | """ |
| 622 | | cache_key = self._get_cache_key() |
| 623 | | |
| 624 | | if cache_key in self.__cache: |
| 625 | | value = self.__cache[cache_key] |
| 626 | | else: |
| 627 | | self._value = str(self._value) |
| 628 | | value = pickle.loads(self._value) |
| 629 | | self.__cache[cache_key] = value |
| 630 | | return value |
| 631 | | |
| 632 | | def __set_value(self, value): |
| 633 | | """ |
| 634 | | Save a the pickleable Python object into the database. Remember the |
| 635 | | source object in the cache |
| 636 | | """ |
| 637 | | cache_key = self._get_cache_key() |
| 638 | | |
| 639 | | self.__cache[cache_key] = value |
| 640 | | self._value = pickle.dumps(value) |
| 641 | | |
| 642 | | #__________________________________________________________________________ |
| 643 | | # get and set methods for the default value property of the entry |
| 644 | | |
| 645 | | def __get_default_value(self): |
| 646 | | """ |
| 647 | | returns the default value back |
| 648 | | """ |
| 649 | | self._default_value = str(self._default_value) |
| 650 | | default_value = pickle.loads(self._default_value) |
| 651 | | return default_value |
| 652 | | |
| 653 | | def __set_default_value(self, default_value): |
| 654 | | """ |
| 655 | | save a default value |
| 656 | | """ |
| 657 | | self._default_value = pickle.dumps(default_value) |
| 658 | | |
| 659 | | #__________________________________________________________________________ |
| 660 | | # The attributes |
| 661 | | |
| 662 | | plugin = models.ForeignKey( |
| 663 | | "Plugin", help_text="The associated plugin", |
| 664 | | null=True, blank=True, editable=False |
| 665 | | ) |
| 666 | | name = models.CharField( |
| 667 | | max_length=150, db_index=True, editable=False, |
| 668 | | help_text="Name of the preferences entry", |
| 669 | | ) |
| 670 | | description = models.TextField(editable=False) |
| 671 | | |
| 672 | | _value = models.TextField( |
| 673 | | editable=False, help_text="Pickled Python object" |
| 674 | | ) |
| 675 | | value = property(__get_value, __set_value) |
| 676 | | |
| 677 | | _default_value = models.TextField( |
| 678 | | editable=False, help_text="Pickled Python object" |
| 679 | | ) |
| 680 | | default_value = property(__get_default_value, __set_default_value) |
| 681 | | |
| 682 | | field_type = models.CharField(max_length=150, editable=False, |
| 683 | | help_text="The data type for this entry (For building a html form)." |
| 684 | | ) |
| 685 | | |
| 686 | | lastupdatetime = models.DateTimeField(auto_now=True) |
| 687 | | lastupdateby = models.ForeignKey( |
| 688 | | User, null=True, blank=True, editable=False, |
| 689 | | ) |
| 690 | | |
| 691 | | #__________________________________________________________________________ |
| 692 | | |
| 693 | | def save(self): |
| 694 | | """ |
| 695 | | Save a new or update a preference entry |
| 696 | | """ |
| 697 | | if self.id == None: |
| 698 | | # A new preferences should be saved. |
| 699 | | if self._default_value == "": |
| 700 | | # No default value given in the __init__ -> Use current value |
| 701 | | self._default_value = self._value |
| 702 | | |
| 703 | | super(Preference, self).save() # Call the "real" save() method |
| 704 | | |
| 705 | | def __unicode__(self): |
| 706 | | # <Preference: Preference object> |
| 707 | | return "%s '%s'" % (self.plugin, self.name) |
| 708 | | |
| 709 | | class Meta: |
| 710 | | # Use the old table |
| 711 | | db_table = u'PyLucid_preference' |