Ejemplo n.º 1
0
 def setUp(self):
     self.widget = Plugin(name='That gum you like is coming back in style', order=0)
     self.widget.save()
     self.report_plugin = Report(name='Drymouth')
     self.report_plugin.save()
     self.machine_detail_plugin = MachineDetailPlugin(name='Burrito Mojado', order=0)
     self.machine_detail_plugin.save()
Ejemplo n.º 2
0
def settings_report_enable(request, plugin_name):
    # only do this if there isn't a plugin already with the name
    try:
        _ = Report.objects.get(name=plugin_name)
    except Report.DoesNotExist:
        plugin = sal.plugin.PluginManager.get_plugin_by_name(plugin_name)
        if plugin:
            report = Report(name=plugin_name)
            report.save()
    return redirect('settings_reports')
Ejemplo n.º 3
0
class PluginTest(TestCase):

    def setUp(self):
        self.widget = Plugin(name='That gum you like is coming back in style', order=0)
        self.widget.save()
        self.report_plugin = Report(name='Drymouth')
        self.report_plugin.save()
        self.machine_detail_plugin = MachineDetailPlugin(name='Burrito Mojado', order=0)
        self.machine_detail_plugin.save()

    def test_process_plugin_no_yapsy_plugin(self):
        """Test that requesting non-existent plugins via URL 404s"""
        self.assertRaises(Http404, non_ui_views.process_plugin, 'Once a moose bit my sister')

    @patch('sal.plugin.PluginManager.get_plugin_by_name')
    def test_process_widget_not_enabled(self, manager):
        """Test that requesting disabled plugins via URL 404s"""
        manager.return_value = Widget()
        self.assertRaises(Http404, non_ui_views.process_plugin, 'Once a moose bit my sister')

    @patch('sal.plugin.PluginManager.get_plugin_by_name')
    def test_process_report_not_enabled(self, manager):
        """Test that requesting disabled plugins via URL 404s"""
        manager.return_value = ReportPlugin()
        self.assertRaises(Http404, non_ui_views.process_plugin, 'Once a moose bit my sister')

    @patch('sal.plugin.PluginManager.get_plugin_by_name')
    def test_process_detailplugin_not_enabled(self, manager):
        """Test that requesting disabled detail plugins via URL 404s"""
        manager.return_value = DetailPlugin()
        self.assertRaises(Http404, non_ui_views.process_plugin, 'Once a moose bit my sister')

    @patch('sal.plugin.PluginManager.get_plugin_by_name')
    def test_process_widget_enabled(self, manager):
        """Test that requesting enabled, existing plugins works"""
        manager.return_value = Widget()
        self.assertTrue(non_ui_views.process_plugin(plugin_name=self.widget.name))

    @patch('sal.plugin.PluginManager.get_plugin_by_name')
    def test_process_report_enabled(self, manager):
        """Test that requesting enabled, existing reports works"""
        manager.return_value = ReportPlugin()
        self.assertTrue(non_ui_views.process_plugin(plugin_name=self.report_plugin.name))

    @patch('sal.plugin.PluginManager.get_plugin_by_name')
    def test_process_machine_detail_plugin_enabled(self, manager):
        """Test that requesting enabled, existing detail plugins works"""
        manager.return_value = DetailPlugin()
        self.assertTrue(non_ui_views.process_plugin(plugin_name=self.machine_detail_plugin.name))
Ejemplo n.º 4
0
 def setUp(self):
     self.ga_user = User.objects.get(pk=1)
     user_profile = self.ga_user.userprofile
     user_profile.level = 'GA'
     user_profile.save()
     settings.BASIC_AUTH = False
     self.client = Client()
     self.client.force_login(self.ga_user)
     self.url = '/settings/plugins/enable'
     # Avoid sending analytics to the project while testing!
     server.utils.set_setting('send_data', False)
     self.widget = Plugin(name='Widget', order=0)
     self.widget.save()
     self.report_plugin = Report(name='Report')
     self.report_plugin.save()
Ejemplo n.º 5
0
def settings_report_enable(request, plugin_name):
    # only do this if there isn't a plugin already with the name
    try:
        plugin = Report.objects.get(name=plugin_name)
    except Report.DoesNotExist:
        plugin = Report(name=plugin_name)
        plugin.save()
    return redirect('settings_reports')
