krischer / jane

Jane - Document Database for Seismology
http://krischer.github.io/jane
GNU General Public License v3.0
27 stars 9 forks source link

Can not register multiple "retrieve permissions" #19

Open megies opened 8 years ago

megies commented 8 years ago

I'm trying to register a second custom retrieve permission for quakeml document type, analogous to CanSeePrivateEventsRetrievePermissionPlugin (starting from a completely empty jane DB), but this seems to be not possible as during python manage.py migrate there is an error (see below)...

For my understanding the key in the documents_documenttype_retrieve_permissions table should include also the enumerated id field of the table.. but django and jane internals are still pretty much a mystery to me at this point..

Operations to perform:
  Synchronize unmigrated apps: template_timings_panel, stationxml, djangoplugins, corsheaders, quakeml, fdsnws, staticfiles, jane, rest_framework, messages, rest_framework_gis, debug_toolbar
  Apply all migrations: admin, contenttypes, waveforms, documents, sessions, auth
Synchronizing apps without migrations:
  Creating tables...
    Creating table djangoplugins_pluginpoint
    Creating table djangoplugins_plugin
    Creating table corsheaders_corsmodel
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  Rendering model states... DONE
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying documents.0001_initial... OK
  Applying sessions.0001_initial... OK
  Applying waveforms.0001_initial... OK
Registering PluginPoint for jane.documents.plugins.IndexerPluginPoint
Registering Plugin for jane.quakeml.plugins.QuakeMLIndexerPlugin
Registering Plugin for jane.stationxml.plugins.StationIndexerPlugin
Registering PluginPoint for jane.documents.plugins.DocumentPluginPoint
Registering Plugin for jane.stationxml.plugins.StationXMLPlugin
Registering Plugin for jane.quakeml.plugins.QuakeMLPlugin
Registering PluginPoint for jane.documents.plugins.ValidatorPluginPoint
Registering Plugin for jane.quakeml.plugins.QuakeMLValidatorPlugin
Registering Plugin for jane.stationxml.plugins.StationValidatorPlugin
Registering PluginPoint for jane.documents.plugins.RetrievePermissionPluginPoint
Registering Plugin for jane.quakeml.plugins.CanSeePrivateEventsRetrievePermissionPlugin
Registering Plugin for jane.quakeml.plugins.MagnitudeGreaterEqual1RetrievePermissionPlugin
Traceback (most recent call last):
  File "/home/megies/anaconda/envs/jane/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
psycopg2.IntegrityError: duplicate key value violates unique constraint "djangoplugins_plugin_point_id_name_key"
DETAIL:  Key (point_id, name)=(4, quakeml) already exists.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 12, in <module>
    execute_from_command_line(sys.argv)
  File "/home/megies/anaconda/envs/jane/lib/python3.4/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/home/megies/anaconda/envs/jane/lib/python3.4/site-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/megies/anaconda/envs/jane/lib/python3.4/site-packages/django/core/management/base.py", line 393, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/megies/anaconda/envs/jane/lib/python3.4/site-packages/django/core/management/base.py", line 444, in execute
    output = self.handle(*args, **options)
  File "/home/megies/anaconda/envs/jane/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 226, in handle
    emit_post_migrate_signal(created_models, self.verbosity, self.interactive, connection.alias)
  File "/home/megies/anaconda/envs/jane/lib/python3.4/site-packages/django/core/management/sql.py", line 288, in emit_post_migrate_signal
    db=db)
  File "/home/megies/anaconda/envs/jane/lib/python3.4/site-packages/django/dispatch/dispatcher.py", line 201, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/home/megies/anaconda/envs/jane/lib/python3.4/site-packages/djangoplugins/management/__init__.py", line 16, in sync_plugins
    SyncPlugins(False, verbosity).all()
  File "/home/megies/anaconda/envs/jane/lib/python3.4/site-packages/djangoplugins/management/commands/syncplugins.py", line 122, in all
    self.points()
  File "/home/megies/anaconda/envs/jane/lib/python3.4/site-packages/djangoplugins/management/commands/syncplugins.py", line 98, in points
    self.plugins(point, inst)
  File "/home/megies/anaconda/envs/jane/lib/python3.4/site-packages/djangoplugins/management/commands/syncplugins.py", line 114, in plugins
    inst.save()
  File "/home/megies/anaconda/envs/jane/lib/python3.4/site-packages/djangoplugins/models.py", line 118, in save
    return super(Plugin, self).save(*args, **kwargs)
  File "/home/megies/anaconda/envs/jane/lib/python3.4/site-packages/django/db/models/base.py", line 734, in save
    force_update=force_update, update_fields=update_fields)
  File "/home/megies/anaconda/envs/jane/lib/python3.4/site-packages/django/db/models/base.py", line 762, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/home/megies/anaconda/envs/jane/lib/python3.4/site-packages/django/db/models/base.py", line 846, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/home/megies/anaconda/envs/jane/lib/python3.4/site-packages/django/db/models/base.py", line 885, in _do_insert
    using=using, raw=raw)
  File "/home/megies/anaconda/envs/jane/lib/python3.4/site-packages/django/db/models/manager.py", line 127, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/megies/anaconda/envs/jane/lib/python3.4/site-packages/django/db/models/query.py", line 920, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/home/megies/anaconda/envs/jane/lib/python3.4/site-packages/django/db/models/sql/compiler.py", line 974, in execute_sql
    cursor.execute(sql, params)
  File "/home/megies/anaconda/envs/jane/lib/python3.4/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/megies/anaconda/envs/jane/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/home/megies/anaconda/envs/jane/lib/python3.4/site-packages/django/db/utils.py", line 97, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/megies/anaconda/envs/jane/lib/python3.4/site-packages/django/utils/six.py", line 658, in reraise
    raise value.with_traceback(tb)
  File "/home/megies/anaconda/envs/jane/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: duplicate key value violates unique constraint "djangoplugins_plugin_point_id_name_key"
DETAIL:  Key (point_id, name)=(4, quakeml) already exists.
megies commented 8 years ago

The same applies to ValidatorPluginPoint as well, putting a second validator in quakeml/plugins.py, like e.g.:

   32 class QuakeMLValidatorPlugin(ValidatorPluginPoint):
   33     """
   34     Any number of validators can be defined for each document type.
   35 
   36     Each validator must have a validate() method that returns True or False
   37     depending on weather or not the particular validation has passed or not.
   38     Only documents that pass all validations can be stored in the database.
   39     """
   40     name = 'quakeml'
   41     title = 'QuakeML XMLSchema Validator'
   42 
   43     def validate(self, document):
   44         from obspy.io.quakeml.core import _validate as validate_quakeml  # NOQA
   45         try:
   46             is_valid = validate_quakeml(document)
   47         except:
   48             is_valid = False
   49         return is_valid
   50 
   51 
+  52 class QuakeMLValidatorPlugin2(ValidatorPluginPoint):
+  53     """
+  54     """
+  55     name = 'quakeml'
+  56     title = 'QuakeML Dummy Validator'
+  57 
+  58     def validate(self, document):
+  59         return True

Makes the initial DB setup with python manage.py migrate fail (starting from a completely empty DB). So, I'm sure now that this is unintended behavior (at least the docstring says otherwise), i.e. a bug.

megies commented 8 years ago

I think this has to be fixed in https://github.com/krischer/django-plugins..