Subversion Repositories

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

Rev 647 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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