Ejemplo n.º 6
0
class WidgetSettingsTest(TestCase):
    """Functional tests for Widget settings views."""
    fixtures = ['user_fixture.json']

    def setUp(self):
        self.ga_user = User.objects.get(pk=1)
        user_profile = self.ga_user.userprofile
        user_profile.level = 'GA'
        user_profile.save()
        settings.BASIC_AUTH = False
        self.client = Client()
        self.client.force_login(self.ga_user)
        self.url = '/settings/plugins/enable'
        # Avoid sending analytics to the project while testing!
        server.utils.set_setting('send_data', False)
        self.widget = Plugin(name='Widget', order=0)
        self.widget.save()
        self.report_plugin = Report(name='Report')
        self.report_plugin.save()

    def test_enable_enabled_widget(self):
        """Test that enabling an enabled widget does nothing"""
        self.client.get(f'{self.url}/{self.widget.name}/')
        self.assertEqual(Plugin.objects.count(), 1)

    @patch('sal.plugin.PluginManager.get_plugin_by_name')
    def test_enable_disabled_widget(self, manager):
        """Test that enabling a disabled widget works"""
        # Fake that this plugin has a yapsy file
        manager.return_value = Widget()
        self.client.get(f'{self.url}/tacos/')
        self.assertEqual(Plugin.objects.count(), 2)

    def test_enable_nonexistent_widget(self):
        """If a plugin does not exist, redirect to plugin list."""
        self.client.get(f'{self.url}/tacos/')
        self.assertEqual(Plugin.objects.count(), 1)
Ejemplo n.º 7
0
def run_migration():
    if type(db) == peewee.SqliteDatabase:
        migrator = SqliteMigrator(db)
    elif type(db) == peewee.MySQLDatabase:
        migrator = MySQLMigrator(db)
    elif type(db) == peewee.PostgresqlDatabase:
        migrator = PostgresqlMigrator(db)
    else:
        return

    migrate(
        migrator.add_column('report', 'first_report_datetime',
                            peewee.DateTimeField(default=UTC_now)),
        migrator.drop_column('report', 'is_open'),
    )
    query = Report.select()
    for report in query:
        report.first_report_datetime = report.datetime
        report.save()
Ejemplo n.º 8
0
Archivo: 0.4.py Proyecto: Phyks/Cygnal
def run_migration():
    if type(db) == peewee.SqliteDatabase:
        migrator = SqliteMigrator(db)
    elif type(db) == peewee.MySQLDatabase:
        migrator = MySQLMigrator(db)
    elif type(db) == peewee.PostgresqlDatabase:
        migrator = PostgresqlMigrator(db)
    else:
        return

    migrate(
        migrator.add_column('report', 'source',
                            peewee.CharField(max_length=255, default='')),
        migrator.add_column('report', 'shape_geojson',
                            peewee.TextField(default=None, null=True)),
    )
    query = Report.select()
    for report in query:
        report.source = 'unknown'
        report.save()
Ejemplo n.º 9
0
def report():
    if not request.json:
        abort(400)

    body = request.json

    ip = request.remote_addr
    s = Server.query.filter(Server.ip == ip,
                            Server.name == body.get('server_name',
                                                    '')).first()
    if s is None:
        return
    r = Report(agent=body.get('agent', ''),
               status=body.get('status', ''),
               desc=body.get('desc', ''),
               id_server=s.id)
    db.session.add(r)
    update_agent_status(s, r.agent, r.status)
    db.session.commit()

    return "Report saved"
