Browse Source

#61: make shortcut optional in backend and create new test case

pull/73/head
Bramble Xu 6 years ago
parent
commit
9e9c5485fc
4 changed files with 36 additions and 4 deletions
  1. 18
      app/server/migrations/0003_shortcut.py
  2. 6
      app/server/models.py
  3. 6
      app/server/tests/test_models.py
  4. 10
      app/server/utils.py

18
app/server/migrations/0003_shortcut.py

@ -0,0 +1,18 @@
# Generated by Django 2.1.5 on 2019-02-06 02:00
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('server', '0002_document_metadata'),
]
operations = [
migrations.AlterField(
model_name='label',
name='shortcut',
field=models.CharField(blank=True, choices=[('a', 'a'), ('b', 'b'), ('c', 'c'), ('d', 'd'), ('e', 'e'), ('f', 'f'), ('g', 'g'), ('h', 'h'), ('i', 'i'), ('j', 'j'), ('k', 'k'), ('l', 'l'), ('m', 'm'), ('n', 'n'), ('o', 'o'), ('p', 'p'), ('q', 'q'), ('r', 'r'), ('s', 's'), ('t', 't'), ('u', 'u'), ('v', 'v'), ('w', 'w'), ('x', 'x'), ('y', 'y'), ('z', 'z'), ('ctrl a', 'ctrl a'), ('ctrl b', 'ctrl b'), ('ctrl c', 'ctrl c'), ('ctrl d', 'ctrl d'), ('ctrl e', 'ctrl e'), ('ctrl f', 'ctrl f'), ('ctrl g', 'ctrl g'), ('ctrl h', 'ctrl h'), ('ctrl i', 'ctrl i'), ('ctrl j', 'ctrl j'), ('ctrl k', 'ctrl k'), ('ctrl l', 'ctrl l'), ('ctrl m', 'ctrl m'), ('ctrl n', 'ctrl n'), ('ctrl o', 'ctrl o'), ('ctrl p', 'ctrl p'), ('ctrl q', 'ctrl q'), ('ctrl r', 'ctrl r'), ('ctrl s', 'ctrl s'), ('ctrl t', 'ctrl t'), ('ctrl u', 'ctrl u'), ('ctrl v', 'ctrl v'), ('ctrl w', 'ctrl w'), ('ctrl x', 'ctrl x'), ('ctrl y', 'ctrl y'), ('ctrl z', 'ctrl z'), ('shift a', 'shift a'), ('shift b', 'shift b'), ('shift c', 'shift c'), ('shift d', 'shift d'), ('shift e', 'shift e'), ('shift f', 'shift f'), ('shift g', 'shift g'), ('shift h', 'shift h'), ('shift i', 'shift i'), ('shift j', 'shift j'), ('shift k', 'shift k'), ('shift l', 'shift l'), ('shift m', 'shift m'), ('shift n', 'shift n'), ('shift o', 'shift o'), ('shift p', 'shift p'), ('shift q', 'shift q'), ('shift r', 'shift r'), ('shift s', 'shift s'), ('shift t', 'shift t'), ('shift u', 'shift u'), ('shift v', 'shift v'), ('shift w', 'shift w'), ('shift x', 'shift x'), ('shift y', 'shift y'), ('shift z', 'shift z'), ('ctrl shift a', 'ctrl shift a'), ('ctrl shift b', 'ctrl shift b'), ('ctrl shift c', 'ctrl shift c'), ('ctrl shift d', 'ctrl shift d'), ('ctrl shift e', 'ctrl shift e'), ('ctrl shift f', 'ctrl shift f'), ('ctrl shift g', 'ctrl shift g'), ('ctrl shift h', 'ctrl shift h'), ('ctrl shift i', 'ctrl shift i'), ('ctrl shift j', 'ctrl shift j'), ('ctrl shift k', 'ctrl shift k'), ('ctrl shift l', 'ctrl shift l'), ('ctrl shift m', 'ctrl shift m'), ('ctrl shift n', 'ctrl shift n'), ('ctrl shift o', 'ctrl shift o'), ('ctrl shift p', 'ctrl shift p'), ('ctrl shift q', 'ctrl shift q'), ('ctrl shift r', 'ctrl shift r'), ('ctrl shift s', 'ctrl shift s'), ('ctrl shift t', 'ctrl shift t'), ('ctrl shift u', 'ctrl shift u'), ('ctrl shift v', 'ctrl shift v'), ('ctrl shift w', 'ctrl shift w'), ('ctrl shift x', 'ctrl shift x'), ('ctrl shift y', 'ctrl shift y'), ('ctrl shift z', 'ctrl shift z'), ('', '')], max_length=15, null=True),
),
]

6
app/server/models.py

@ -1,10 +1,10 @@
import json import json
import string
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.db import models from django.db import models
from django.urls import reverse from django.urls import reverse
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.staticfiles.storage import staticfiles_storage from django.contrib.staticfiles.storage import staticfiles_storage
from .utils import get_key_choices
class Project(models.Model): class Project(models.Model):
@ -120,11 +120,11 @@ class Project(models.Model):
class Label(models.Model): class Label(models.Model):
KEY_CHOICES = ((u, c) for u, c in zip(string.ascii_lowercase, string.ascii_lowercase))
KEY_CHOICES = get_key_choices()
COLOR_CHOICES = () COLOR_CHOICES = ()
text = models.CharField(max_length=100) text = models.CharField(max_length=100)
shortcut = models.CharField(max_length=10, choices=KEY_CHOICES)
shortcut = models.CharField(max_length=15, blank=True, null=True, choices=KEY_CHOICES)
project = models.ForeignKey(Project, related_name='labels', on_delete=models.CASCADE) project = models.ForeignKey(Project, related_name='labels', on_delete=models.CASCADE)
background_color = models.CharField(max_length=7, default='#209cee') background_color = models.CharField(max_length=7, default='#209cee')
text_color = models.CharField(max_length=7, default='#ffffff') text_color = models.CharField(max_length=7, default='#ffffff')

6
app/server/tests/test_models.py

@ -21,11 +21,15 @@ class TestProject(TestCase):
class TestLabel(TestCase): class TestLabel(TestCase):
def test_shortcut_uniqueness(self): def test_shortcut_uniqueness(self):
label = mixer.blend('server.Label')
label = mixer.blend('server.Label', shortcut='a')
mixer.blend('server.Label', shortcut=label.shortcut) mixer.blend('server.Label', shortcut=label.shortcut)
with self.assertRaises(IntegrityError): with self.assertRaises(IntegrityError):
Label(project=label.project, shortcut=label.shortcut).save() Label(project=label.project, shortcut=label.shortcut).save()
def test_create_none_shortcut(self):
label = mixer.blend('server.Label', shortcut=None)
self.assertEqual(label.shortcut, None)
def test_text_uniqueness(self): def test_text_uniqueness(self):
label = mixer.blend('server.Label') label = mixer.blend('server.Label')
mixer.blend('server.Label', text=label.text) mixer.blend('server.Label', text=label.text)

10
app/server/utils.py

@ -0,0 +1,10 @@
import string
def get_key_choices():
selectKey, shortKey = [c for c in string.ascii_lowercase], [c for c in string.ascii_lowercase]
checkKey = 'ctrl shift'
shortKey += [ck + ' ' + sk for ck in checkKey.split() for sk in selectKey]
shortKey += [checkKey + ' ' + sk for sk in selectKey]
shortKey += ['']
KEY_CHOICES = ((u, c) for u, c in zip(shortKey, shortKey))
return KEY_CHOICES
Loading…
Cancel
Save