Subversion Repositories

?revision_form?Rev ?revision_input??revision_submit??revision_endform?

Rev 647 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
439 kahless 1
from django.db.models import signals, get_apps, get_models
452 kahless 2
from django.conf import settings
391 kahless 3
 
439 kahless 4
 
452 kahless 5
class CategoryTypeBase(type):
6
    "Metaclass for all category types"
7
    def __new__(cls, name, bases, attrs):
8
        # If this isn't a subclass of Model, don't do anything special.
9
        new_class = super(CategoryTypeBase, cls).__new__(cls, name, bases, attrs)
10
        try:
11
            parents = [b for b in bases if issubclass(b, CategoryType)]
12
            if not parents:
13
                return new_class
14
        except NameError:
15
            # 'Model' isn't defined yet, meaning we're looking at Django's own
16
            # Model class, defined below.
17
            return new_class
18
 
19
        register_category_type(new_class)
20
        return new_class
21
 
22
 
391 kahless 23
class CategoryType(object):
24
    """
25
    Base class for all category types.
26
    """
27
 
452 kahless 28
    __metaclass__ = CategoryTypeBase
29
 
391 kahless 30
    # The name uniqueley identifies this category type.
31
    name = None
32
 
33
    # The label which will be displayed to the user.
34
    label = None
35
 
36
 
393 kahless 37
    def __init__(self, category):
38
        self.category = category
39
 
40
 
41
    def get_threadlist_template(self):
42
        """
43
        Allows subclasses to use a completely different template.
44
 
45
        The suggsted approach is to use a custom template which extends
46
        sphene/sphboard/listCategories.html and simply overwrite the
47
        required blocks.
48
        """
49
        return 'sphene/sphboard/listCategories.html'
50
 
51
    def get_new_thread_link_template(self):
52
        return 'sphene/sphboard/_new_thread_link.html'
53
 
54
    def get_show_thread_template(self):
55
        """
56
        The suggestd approach is to use a custom template which extends
57
        sphene/sphboard/showThread.html and only overwrite required blocks.
58
        """
59
        return 'sphene/sphboard/showThread.html'
60
 
61
    def get_post_form_class(self, replypost, editpost):
62
        """
647 bkroeze 63
        Should return a 'forms' form instance - a subclass of
393 kahless 64
        sphene.sphboard.views.PostForm
65
 
66
        Arguments:
67
        replypost: The post to which this form should reply to.
68
        editpost: The post which is edited (or None)
69
 
70
        To test if the user edits/creates a new reply to a thread
71
        (instead of creating a new thread) you can use the following code:
72
        if replypost is not None and \
73
                (editpost is None or editpost.thread is not None):
74
        """
75
        return None
76
 
77
    def save_post(self, newpost, data):
78
        """
79
        This is called right after a 'Post' was saved, and so allows
80
        this category type to store additional data in it's own entity.
81
 
82
        Arguments:
83
        post: The post to which the user replies to.
84
        newpost: the new Post object which was just saved/created.
85
        data: the cleaned_data of the form.
86
        """
87
        pass
88
 
417 kahless 89
    def get_absolute_url_for_post(self, post):
90
        """
91
        Allows implementors to hook into the get_absolute_url() method of a Post
92
        """
93
        return None
393 kahless 94
 
568 kahless 95
    def get_absolute_url_for_category(self):
96
        """
97
        should return the absolute url for self.category
98
        """
99
        return None
393 kahless 100
 
568 kahless 101
 
456 kahless 102
    def append_edit_message_to_post(self, post):
103
        """
104
        Determines if an 'edit message' should be appended to a post the user has just
105
        modified.
106
        """
107
        return True
108
 
608 kahless 109
    def is_displayed(self):
110
        """
111
        Return True if it should be displayed in overviews,
112
        False otherwise.
113
        """
114
        return True
115
 
696 kahless 116
    def is_separator(self):
117
        """
118
        defines if this category is just meant as a separator
119
        (ie. nobody can post new threads, etc.)
120
        this is only true for 'sphseparator' - and only existed
121
        because a {% ifequal category_type "sphseparator" %}
122
        destroyed my template inheritance:
123
        http://code.djangoproject.com/ticket/6510
124
        """
125
        return False
608 kahless 126
 
696 kahless 127
 
391 kahless 128
category_type_registry = { }
452 kahless 129
initialized = False
391 kahless 130
 
131
 
132
def register_category_type(category_type):
133
    """
134
    Call this method with an instance of a CategoryType
135
    subclass to add a custom category type.
136
    """
137
    category_type_registry[category_type.name] = category_type
138
 
139
def get_category_type(category_type_name):
140
    """
141
    Returns the CategoryType instance for the given type name,
142
    or None if it is not known.
143
    """
452 kahless 144
    __assure_initialized();
391 kahless 145
    return category_type_registry.get(category_type_name, None)
146
 
147
def get_category_type_list():
148
    """
149
    Returns a list of all known category types.
150
    """
452 kahless 151
    __assure_initialized();
391 kahless 152
    return category_type_registry.values()
439 kahless 153
 
452 kahless 154
def __assure_initialized():
155
    if not initialized: #category_type_registry:
156
        __init_category_types()
439 kahless 157
 
452 kahless 158
def __init_category_types():
159
    # for now use settings.INSTALLED_APPS
160
    # but in the end we should better use get_apps() ?
161
 
162
    for app_name in settings.INSTALLED_APPS:
163
        mod = __import__(app_name, {}, {}, ['categorytypes'])
164
        if hasattr(mod, 'categorytypes'):
165
            initialized = True
166
            #print "We found categorytypes in %s" % app_name
167
 
168
 
439 kahless 169
#    apps = get_apps()
170
#    for app in apps:
452 kahless 171
#        try:
172
#            app.categorytypes
173
#            print "Wanting to search in %s - %s" % (type(app),str(app))
174
#        except AttributeError:
175
#            pass
176
 
439 kahless 177
#        mod = __import__(app