Ejemplo n.º 10
0
def process_opendata(name, data, report_type=REPORT_TYPE):
    # Coordinates of current reports in db, as shapely Points in Lambert93
    current_reports_points = []
    active_reports_from_db = Report.select().where(
        # Load reports from db of the current type
        (Report.type == report_type) & (
            # Either with an expiration_datetime in the future
            ((Report.expiration_datetime is not None) &
             (Report.expiration_datetime > UTC_now())) |
            # Or without expiration_datetime but which are still active (shown
            # on the map)
            ((Report.expiration_datetime is None) &
             (Report.downvotes < REPORT_DOWNVOTES_THRESHOLD))))
    for report in active_reports_from_db:
        current_reports_points.append(
            transform(project, Point(report.lng, report.lat)))

    for item in data:
        try:
            fields = item['fields']

            # Check that the work is currently being done
            now = arrow.now('Europe/Paris')
            if fields['date_debut']:
                start_date = arrow.get(fields['date_debut'].replace('/', '-'))
            else:
                # Defaults to now if start date is unknown
                start_date = arrow.get(now)
            if fields['date_fin']:
                end_date = arrow.get(fields['date_fin'].replace('/', '-'))
            else:
                # Defaults to in a week if start date is unknown
                end_date = arrow.get(now).shift(days=+7)
            if not (start_date < now < end_date):
                logging.info(
                    'Ignoring record %s, work not currently in progress.',
                    item['recordid'])
                continue

            # Report geographical shape
            if 'geo_shape' in fields:
                maybe_multi_geo_shape = shape(fields['geo_shape'])
            else:
                maybe_multi_geo_shape = shape(item['geometry'])

            geo_shapes = []
            if (isinstance(maybe_multi_geo_shape, MultiPolygon)
                    or isinstance(maybe_multi_geo_shape, MultiPoint)):
                # Split MultiPolygon into multiple Polygon
                # Same for MultiPoint
                positions = [p.centroid for p in maybe_multi_geo_shape]
                geo_shapes = [p for p in maybe_multi_geo_shape]
            elif isinstance(maybe_multi_geo_shape, MultiLineString):
                # Split MultiLineString into multiple LineString
                positions = [
                    p.interpolate(0.5, normalized=True)
                    for p in maybe_multi_geo_shape
                ]
                geo_shapes = [p for p in maybe_multi_geo_shape]
            elif isinstance(maybe_multi_geo_shape, LineString):
                # LineString, interpolate midpoint
                positions = [
                    maybe_multi_geo_shape.interpolate(0.5, normalized=True)
                ]
                geo_shapes = [maybe_multi_geo_shape]
            else:
                # Polygon or Point
                positions = [maybe_multi_geo_shape.centroid]
                geo_shapes = [maybe_multi_geo_shape]

            for (geo_shape, position) in zip(geo_shapes, positions):
                # Check if this precise position is already in the database
                if transform(project, position) in current_reports_points:
                    logging.info(
                        ('Ignoring record %s, a similar report is already in '
                         'the database.'), item['recordid'])
                    continue
                # Check no similar reports is within the area of the report, up
                # to the report distance.
                overlap_area = transform(
                    project, geo_shape).buffer(MIN_DISTANCE_REPORT_DETAILS)
                is_already_inserted = False
                for report_point in current_reports_points:
                    if report_point.within(overlap_area):
                        # A similar report is already there
                        is_already_inserted = True
                        logging.info(
                            ('Ignoring record %s, a similar report is already '
                             'in the database.'), item['recordid'])
                        break
                if is_already_inserted:
                    # Skip this report if a similar one is nearby
                    continue

                # Get the position of the center of the item
                lng, lat = position.x, position.y
                # Compute expiration datetime
                expiration_datetime = end_date.replace(microsecond=0).naive

                # Add the report to the db
                logging.info('Adding record %s to the database.',
                             item['recordid'])
                Report.create(type=report_type,
                              expiration_datetime=expiration_datetime,
                              lat=lat,
                              lng=lng,
                              source=item['source'],
                              shape_geojson=json.dumps(mapping(geo_shape)))
        except KeyError as exc:
            logging.warning('Invalid record %s in %s, missing key: %s',
                            item.get('recordid', '?'), name, exc)
Ejemplo n.º 11
0
def process_opendata(name, data, report_type=REPORT_TYPE):
    # Coordinates of current reports in db, as shapely Points in Lambert93
    current_reports_points = []
    active_reports_from_db = Report.select().where(
        # Load reports from db of the current type
        (Report.type == report_type) & (
            # Either with an expiration_datetime in the future
            ((Report.expiration_datetime is not None) &
             (Report.expiration_datetime > UTC_now())) |
            # Or without expiration_datetime but which are still active (shown
            # on the map)
            ((Report.expiration_datetime is None) &
             (Report.downvotes < REPORT_DOWNVOTES_THRESHOLD))))
    for report in active_reports_from_db:
        current_reports_points.append(
            transform(project, Point(report.lng, report.lat)))

    # TODO: Remove reports which are no longer valid

    # Filter out unknown states and roads without traffic
    data = [
        x for x in data
        if x['fields'].get('state') not in ['FLUIDE', 'INCONNU']
    ]

    for item in data:
        try:
            fields = item['fields']

            # Get geometry and position
            geometry = shape(item['geometry'])
            position = geometry.centroid
            lng, lat = position.x, position.y

            # Check if this precise position is already in the database
            if transform(project, position) in current_reports_points:
                logging.info(
                    ('Ignoring record %s, a similar report is already in '
                     'the database.'), item['recordid'])
                continue
            # Check no similar reports is within the area of the report, up
            # to the report distance.
            overlap_area = transform(
                project, geometry).buffer(MIN_DISTANCE_REPORT_DETAILS)
            is_already_inserted = False
            for report_point in current_reports_points:
                if report_point.within(overlap_area):
                    # A similar report is already there
                    is_already_inserted = True
                    logging.info(
                        ('Ignoring record %s, a similar report is already '
                         'in the database.'), item['recordid'])
                    break
            if is_already_inserted:
                # Skip this report if a similar one is nearby
                continue

            # Expires in an hour
            expiration_datetime = (
                # TODO: Check the datetime value in the opendata file
                arrow.get(fields['datetime']).shift(hours=+1).naive)

            # Add the report to the db
            logging.info('Adding record %s to the database.', item['recordid'])
            Report.create(type=report_type,
                          expiration_datetime=expiration_datetime,
                          lat=lat,
                          lng=lng,
                          source=item['source'],
                          shape_geojson=json.dumps(mapping(geometry)))
        except KeyError as exc:
            logging.warning('Invalid record %s in %s, missing key: %s',
                            item.get('recordid', '?'), name, exc)