Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 91 lines (68 sloc) 3.458 kb
e45537c Peter Cicman Several changes - works well with CMS_MODERATOR=False
pcicman authored
1 #from copy import deepcopy
bcf4d75 Peter Cicman Lets start
pcicman authored
2 from django.db.models.base import ModelBase
e45537c Peter Cicman Several changes - works well with CMS_MODERATOR=False
pcicman authored
3 #from django.db.models.loading import get_model
4 #from django.db.models.fields.related import RelatedField
3c695c2 Peter Cicman added QuerySet object for Page model, several changes to publisher:
pcicman authored
5 from publisher.manager import PublisherManager
6 from publisher.options import PublisherOptions
7 from publisher.mptt_support import install_mptt, finish_mptt
bcf4d75 Peter Cicman Lets start
pcicman authored
8
9 def install_publisher():
10 """Check if publisher isn't installed already, install it otherwise. But
11 install it only once.
12 """
e45537c Peter Cicman Several changes - works well with CMS_MODERATOR=False
pcicman authored
13
14 if hasattr(ModelBase, '_publisher_installed'):
15 # already installed, go away
16 return
17
bcf4d75 Peter Cicman Lets start
pcicman authored
18 _old_new = ModelBase.__new__
19 def publisher_modelbase_new(cls, name, bases, attrs):
3c695c2 Peter Cicman added QuerySet object for Page model, several changes to publisher:
pcicman authored
20 from publisher.models import Publisher
21
22
bcf4d75 Peter Cicman Lets start
pcicman authored
23 """Override modelbase new method, check if Publisher attribute is
24 subclass of Publisher.
25 """
26 # in case of model inheritance
27 base_under_publisher = bool(filter(lambda b: issubclass(b, Publisher), bases))
28
29 is_publisher_model = Publisher in bases or base_under_publisher
30
3c695c2 Peter Cicman added QuerySet object for Page model, several changes to publisher:
pcicman authored
31 if is_publisher_model:
e45537c Peter Cicman Several changes - works well with CMS_MODERATOR=False
pcicman authored
32 if ('objects' in attrs) and (not isinstance(attrs['objects'], PublisherManager)):
33 raise ValueError, ("Model %s extends Publisher, " +
34 "so its 'objects' manager must be " +
35 "a subclass of publisher.PublisherManager") % (name,)
bcf4d75 Peter Cicman Lets start
pcicman authored
36
eb89e76 Peter Cicman Default manager on every publisher model now links to PublisherManager. ...
pcicman authored
37 if not 'objects' in attrs:
38 attrs['objects'] = PublisherManager()
39
e45537c Peter Cicman Several changes - works well with CMS_MODERATOR=False
pcicman authored
40 attrs['_is_publisher_model'] = lambda self: True
3c695c2 Peter Cicman added QuerySet object for Page model, several changes to publisher:
pcicman authored
41
42 # build meta object
43 publisher_meta = attrs.pop('PublisherMeta', None)
44 attrs['_publisher_meta'] = PublisherOptions(name, bases, publisher_meta)
45
e45537c Peter Cicman Several changes - works well with CMS_MODERATOR=False
pcicman authored
46
47 # take care of mptt, if required
48 attrs = install_mptt(cls, name, bases, attrs)
49
3c695c2 Peter Cicman added QuerySet object for Page model, several changes to publisher:
pcicman authored
50 new_class = _old_new(cls, name, bases, attrs)
e45537c Peter Cicman Several changes - works well with CMS_MODERATOR=False
pcicman authored
51 finish_mptt(new_class)
52 return new_class
53
54 '''
bcf4d75 Peter Cicman Lets start
pcicman authored
55 """
56 if Publisher in bases or base_under_publisher:
57 # copy attrs, because ModelBase affects them
58 public_attrs = deepcopy(attrs)
59
60 attrs['_is_publisher_model'] = lambda self: True
61
62 # create proxy - accessor for public model
63 class PublicModelProxy(object):
64 def __get__(self, name, cls):
65 public_name = PublisherManager.PUBLISHER_MODEL_NAME % cls._meta.object_name
66 model = get_model(cls._meta.app_label, public_name.lower())
67 return model
68
69 attrs['PublicModel'] = PublicModelProxy()
70 """
71
e45537c Peter Cicman Several changes - works well with CMS_MODERATOR=False
pcicman authored
72
bcf4d75 Peter Cicman Lets start
pcicman authored
73
74 if is_publisher_model:
75 # register it for future use..., @see publisher.post
76 if not base_under_publisher:
77 public_bases = list(bases)
78 public_bases.remove(Publisher)
79 if not public_bases:
80 public_bases = (models.Model,)
81 else:
82 public_bases = bases
83 publisher_manager.register(cls, name, tuple(public_bases), public_attrs, new_class)
84
85 finish_mptt(new_class)
e45537c Peter Cicman Several changes - works well with CMS_MODERATOR=False
pcicman authored
86 '''
bcf4d75 Peter Cicman Lets start
pcicman authored
87 return new_class
88
89 ModelBase.__new__ = staticmethod(publisher_modelbase_new)
90
3c695c2 Peter Cicman added QuerySet object for Page model, several changes to publisher:
pcicman authored
91 ModelBase._publisher_installed = True
Something went wrong with that request. Please try again.