# HG changeset patch
# User verrierj
# Date 1327671579 -3600
# Node ID b1dd78b597507c1fdb29b37fc22dc027a55bc58a
# Parent 8004f0d1a1bf8378b5a733a4a9dc2e25e28496a4
add all polemic attributes and polemic attribute rates on contents
diff -r 8004f0d1a1bf -r b1dd78b59750 src/ldt/ldt/ldt_utils/models.py
--- a/src/ldt/ldt/ldt_utils/models.py Fri Jan 27 11:26:48 2012 +0100
+++ b/src/ldt/ldt/ldt_utils/models.py Fri Jan 27 14:39:39 2012 +0100
@@ -141,6 +141,13 @@
permissions = (
('view_content', 'Can view content'),
)
+
+ def __init__(self, *args, **kwargs):
+
+ super(Content, self).__init__(*args, **kwargs)
+
+ if not hasattr(Content, 'pol_positive'):
+ self.__add_polemic_attributes()
def natural_key(self):
return self.iri_id
@@ -376,68 +383,65 @@
def get_tags(self):
return Tag.objects.get_for_object(self)
- indices = {
- 'annotation_volume_begin' : 0,
- 'annotation_volume_end' : settings.DIVISIONS_FOR_STAT_ANNOTATION,
- 'pol_positive' : settings.DIVISIONS_FOR_STAT_ANNOTATION,
- 'pol_negative' : settings.DIVISIONS_FOR_STAT_ANNOTATION + 1,
- 'pol_reference' : settings.DIVISIONS_FOR_STAT_ANNOTATION + 2,
- 'pol_question' : settings.DIVISIONS_FOR_STAT_ANNOTATION + 3,
- }
+ pol_indices = {
+ 'annotation_volume_begin' : 0,
+ 'annotation_volume_end' : settings.DIVISIONS_FOR_STAT_ANNOTATION,
+ 'pol_positive' : settings.DIVISIONS_FOR_STAT_ANNOTATION,
+ 'pol_negative' : settings.DIVISIONS_FOR_STAT_ANNOTATION + 1,
+ 'pol_reference' : settings.DIVISIONS_FOR_STAT_ANNOTATION + 2,
+ 'pol_question' : settings.DIVISIONS_FOR_STAT_ANNOTATION + 3,
+ }
+
+ # add polemic attributes and polemic attribute rates to class Content
+ def __add_polemic_attributes(self):
+ for element in self.pol_indices.keys():
+ if element.startswith('pol_'):
+ Content.add_to_class(element, property(self.__make_getter(element), self.__make_setter(element)))
+ Content.add_to_class("%s_rate" % element, property(self.__make_rate(element)))
+
+ def __make_getter(self, i):
+ def inner_getter(self):
+ l = self.__str2list(self.stat_annotation)
+ return l[Content.pol_indices[i]]
+ return inner_getter
+
+ def __make_setter(self, i):
+ def inner_setter(self, value):
+ l = self.__str2list(self.stat_annotation)
+ l[Content.pol_indices[i]] = value
+ self.stat_annotation = self.__list2str(l)
+ return inner_setter
+
+ def __make_rate(self, i):
+ def inner_rate(self):
+ if self.nb_annotation <= 0:
+ return 0
+ return int(getattr(self, i) / float(self.nb_annotation) * 100 )
+ return inner_rate
+
+ def __list2str(self, l):
+ return ("%s" % l)[1:-1].replace(' ','')
+
+ def __str2list(self, s):
+ return [int(x) for x in s.split(',')]
+
def annotation_volume(): #@NoSelf
def fget(self):
- l = self.str2list(self.stat_annotation)
- return l[Content.indices['annotation_volume_begin']:Content.indices['annotation_volume_end']]
+ l = self.__str2list(self.stat_annotation)
+ return l[Content.pol_indices['annotation_volume_begin']:Content.pol_indices['annotation_volume_end']]
def fset(self, value): # value is a list
- l = self.str2list(self.stat_annotation)
- l[Content.indices['annotation_volume_begin']:Content.indices['annotation_volume_end']] = value
- self.stat_annotation = self.list2str(l)
+ l = self.__str2list(self.stat_annotation)
+ l[Content.pol_indices['annotation_volume_begin']:Content.pol_indices['annotation_volume_end']] = value
+ self.stat_annotation = self.__list2str(l)
return locals()
- annotation_volume = property(**annotation_volume())
-
- def polemic_positive(): #@NoSelf
-
- def fget(self):
- l = self.str2list(self.stat_annotation)
- return l[Content.indices['pol_positive']]
-
- def fset(self, value):
- l = self.str2list(self.stat_annotation)
- l[Content.indices['pol_positive']] = value
- self.stat_annotation = self.list2str(l)
-
- return locals()
-
- polemic_positive = property(**polemic_positive())
-
+ annotation_volume = property(**annotation_volume())
- def polemic_negative(): #@NoSelf
-
- def fget(self):
- l = self.str2list(self.stat_annotation)
- return l[Content.indices['pol_negative']]
-
- def fset(self, value):
- l = self.str2list(self.stat_annotation)
- l[Content.indices['pol_negative']] = value
- self.stat_annotation = self.list2str(l)
-
- return locals()
-
- polemic_negative = property(**polemic_negative())
-
-
-
- def list2str(self, l):
- return ("%s" % l)[1:-1].replace(' ','')
-
- def str2list(self, s):
- return [int(x) for x in s.split(',')]
+
diff -r 8004f0d1a1bf -r b1dd78b59750 src/ldt/ldt/ldt_utils/templates/front/front_all_contents.html
--- a/src/ldt/ldt/ldt_utils/templates/front/front_all_contents.html Fri Jan 27 11:26:48 2012 +0100
+++ b/src/ldt/ldt/ldt_utils/templates/front/front_all_contents.html Fri Jan 27 14:39:39 2012 +0100
@@ -32,8 +32,8 @@
{% thumbnail content.image "294x165" format="PNG" crop="center" as im %}{% endthumbnail %}