예제 #1
0
파일: query.py 프로젝트: nyuhuhuu/trachacks
    def expand_macro(self, formatter, name, content):
        req = formatter.req
        query_string = ""
        argv, kwargs = parse_args(content, strict=False)

        if "order" not in kwargs:
            kwargs["order"] = "id"
        if "max" not in kwargs:
            kwargs["max"] = "0"  # unlimited by default

        query_string = "&".join(["%s=%s" % item for item in kwargs.iteritems()])
        query = Query.from_string(self.env, query_string)

        tickets = query.execute(req)
        tickets = [t for t in tickets if "TICKET_VIEW" in req.perm("ticket", t["id"])]
        ticket_ids = [t["id"] for t in tickets]

        # locate the tickets
        geoticket = GeoTicket(self.env)
        locations = geoticket.locate_tickets(ticket_ids, req)

        if not locations:
            return tag.div(tag.b("MapTickets: "), "No locations found for ", tag.i(content))

        data = dict(locations=Markup(simplejson.dumps(locations)), query_href=query.get_href(req), query_string=content)

        # set an id for the map
        map_id = req.environ.setdefault("MapTicketsId", 0) + 1
        req.environ["MapTicketsId"] = map_id
        data["map_id"] = "tickets-map-%d" % map_id

        return Chrome(self.env).render_template(req, "map_tickets.html", data, None, fragment=True)
예제 #2
0
    def enabled(self):
        """return whether this plugin is functional"""

        # ensure PostGIS is enabled on the DB
        geoticket = GeoTicket(self.env)
        if not geoticket.postgis_enabled():
            return False

        # check for available drivers
        drivers = self.drivers()
        return bool(drivers)
예제 #3
0
    def post_process_request(self, req, template, data, content_type):
        """Do any post-processing the request might need; typically adding
        values to the template `data` dictionary, or changing template or
        mime type.
        
        `data` may be update in place.

        Always returns a tuple of (template, data, content_type), even if
        unchanged.

        Note that `template`, `data`, `content_type` will be `None` if:
         - called when processing an error page
         - the default request handler did not return any result

        (Since 0.11)
        """

        if template == 'ticket.html':
            geoticket = GeoTicket(self.env)
            ticket = data['ticket']
            message = req.session.pop('geolocation_error', None)
            if message:
                add_warning(req, Markup(message))

        return (template, data, content_type)
예제 #4
0
    def expand_macro(self, formatter, name, content):
        req = formatter.req
        query_string = ''
        argv, kwargs = parse_args(content, strict=False)

        if 'order' not in kwargs:
            kwargs['order'] = 'id'
        if 'max' not in kwargs:
            kwargs['max'] = '0'  # unlimited by default

        query_string = '&'.join(
            ['%s=%s' % item for item in kwargs.iteritems()])
        query = Query.from_string(self.env, query_string)

        tickets = query.execute(req)
        tickets = [
            t for t in tickets if 'TICKET_VIEW' in req.perm('ticket', t['id'])
        ]
        ticket_ids = [t['id'] for t in tickets]

        # locate the tickets
        geoticket = GeoTicket(self.env)
        locations = geoticket.locate_tickets(ticket_ids, req)

        if not locations:
            return tag.div(tag.b('MapTickets: '), "No locations found for ",
                           tag.i(content))

        data = dict(locations=Markup(simplejson.dumps(locations)),
                    query_href=query.get_href(req),
                    query_string=content)

        # set an id for the map
        map_id = req.environ.setdefault('MapTicketsId', 0) + 1
        req.environ['MapTicketsId'] = map_id
        data['map_id'] = 'tickets-map-%d' % map_id

        return Chrome(self.env).render_template(req,
                                                'map_tickets.html',
                                                data,
                                                None,
                                                fragment=True)
