Subversion Repositories

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

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

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