Subversion Repositories

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

Rev 568 | Rev 647 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 568 Rev 608
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 'newforms' form instance - a subclass of
63
        Should return a 'newforms' 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):
-
 
110
        """
-
 
111
        Return True if it should be displayed in overviews,
-
 
112
        False otherwise.
-
 
113
        """
-
 
114
        return True
-
 
115
 
-
 
116
 
109
category_type_registry = { }
117
category_type_registry = { }
110
initialized = False
118
initialized = False
111
 
119
 
112
 
120
 
113
def register_category_type(category_type):
121
def register_category_type(category_type):
114
    """
122
    """
115
    Call this method with an instance of a CategoryType
123
    Call this method with an instance of a CategoryType
116
    subclass to add a custom category type.
124
    subclass to add a custom category type.
117
    """
125
    """
118
    category_type_registry[category_type.name] = category_type
126
    category_type_registry[category_type.name] = category_type
119
 
127
 
120
def get_category_type(category_type_name):
128
def get_category_type(category_type_name):
121
    """
129
    """
122
    Returns the CategoryType instance for the given type name,
130
    Returns the CategoryType instance for the given type name,
123
    or None if it is not known.
131
    or None if it is not known.
124
    """
132
    """
125
    __assure_initialized();
133
    __assure_initialized();
126
    return category_type_registry.get(category_type_name, None)
134
    return category_type_registry.get(category_type_name, None)
127
 
135
 
128
def get_category_type_list():
136
def get_category_type_list():
129
    """
137
    """
130
    Returns a list of all known category types.
138
    Returns a list of all known category types.
131
    """
139
    """
132
    __assure_initialized();
140
    __assure_initialized();
133
    return category_type_registry.values()
141
    return category_type_registry.values()
134
 
142
 
135
def __assure_initialized():
143
def __assure_initialized():
136
    if not initialized: #category_type_registry:
144
    if not initialized: #category_type_registry:
137
        __init_category_types()
145
        __init_category_types()
138
 
146
 
139
def __init_category_types():
147
def __init_category_types():
140
    # for now use settings.INSTALLED_APPS
148
    # for now use settings.INSTALLED_APPS
141
    # but in the end we should better use get_apps() ?
149
    # but in the end we should better use get_apps() ?
142
 
150
 
143
    for app_name in settings.INSTALLED_APPS:
151
    for app_name in settings.INSTALLED_APPS:
144
        mod = __import__(app_name, {}, {}, ['categorytypes'])
152
        mod = __import__(app_name, {}, {}, ['categorytypes'])
145
        if hasattr(mod, 'categorytypes'):
153
        if hasattr(mod, 'categorytypes'):
146
            initialized = True
154
            initialized = True
147
            #print "We found categorytypes in %s" % app_name
155
            #print "We found categorytypes in %s" % app_name
148
 
156
 
149
 
157
 
150
#    apps = get_apps()
158
#    apps = get_apps()
151
#    for app in apps:
159
#    for app in apps:
152
#        try:
160
#        try:
153
#            app.categorytypes
161
#            app.categorytypes
154
#            print "Wanting to search in %s - %s" % (type(app),str(app))
162
#            print "Wanting to search in %s - %s" % (type(app),str(app))
155
#        except AttributeError:
163
#        except AttributeError:
156
#            pass
164
#            pass
157
 
165
 
158
#        mod = __import__(app
166
#        mod = __import__(app
159
 
167
 
160
 
168
 
161
Generated by GNU Enscript 1.6.5.2.
169
Generated by GNU Enscript 1.6.5.2.
162
 
170
 
163
 
171