views.py 4.64 KB
Newer Older
Nicolas Chauvat's avatar
Nicolas Chauvat committed
1
2
3
4
5
6
7
8
9
10
"""cube-specific forms/views/actions/components

:organization: Logilab
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses
"""

from logilab.mtconverter import xml_escape

Nicolas Chauvat's avatar
Nicolas Chauvat committed
11
from cubicweb.selectors import has_related_entities, is_instance
Nicolas Chauvat's avatar
Nicolas Chauvat committed
12

13
from cubicweb.web import uicfg, component, box
Nicolas Chauvat's avatar
Nicolas Chauvat committed
14
15
16
from cubicweb.web.views import primary, baseviews
from cubicweb.web.facet import RelationFacet, AttributeFacet, RangeFacet, DateRangeFacet

Nicolas Chauvat's avatar
Nicolas Chauvat committed
17
from cubes.similarity import get_vspace
18
from cubes.similarity.views import SimilarAction
Nicolas Chauvat's avatar
Nicolas Chauvat committed
19

Nicolas Chauvat's avatar
Nicolas Chauvat committed
20
21
22
23
24
25
26
uicfg.primaryview_section.tag_subject_of(('ClassifiedAd', 'same_as', '*'), 'hidden')
uicfg.primaryview_section.tag_subject_of(('ClassifiedAd', 'url', '*'), 'hidden')
uicfg.primaryview_section.tag_subject_of(('ClassifiedAd', 'has_image', '*'), 'hidden')
#uicfg.primaryview_section.tag_subject_of(('ClassifiedAd', 'advertise', '*'), 'hidden')
uicfg.primaryview_section.tag_subject_of(('ClassifiedAd', 'title', '*'), 'hidden')
uicfg.actionbox_appearsin_addmenu.tag_subject_of(('ClassifiedAd', 'has_image', '*'), True)

27
class ExternalUriGallery(baseviews.EntityView):
28
29
    __regid__ = 'gallery'
    title = _('gallery')
Nicolas Chauvat's avatar
Nicolas Chauvat committed
30
    __select__ = is_instance('ExternalUri')
31
32
33

    def call(self):
        for entity in self.cw_rset.entities():
34
            self.w(u'<img style="max-height: 100px; padding: 2px" src="%s" />' % entity.uri)
35

Nicolas Chauvat's avatar
Nicolas Chauvat committed
36
class ClassifiedAdPrimaryView(primary.PrimaryView):
Nicolas Chauvat's avatar
Nicolas Chauvat committed
37
    __select__ = primary.PrimaryView.__select__ & is_instance('ClassifiedAd')
Nicolas Chauvat's avatar
Nicolas Chauvat committed
38

Nicolas Chauvat's avatar
Nicolas Chauvat committed
39
40
    def render_entity_relations(self, entity):
        super(ClassifiedAdPrimaryView, self).render_entity_relations(entity)
41
        self.wview('gallery', entity.related('has_image'), 'null')
Nicolas Chauvat's avatar
Nicolas Chauvat committed
42
43

class ClassifiedAdInContextView(baseviews.SameETypeListItemView):
Nicolas Chauvat's avatar
Nicolas Chauvat committed
44
    __select__ = is_instance('ClassifiedAd')
Nicolas Chauvat's avatar
Nicolas Chauvat committed
45
46

    def cell_call(self, row, col, contexteid=None):
Nicolas Chauvat's avatar
Nicolas Chauvat committed
47
        entity = self.cw_rset.get_entity(row, col)
Nicolas Chauvat's avatar
Nicolas Chauvat committed
48
49
50
51
52
53
54
55
56
        abs_url = entity.absolute_url()
        self.w(u'<table><tr><td width="20%" align="right">')
        if entity.has_image:
            self.w(u'<a href="%s"><img height="100" src="%s" /></a>' % (abs_url, entity.has_image[0].uri))
        self.w(u'</td><td>')
        self.w(u'<div><a href="%s">%s</a><br/>%s</div>' %
               (abs_url, xml_escape(entity.title or u''), xml_escape(entity.description or u'')))
        self.w(u'</td></tr></table>')

