-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
3eb32ca
commit 80e28ad
Showing
13 changed files
with
412 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import json | ||
from json import JSONDecodeError | ||
|
||
from django.core.serializers.json import DjangoJSONEncoder | ||
from django.db import models | ||
from django.contrib.postgres.fields import JSONField as PostgresJSONField | ||
|
||
|
||
class JSONField(PostgresJSONField): | ||
def __init__(self, encoder=DjangoJSONEncoder, **options): | ||
super().__init__(encoder=encoder, **options) | ||
|
||
def from_db_value(self, value, expression, connection): | ||
if isinstance(value, str): | ||
# TODO test datetime saving `object_hook` | ||
# TODO most likely we would have to add some guessing for the types encoded by DjangoJSONEncoder | ||
try: | ||
return json.loads(value) | ||
except JSONDecodeError as e: | ||
# TODO this should be handled by validation | ||
e.msg += ': ' + value[max(0, e.pos - 10):e.pos] + '>here>' + value[e.pos:min(e.pos + 10, len(value))] | ||
|
||
return value | ||
|
||
|
||
# TODO delete when squashing migrations | ||
class JSONTextField(models.TextField): | ||
""" | ||
JSONField is a generic textfield that neatly serializes/unserializes | ||
JSON objects seamlessly. | ||
Django snippet #1478, Credit: https://stackoverflow.com/a/41839021/803174 | ||
example: | ||
class Page(models.Model): | ||
data = JSONField(blank=True, null=True) | ||
page = Page.objects.get(pk=5) | ||
page.data = {'title': 'test', 'type': 3} | ||
page.save() | ||
""" | ||
|
||
# TODO in Django Admin the value is shown as serialized dict with single quotes, it should have been json | ||
def to_python(self, value): | ||
if value == "": | ||
return None | ||
|
||
if isinstance(value, str): | ||
# TODO test datetime saving `object_hook` | ||
# TODO most likely we would have to add some guessing for the types encoded by DjangoJSONEncoder | ||
try: | ||
return json.loads(value) | ||
except JSONDecodeError as e: | ||
# TODO this should be handled by validation | ||
e.msg += ': ' + value[max(0, e.pos - 10):e.pos] + '>here>' + value[e.pos:min(e.pos + 10, len(value))] | ||
|
||
return value | ||
|
||
def to_json(self, value): | ||
if value == "": | ||
return None | ||
if isinstance(value, dict): | ||
value = json.dumps(value, cls=DjangoJSONEncoder) | ||
return value | ||
|
||
def from_db_value(self, value, *args): | ||
return self.to_python(value) | ||
|
||
def value_to_string(self, obj): | ||
# Used by seralization framework: manage.py dumpdata | ||
value = self.value_from_object(obj) | ||
|
||
return str(self.to_json(value)) | ||
|
||
def get_prep_value(self, value): | ||
# Use by lookups (equals to, etc.) | ||
value = super().get_prep_value(value) | ||
|
||
if isinstance(value, dict): | ||
return self.to_json(value) | ||
|
||
return self.to_python(value) | ||
|
||
# def get_db_prep_save(self, value, *args, **kwargs): | ||
# return self.to_json(value) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Generated by Django 2.2.5 on 2019-11-14 16:40 | ||
|
||
import django.core.serializers.json | ||
from django.db import migrations | ||
import moonsheep.json_field | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('moonsheep', '0004_auto_20191106_1758'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='entry', | ||
name='data2', | ||
field=moonsheep.json_field.JSONField(default={}, encoder=django.core.serializers.json.DjangoJSONEncoder), | ||
preserve_default=False, | ||
), | ||
migrations.AddField( | ||
model_name='task', | ||
name='params2', | ||
field=moonsheep.json_field.JSONField(default={}, encoder=django.core.serializers.json.DjangoJSONEncoder), | ||
preserve_default=False, | ||
), | ||
migrations.AlterField( | ||
model_name='entry', | ||
name='data', | ||
field=moonsheep.json_field.JSONTextField(), | ||
), | ||
migrations.AlterField( | ||
model_name='task', | ||
name='params', | ||
field=moonsheep.json_field.JSONTextField(blank=True), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Generated by Django 2.2.5 on 2019-11-14 16:42 | ||
|
||
from django.db import migrations | ||
|
||
def copy_json_fields(apps, schema_editor): | ||
# We can't import the Person model directly as it may be a newer | ||
# version than this migration expects. We use the historical version. | ||
Task = apps.get_model('moonsheep', 'Task') | ||
for t in Task.objects.all(): | ||
t.params2 = t.params | ||
t.save() | ||
|
||
Entry = apps.get_model('moonsheep', 'Entry') | ||
for e in Entry.objects.all(): | ||
e.data2 = e.data | ||
e.save() | ||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('moonsheep', '0005_auto_20191114_1640'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(copy_json_fields), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Generated by Django 2.2.5 on 2019-11-14 16:48 | ||
|
||
from django.db import migrations | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('moonsheep', '0006_auto_20191114_1642'), | ||
] | ||
|
||
operations = [ | ||
migrations.RemoveField( | ||
model_name='entry', | ||
name='data', | ||
), | ||
migrations.RemoveField( | ||
model_name='task', | ||
name='params', | ||
), | ||
migrations.RenameField( | ||
model_name='entry', | ||
old_name='data2', | ||
new_name='data' | ||
), | ||
migrations.RenameField( | ||
model_name='task', | ||
old_name='params2', | ||
new_name='params', | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Generated by Django 2.2.5 on 2019-11-14 16:53 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('moonsheep', '0007_auto_20191114_1648'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='task', | ||
name='parent', | ||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='children', to='moonsheep.Task'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
function support_manual_verification(data) { | ||
for (let [fld, values] of Object.entries(data)) { | ||
let input = jQuery(`input[name="${fld}"]`) | ||
if (values.length == 1) { | ||
// set chosen value | ||
input.val(values[0]) | ||
// color it | ||
input.css('outline', 'lime solid 3px') // TODO set class | ||
|
||
} else if (values.length > 0) { | ||
// color it | ||
input.css('outline', 'orangered solid 3px') // TODO set class | ||
// set hover / popup | ||
// TODO https://www.w3schools.com/css/css_tooltip.asp | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.