Browse Source

Merge pull request #255 from CatalystCode/bugfix/support-sql-server

Bugfix/Add support for SQL Server database
pull/311/head
Hiroki Nakayama 5 years ago
committed by GitHub
parent
commit
1f316b692f
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 63 additions and 16 deletions
  1. 20
      Dockerfile
  2. 4
      app/api/migrations/0001_initial.py
  3. 28
      app/api/migrations/0003_support_sql_server.py
  4. 17
      app/api/models.py
  5. 4
      app/api/tests/test_models.py
  6. 5
      app/app/settings.py
  7. 1
      requirements.txt

20
Dockerfile

@ -3,7 +3,11 @@ FROM python:${PYTHON_VERSION}-stretch AS builder
ARG NODE_VERSION="8.x"
RUN curl -sL "https://deb.nodesource.com/setup_${NODE_VERSION}" | bash - \
&& apt-get install nodejs
&& apt-get install --no-install-recommends -y \
nodejs=8.16.0-1nodesource1
RUN apt-get install --no-install-recommends -y \
unixodbc-dev=2.3.4-1
COPY app/server/static/package*.json /doccano/app/server/static/
RUN cd /doccano/app/server/static \
@ -29,6 +33,20 @@ RUN cd /doccano \
FROM python:${PYTHON_VERSION}-slim-stretch AS runtime
RUN apt-get update \
&& apt-get install --no-install-recommends -y \
curl=7.52.1-5+deb9u9 \
gnupg=2.1.18-8~deb9u4 \
apt-transport-https=1.4.9 \
&& curl -fsS https://packages.microsoft.com/keys/microsoft.asc | apt-key add - \
&& curl -fsS https://packages.microsoft.com/config/debian/9/prod.list > /etc/apt/sources.list.d/mssql.list \
&& apt-get update \
&& ACCEPT_EULA=Y apt-get install --no-install-recommends -y \
msodbcsql17=17.3.1.1-1 \
mssql-tools=17.3.0.1-1 \
&& apt-get remove -y curl gnupg apt-transport-https \
&& rm -rf /var/lib/apt/lists/*
RUN useradd -ms /bin/sh doccano
COPY --from=builder /deps /deps

4
app/api/migrations/0001_initial.py

@ -161,10 +161,6 @@ class Migration(migrations.Migration):
name='sequenceannotation',
unique_together={('document', 'user', 'label', 'start_offset', 'end_offset')},
),
migrations.AlterUniqueTogether(
name='seq2seqannotation',
unique_together={('document', 'user', 'text')},
),
migrations.AlterUniqueTogether(
name='label',
unique_together={('project', 'text'), ('project', 'prefix_key', 'suffix_key')},

28
app/api/migrations/0003_support_sql_server.py

@ -0,0 +1,28 @@
# Generated by Django 2.1.7 on 2019-06-26 13:20
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('api', '0002_approve_document_labels'),
]
operations = [
migrations.AlterField(
model_name='seq2seqannotation',
name='text',
field=models.CharField(max_length=500),
),
migrations.AlterUniqueTogether(
name='seq2seqannotation',
unique_together={('document', 'user', 'text')},
),
migrations.AlterUniqueTogether(
name='label',
unique_together={('project', 'text')},
),
]

17
app/api/models.py

@ -163,19 +163,18 @@ class Label(models.Model):
# Don't allow shortcut key not to have a suffix key.
if self.prefix_key and not self.suffix_key:
raise ValidationError('Shortcut key may not have a suffix key.')
super().clean()
def validate_unique(self, exclude=None):
# Don't allow to save same shortcut key when prefix_key is null.
if Label.objects.exclude(id=self.id).filter(suffix_key=self.suffix_key,
prefix_key__isnull=True).exists():
raise ValidationError('Duplicate key.')
super().validate_unique(exclude)
# each shortcut (prefix key + suffix key) can only be assigned to one label
if self.suffix_key or self.prefix_key:
other_labels = self.project.labels.exclude(id=self.id)
if other_labels.filter(suffix_key=self.suffix_key, prefix_key=self.prefix_key).exists():
raise ValidationError('A label with this shortcut already exists in the project')
super().clean()
class Meta:
unique_together = (
('project', 'text'),
('project', 'prefix_key', 'suffix_key')
)
@ -226,7 +225,7 @@ class SequenceAnnotation(Annotation):
class Seq2seqAnnotation(Annotation):
document = models.ForeignKey(Document, related_name='seq2seq_annotations', on_delete=models.CASCADE)
text = models.TextField()
text = models.CharField(max_length=500)
class Meta:
unique_together = ('document', 'user', 'text')

4
app/api/tests/test_models.py

@ -91,11 +91,11 @@ class TestLabel(TestCase):
def test_keys_uniqueness(self):
label = mommy.make('Label', prefix_key='ctrl', suffix_key='a')
with self.assertRaises(IntegrityError):
with self.assertRaises(ValidationError):
Label(project=label.project,
text='example',
prefix_key=label.prefix_key,
suffix_key=label.suffix_key).save()
suffix_key=label.suffix_key).full_clean()
def test_suffix_key_uniqueness(self):
label = mommy.make('Label', prefix_key=None, suffix_key='a')

5
app/app/settings.py

@ -254,6 +254,11 @@ DATABASES['default'].update(dj_database_url.config(
if DATABASES['default'].get('ENGINE') == 'django.db.backends.sqlite3':
DATABASES['default'].get('OPTIONS', {}).pop('sslmode', None)
# default to a sensible modern driver for Azure SQL
if DATABASES['default'].get('ENGINE') == 'sql_server.pyodbc':
db_options = DATABASES['default'].setdefault('OPTIONS', {})\
.setdefault('driver', 'ODBC Driver 17 for SQL Server')
# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

1
requirements.txt

@ -9,6 +9,7 @@ django-heroku==0.3.1
django-webpack-loader==0.6.0
django-widget-tweaks==1.4.2
django-polymorphic==2.0.3
django-pyodbc-azure==2.1.0.0
django-rest-polymorphic==0.1.8
djangorestframework==3.8.2
djangorestframework-csv==2.1.0

Loading…
Cancel
Save