예제 #5
0
    def process_request(self, req):
        """Process the request. For ClearSilver, return a (template_name,
        content_type) tuple, where `template` is the ClearSilver template to use
        (either a `neo_cs.CS` object, or the file name of the template), and
        `content_type` is the MIME type of the content. For Genshi, return a
        (template_name, data, content_type) tuple, where `data` is a dictionary
        of substitutions for the template.

        For both templating systems, "text/html" is assumed if `content_type` is
        `None`.

        Note that if template processing should not occur, this method can
        simply send the response itself and not return anything.
        """

        # get the GeoTicket component
        assert self.env.is_component_enabled(GeoTicket)
        geoticket = GeoTicket(self.env)

        # add the query script
        add_script(req, 'common/js/query.js')

        # get the panel configuration
        config = self.panels()

        # build the panels
        panels = []
        located_tickets = geoticket.tickets_with_location()
        for panel in config:

            # query the tickets
            query_string = panel['query']
            query = Query.from_string(self.env, query_string)

            # decide the date to sort by
            if query.order == 'time':
                date_to_display = 'time_created'
            else:
                date_to_display = 'time_changed'
            results = query.execute(req)
            n_tickets = len(results)
            results = [
                result for result in results if result['id'] in located_tickets
            ]
            locations = []
            tickets = []
            results = results[:self.dashboard_tickets]
            for result in results:
                ticket = Ticket(self.env, result['id'])
                try:

                    address, (lat, lon) = geoticket.locate_ticket(ticket)
                    content = geoticket.feature_content(req, ticket)

                    # style for the markers
                    style = {}
                    for extension in self.marker_style:
                        style.update(extension.style(ticket, req, **style))
                    style = style or None

                    locations.append({
                        'latitude': lat,
                        'longitude': lon,
                        'style': style,
                        'content': Markup(content)
                    })
                    tickets.append(ticket)
                except GeolocationException:
                    continue

            title = panel['label']
            panels.append({
                'title': title,
                'id': panel['id'],
                'locations': Markup(simplejson.dumps(locations)),
                'tickets': tickets,
                'n_tickets': n_tickets,
                'date_to_display': date_to_display,
                'query_href': query.get_href(req.href)
            })

        # add the tag cloud, if enabled
        cloud = None
        if self.display_cloud:
            if TagCloudMacro is None:
                self.log.warn(
                    "[geo] display_cloud is set but the TagsPlugin is not installed"
                )
            else:
                formatter = Formatter(self.env, Context.from_request(req))
                macro = TagCloudMacro(self.env)
                cloud = macro.expand_macro(formatter, 'TagCloud', '')
                add_stylesheet(req, 'tags/css/tractags.css')
                add_stylesheet(req, 'tags/css/tagcloud.css')

        # compile data for the genshi template
        data = dict(panels=panels,
                    cloud=cloud,
                    openlayers_url=self.openlayers_url)
        return ('mapdashboard.html', data, 'text/html')
예제 #6
0
    def process_request(self, req):
        """Process the request. For ClearSilver, return a (template_name,
        content_type) tuple, where `template` is the ClearSilver template to use
        (either a `neo_cs.CS` object, or the file name of the template), and
        `content_type` is the MIME type of the content. For Genshi, return a
        (template_name, data, content_type) tuple, where `data` is a dictionary
        of substitutions for the template.

        For both templating systems, "text/html" is assumed if `content_type` is
        `None`.

        Note that if template processing should not occur, this method can
        simply send the response itself and not return anything.
        """

        # get the GeoTicket component
        assert self.env.is_component_enabled(GeoTicket)
        geoticket = GeoTicket(self.env)

        # add the query script
        add_script(req, 'common/js/query.js')

        # get the panel configuration
        config = self.panels()

        # build the panels
        panels = []
        located_tickets = geoticket.tickets_with_location()
        for panel in config:

            # query the tickets
            query_string = panel['query']
            query = Query.from_string(self.env, query_string)

            # decide the date to sort by
            if query.order == 'time':
                date_to_display = 'time_created'
            else:
                date_to_display = 'time_changed'
            results = query.execute(req)
            n_tickets = len(results)
            results = [ result for result in results if result['id'] in located_tickets ]
            locations = []
            tickets = []
            results = results[:self.dashboard_tickets]
            for result in results:
                ticket = Ticket(self.env, result['id'])
                try:
                
                    address, (lat, lon) = geoticket.locate_ticket(ticket)
                    content = geoticket.feature_content(req, ticket)

                    # style for the markers
                    style = {}
                    for extension in self.marker_style:
                        style.update(extension.style(ticket, req, **style))
                    style = style or None

                    locations.append({'latitude': lat,
                                      'longitude': lon,
                                      'style': style,
                                      'content': Markup(content)})
                    tickets.append(ticket)
                except GeolocationException:
                    continue

            title = panel['label']
            panels.append({'title': title,
                           'id': panel['id'],
                           'locations': Markup(simplejson.dumps(locations)),
                           'tickets': tickets,
                           'n_tickets': n_tickets,
                           'date_to_display': date_to_display,
                           'query_href': query.get_href(req.href)})

        # add the tag cloud, if enabled
        cloud = None
        if self.display_cloud:
            if TagCloudMacro is None:
                self.log.warn("[geo] display_cloud is set but the TagsPlugin is not installed")
            else:
                formatter = Formatter(self.env, Context.from_request(req))
                macro = TagCloudMacro(self.env)
                cloud = macro.expand_macro(formatter, 'TagCloud', '')
                add_stylesheet(req, 'tags/css/tractags.css')
                add_stylesheet(req, 'tags/css/tagcloud.css')

        # compile data for the genshi template
        data = dict(panels=panels,
                    cloud=cloud,
                    openlayers_url=self.openlayers_url)
        return ('mapdashboard.html', data, 'text/html')