Skip to content

Commit

Permalink
Rename MetaSchema to RegistrationSchema, and add the AbstractSchema m…
Browse files Browse the repository at this point in the history
…etaclass

[#PLAT-978]
  • Loading branch information
erinspace committed Jul 30, 2018
1 parent e2a33ae commit 536a80e
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 25 deletions.
4 changes: 2 additions & 2 deletions admin/pre_reg/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from osf.models import (
BaseFileNode,
DraftRegistration,
MetaSchema,
RegistrationSchema,
Node,
OSFUser,
)
Expand Down Expand Up @@ -99,7 +99,7 @@ def get_context_data(self, **kwargs):
def get_object(self, queryset=None):
self.prereg_admins = OSFUser.objects.filter(groups__name='prereg_view')
self.bad_drafts = DraftRegistration.objects.filter(
registration_schema=MetaSchema.objects.get(name='Prereg Challenge', schema_version=2),
registration_schema=RegistrationSchema.objects.get(name='Prereg Challenge', schema_version=2),
approval__state__in=['approved', 'rejected'],
branched_from__files__checkout__in=self.prereg_admins
).distinct().values_list('id', flat=True)
Expand Down
6 changes: 3 additions & 3 deletions api/metaschemas/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from api.base.views import JSONAPIBaseView
from api.base.utils import get_object_or_error

from osf.models import MetaSchema
from osf.models import RegistrationSchema
from api.metaschemas.serializers import MetaSchemaSerializer, RegistrationMetaSchemaSerializer


Expand All @@ -31,7 +31,7 @@ class RegistrationMetaschemaList(JSONAPIBaseView, generics.ListAPIView):

# overrides ListCreateAPIView
def get_queryset(self):
return MetaSchema.objects.filter(schema_version=LATEST_SCHEMA_VERSION, active=True)
return RegistrationSchema.objects.filter(schema_version=LATEST_SCHEMA_VERSION, active=True)


class RegistrationMetaschemaDetail(JSONAPIBaseView, generics.RetrieveAPIView):
Expand All @@ -52,7 +52,7 @@ class RegistrationMetaschemaDetail(JSONAPIBaseView, generics.RetrieveAPIView):
# overrides RetrieveAPIView
def get_object(self):
schema_id = self.kwargs['metaschema_id']
return get_object_or_error(MetaSchema, schema_id, self.request)
return get_object_or_error(RegistrationSchema, schema_id, self.request)


class DeprecatedMetaSchemasList(DeprecatedView, RegistrationMetaschemaList):
Expand Down
4 changes: 2 additions & 2 deletions api/nodes/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from addons.base.exceptions import InvalidAuthError, InvalidFolderError
from website.exceptions import NodeStateError
from osf.models import (Comment, DraftRegistration, Institution,
MetaSchema, AbstractNode, PrivateLink)
RegistrationSchema, AbstractNode, PrivateLink)
from osf.models.external import ExternalAccount
from osf.models.licenses import NodeLicense
from osf.models.preprint_service import PreprintService
Expand Down Expand Up @@ -1179,7 +1179,7 @@ def create(self, validated_data):
metadata = validated_data.pop('registration_metadata', None)

schema_id = validated_data.pop('registration_schema').get('_id')
schema = get_object_or_error(MetaSchema, schema_id, self.context['request'])
schema = get_object_or_error(RegistrationSchema, schema_id, self.context['request'])
if schema.schema_version != LATEST_SCHEMA_VERSION or not schema.active:
raise exceptions.ValidationError('Registration supplement must be an active schema.')

Expand Down
4 changes: 2 additions & 2 deletions osf/management/commands/count_preregistrations.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
from django.core.management.base import BaseCommand
from osf.models import Registration, MetaSchema
from osf.models import Registration, RegistrationSchema

PREREG_SCHEMA_NAMES = [
'Prereg Challenge',
Expand All @@ -17,7 +17,7 @@ class Command(BaseCommand):
def handle(self, *args, **options):
total = 0
for schema_name in PREREG_SCHEMA_NAMES:
metaschemas = MetaSchema.objects.filter(name=schema_name).only('id', 'schema_version')
metaschemas = RegistrationSchema.objects.filter(name=schema_name).only('id', 'schema_version')
for metaschema in metaschemas:
registrations = Registration.objects.filter(registered_schema=metaschema).get_roots()
count = registrations.count()
Expand Down
2 changes: 1 addition & 1 deletion osf/migrations/0055_update_metaschema_active.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

from django.db import migrations

from osf.models import MetaSchema
from website.project.metadata.schemas import LATEST_SCHEMA_VERSION


def update_metaschema_active(*args, **kwargs):
MetaSchema = args[0].get_model('osf', 'metaschema')
MetaSchema.objects.filter(schema_version__lt=LATEST_SCHEMA_VERSION).update(active=False)


Expand Down
19 changes: 19 additions & 0 deletions osf/migrations/0121_generalize_schema_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.13 on 2018-07-23 20:23
from __future__ import unicode_literals

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('osf', '0120_merge_20180716_1457'),
]