Nicolas Chauvat's avatar
Nicolas Chauvat committed
57
58
# class ClassifiedPriceFacet(RangeFacet):
#     __regid__ = 'classified-price-facet'
Nicolas Chauvat's avatar
Nicolas Chauvat committed
59
#     __select__ = RangeFacet.__select__ & is_instance('ClassifiedAd')
Nicolas Chauvat's avatar
Nicolas Chauvat committed
60
#     rtype = 'price'
Nicolas Chauvat's avatar
Nicolas Chauvat committed
61

Nicolas Chauvat's avatar
Nicolas Chauvat committed
62
63
# class ClassifiedPubDateFacet(DateRangeFacet):
#     __regid__ = 'classified-publication_date-facet'
Nicolas Chauvat's avatar
Nicolas Chauvat committed
64
#     __select__ = DateRangeFacet.__select__ & is_instance('ClassifiedAd')
Nicolas Chauvat's avatar
Nicolas Chauvat committed
65
#     rtype = 'publication_date'
Nicolas Chauvat's avatar
Nicolas Chauvat committed
66
67
68
69

class ProximityOfAds(baseviews.EntityView):
    __regid__ = 'proximity-of-ads'
    title = _('proximity of ads')
Nicolas Chauvat's avatar
Nicolas Chauvat committed
70
    __select__ = is_instance('ClassifiedAd')
Nicolas Chauvat's avatar
Nicolas Chauvat committed
71
72
73
74

    def call(self):
        self.w(u'<table><tr><th width="50%%">%s</th><th>%s</th></tr>' %
               (self._cw._('ClassifiedAd'), self._cw._('Similar Ads')))
Nicolas Chauvat's avatar
Nicolas Chauvat committed
75
        for row, ad in enumerate(self.cw_rset.entities()):
Nicolas Chauvat's avatar
Nicolas Chauvat committed
76
            self.w(u'<tr class="%s"><td>' % (row % 2 and u'even' or u'odd'))
Nicolas Chauvat's avatar
Nicolas Chauvat committed
77
            self.w(ad.view('sameetypelistitem'))
Nicolas Chauvat's avatar
Nicolas Chauvat committed
78
            self.w(u'</td><td><ul>')
Nicolas Chauvat's avatar
Nicolas Chauvat committed
79
80
81
            vspace = get_vspace('ClassifiedAd', self._cw)
            for score, eids in vspace.similarity_by_id(ad.eid)[:3]:
                self.w(u'<li>%.2f - %s</li>' % (score, ' '.join(self._cw.entity_from_eid(eid).view('outofcontext') for eid in eids)))
Nicolas Chauvat's avatar
Nicolas Chauvat committed
82
83
84
            self.w(u'</ul></td></tr>')
        self.w(u'</table>')

85
86
class ClosestAdsBox(box.EntityBoxTemplate):
    __regid__ = 'closest_ads_box'
Nicolas Chauvat's avatar
Nicolas Chauvat committed
87
    __select__ = box.EntityBoxTemplate.__select__ & is_instance('ClassifiedAd',)
88
89
90
91
    order = 25

    def cell_call(self, row, col, **kwargs):
        entity = self.cw_rset.get_entity(row, col)
Nicolas Chauvat's avatar
Nicolas Chauvat committed
92
93
        vspace = get_vspace('ClassifiedAd', self._cw)
        scores = vspace.similarity_by_id(entity.eid)
94
95
        if scores:
            self.w(u'<div class="sideBox">')
96
            self.w(u'<div class="sideBoxTitle"><span>%s</span></div>' % _('Similar ads'))
97
            self.w(u'<div class="%s"><div class="sideBoxBody">' % 'sideBox')
Nicolas Chauvat's avatar
Nicolas Chauvat committed
98
99
100
            for score, eids in scores[:5]:
                for eid in eids:
                    self.w(u'<span>%.2f - %s</span><br />' % (score, self._cw.entity_from_eid(eid).view('outofcontext')))
101
102
103
            self.w(u'</div>')
            self.w(u'</div>')
            self.w(u'</div>')
104
105
106

class ClassifiedAdSimilarAction(SimilarAction):
    __select__ = is_instance('ClassifiedAd')