operations = [
migrations.RenameModel(
old_name='MetaSchema',
new_name='RegistrationSchema',
),
]
2 changes: 1 addition & 1 deletion osf/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from osf.models.metaschema import MetaSchema # noqa
from osf.models.metaschema import RegistrationSchema # noqa
from osf.models.base import Guid, BlackListGuid # noqa
from osf.models.user import OSFUser, Email # noqa
from osf.models.contributor import Contributor, RecentlyAddedContributor # noqa
Expand Down
5 changes: 4 additions & 1 deletion osf/models/metaschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from website.project.metadata.utils import create_jsonschema_from_metaschema


class MetaSchema(ObjectIDMixin, BaseModel):
class AbstractSchema(ObjectIDMixin, BaseModel):
name = models.CharField(max_length=255)
schema = DateTimeAwareJSONField(default=dict)
category = models.CharField(max_length=255, null=True, blank=True)
Expand All @@ -21,8 +21,11 @@ class MetaSchema(ObjectIDMixin, BaseModel):
schema_version = models.IntegerField()

class Meta:
abstract = True
unique_together = ('name', 'schema_version')


class RegistrationSchema(AbstractSchema):
def __unicode__(self):
return '(name={}, schema_version={}, id={})'.format(self.name, self.schema_version, self.id)

Expand Down
8 changes: 4 additions & 4 deletions osf/models/registrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from website.archiver import ARCHIVER_INITIATED

from osf.models import (
OSFUser, MetaSchema, RegistrationApproval,
OSFUser, RegistrationSchema, RegistrationApproval,
Retraction, Embargo, DraftRegistrationApproval,
EmbargoTerminationApproval,
)
Expand All @@ -37,7 +37,7 @@ class Registration(AbstractNode):
on_delete=models.SET_NULL,
null=True, blank=True)

registered_schema = models.ManyToManyField(MetaSchema)
registered_schema = models.ManyToManyField(RegistrationSchema)

registered_meta = DateTimeAwareJSONField(default=dict, blank=True)
# TODO Add back in once dependencies are resolved
Expand Down Expand Up @@ -457,13 +457,13 @@ class DraftRegistration(ObjectIDMixin, BaseModel):
# }
# }
registration_metadata = DateTimeAwareJSONField(default=dict, blank=True)
registration_schema = models.ForeignKey('MetaSchema', null=True, on_delete=models.CASCADE)
registration_schema = models.ForeignKey('RegistrationSchema', null=True, on_delete=models.CASCADE)
registered_node = models.ForeignKey('Registration', null=True, blank=True,
related_name='draft_registration', on_delete=models.CASCADE)

approval = models.ForeignKey('DraftRegistrationApproval', null=True, blank=True, on_delete=models.CASCADE)

# Dictionary field mapping extra fields defined in the MetaSchema.schema to their
# Dictionary field mapping extra fields defined in the RegistrationSchema.schema to their
# values. Defaults should be provided in the schema (e.g. 'paymentSent': false),
# and these values are added to the DraftRegistration
# TODO: Use "FIELD_ALIASES"?
Expand Down
6 changes: 3 additions & 3 deletions osf/models/sanctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
)
from website.project import tasks as project_tasks

from osf.models import MetaSchema
from osf.models import RegistrationSchema
from osf.models.base import BaseModel, ObjectIDMixin
from osf.utils.datetime_aware_jsonfield import DateTimeAwareJSONField

Expand Down Expand Up @@ -350,7 +350,7 @@ def _notify_initiator(self):
DraftRegistration = apps.get_model('osf.DraftRegistration')

registration = self._get_registration()
prereg_schema = MetaSchema.get_prereg_schema()
prereg_schema = RegistrationSchema.get_prereg_schema()
draft = DraftRegistration.objects.get(registered_node=registration)

if registration.registered_schema.filter(id=prereg_schema.id).exists():
Expand All @@ -366,7 +366,7 @@ def _email_template_context(self,
is_authorizer=False,
urls=None):
registration = self._get_registration()
prereg_schema = MetaSchema.get_prereg_schema()
prereg_schema = RegistrationSchema.get_prereg_schema()
if registration.registered_schema.filter(pk=prereg_schema.pk).exists():
return {
'custom_message':
Expand Down
12 changes: 6 additions & 6 deletions osf/utils/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from django.apps import apps

from website import settings
from osf.models import NodeLicense, MetaSchema
from osf.models import NodeLicense, RegistrationSchema
from website.project.metadata.schemas import OSF_META_SCHEMAS

logger = logging.getLogger(__file__)
Expand Down Expand Up @@ -132,12 +132,12 @@ def ensure_schemas(*args):
"""
schema_count = 0
try:
MetaSchema = args[0].get_model('osf', 'metaschema')
RegistrationSchema = args[0].get_model('osf', 'metaschema')
except:
# Working outside a migration
from osf.models import MetaSchema
from osf.models import RegistrationSchema
for schema in OSF_META_SCHEMAS:
schema_obj, created = MetaSchema.objects.update_or_create(
schema_obj, created = RegistrationSchema.objects.update_or_create(
name=schema['name'],
schema_version=schema.get('version', 1),
defaults={
Expand All @@ -154,7 +154,7 @@ def ensure_schemas(*args):


def remove_schemas(*args):
pre_count = MetaSchema.objects.all().count()
MetaSchema.objects.all().delete()
pre_count = RegistrationSchema.objects.all().count()
RegistrationSchema.objects.all().delete()

logger.info('Removed {} schemas from the database'.format(pre_count))

0 comments on commit 536a80e

Please sign in to comment.