Applied black

This commit is contained in:
Paul Konstantin Gerke 2022-02-27 20:36:31 +01:00
parent 3ac21ed67b
commit c665ebb181
5 changed files with 97 additions and 44 deletions

View File

@ -6,9 +6,7 @@ from rest_framework.exceptions import ValidationError
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.utils import timezone from django.utils import timezone
from taggit.serializers import ( from taggit.serializers import TagListSerializerField, TaggitSerializer
TagListSerializerField, TaggitSerializer
)
from core import models from core import models
@ -203,6 +201,7 @@ class BMISerializer(CoreModelSerializer):
model = models.BMI model = models.BMI
fields = ("id", "child", "bmi", "date", "notes") fields = ("id", "child", "bmi", "date", "notes")
class TagsSerializer(serializers.HyperlinkedModelSerializer): class TagsSerializer(serializers.HyperlinkedModelSerializer):
class Meta: class Meta:
model = models.BabyBuddyTag model = models.BabyBuddyTag

View File

@ -8,6 +8,7 @@ from core import models
from . import serializers, filters from . import serializers, filters
from .mixins import TimerFieldSupportMixin from .mixins import TimerFieldSupportMixin
class ChildViewSet(viewsets.ModelViewSet): class ChildViewSet(viewsets.ModelViewSet):
queryset = models.Child.objects.all() queryset = models.Child.objects.all()
serializer_class = serializers.ChildSerializer serializer_class = serializers.ChildSerializer

View File

@ -178,9 +178,11 @@ class WeightAdmin(ImportExportMixin, ExportActionMixin, admin.ModelAdmin):
) )
resource_class = WeightImportExportResource resource_class = WeightImportExportResource
class BabyBuddyTaggedItemInline(admin.StackedInline): class BabyBuddyTaggedItemInline(admin.StackedInline):
model = models.BabyBuddyTagged model = models.BabyBuddyTagged
@admin.register(models.BabyBuddyTag) @admin.register(models.BabyBuddyTag)
class BabyBuddyTagAdmin(admin.ModelAdmin): class BabyBuddyTagAdmin(admin.ModelAdmin):
inlines = [BabyBuddyTaggedItemInline] inlines = [BabyBuddyTaggedItemInline]

View File

@ -11,40 +11,96 @@ import taggit.managers
class Migration(migrations.Migration): class Migration(migrations.Migration):
dependencies = [ dependencies = [
('contenttypes', '0002_remove_content_type_name'), ("contenttypes", "0002_remove_content_type_name"),
('core', '0018_bmi_headcircumference_height'), ("core", "0018_bmi_headcircumference_height"),
] ]
operations = [ operations = [
migrations.CreateModel( migrations.CreateModel(
name='BabyBuddyTag', name="BabyBuddyTag",
fields=[ fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), (
('name', models.CharField(max_length=100, unique=True, verbose_name='name')), "id",
('slug', models.SlugField(max_length=100, unique=True, verbose_name='slug')), models.AutoField(
('color', models.CharField(default=core.models.random_color, max_length=32, validators=[django.core.validators.RegexValidator('^#[0-9A-F]{6}$')], verbose_name='Color')), auto_created=True,
('last_used', models.DateTimeField(default=django.utils.timezone.now)), primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"name",
models.CharField(max_length=100, unique=True, verbose_name="name"),
),
(
"slug",
models.SlugField(max_length=100, unique=True, verbose_name="slug"),
),
(
"color",
models.CharField(
default=core.models.random_color,
max_length=32,
validators=[
django.core.validators.RegexValidator("^#[0-9A-F]{6}$")
],
verbose_name="Color",
),
),
("last_used", models.DateTimeField(default=django.utils.timezone.now)),
], ],
options={ options={
'verbose_name': 'Tag', "verbose_name": "Tag",
'verbose_name_plural': 'Tags', "verbose_name_plural": "Tags",
}, },
), ),
migrations.CreateModel( migrations.CreateModel(
name='BabyBuddyTagged', name="BabyBuddyTagged",
fields=[ fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), (
('object_id', models.IntegerField(db_index=True, verbose_name='object ID')), "id",
('content_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='%(app_label)s_%(class)s_tagged_items', to='contenttypes.contenttype', verbose_name='content type')), models.AutoField(
('tag', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='%(app_label)s_%(class)s_items', to='core.babybuddytag')), auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"object_id",
models.IntegerField(db_index=True, verbose_name="object ID"),
),
(
"content_type",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="%(app_label)s_%(class)s_tagged_items",
to="contenttypes.contenttype",
verbose_name="content type",
),
),
(
"tag",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="%(app_label)s_%(class)s_items",
to="core.babybuddytag",
),
),
], ],
options={ options={
'abstract': False, "abstract": False,
}, },
), ),
migrations.AddField( migrations.AddField(
model_name='note', model_name="note",
name='tags', name="tags",
field=taggit.managers.TaggableManager(blank=True, help_text='A comma-separated list of tags.', through='core.BabyBuddyTagged', to='core.BabyBuddyTag', verbose_name='Tags'), field=taggit.managers.TaggableManager(
blank=True,
help_text="A comma-separated list of tags.",
through="core.BabyBuddyTagged",
to="core.BabyBuddyTag",
verbose_name="Tags",
),
), ),
] ]

View File

@ -7,13 +7,12 @@ class TagsEditor(Widget):
class Media: class Media:
js = ("babybuddy/js/tags_editor.js",) js = ("babybuddy/js/tags_editor.js",)
input_type = "hidden"
input_type = 'hidden' template_name = "core/widget_tag_editor.html"
template_name = 'core/widget_tag_editor.html'
@staticmethod @staticmethod
def __unpack_tag(tag): def __unpack_tag(tag):
return {'name': tag.name, 'color': tag.color} return {"name": tag.name, "color": tag.color}
def format_value(self, value: Any) -> Optional[str]: def format_value(self, value: Any) -> Optional[str]:
if value is not None and not isinstance(value, str): if value is not None and not isinstance(value, str):
@ -22,33 +21,29 @@ class TagsEditor(Widget):
def build_attrs(self, base_attrs, extra_attrs=None): def build_attrs(self, base_attrs, extra_attrs=None):
attrs = super().build_attrs(base_attrs, extra_attrs) attrs = super().build_attrs(base_attrs, extra_attrs)
class_string = attrs.get('class', '') class_string = attrs.get("class", "")
class_string = class_string.replace("form-control", "") class_string = class_string.replace("form-control", "")
attrs['class'] = class_string + ' babybuddy-tags-editor' attrs["class"] = class_string + " babybuddy-tags-editor"
return attrs return attrs
def get_context(self, name: str, value: Any, attrs) -> Dict[str, Any]: def get_context(self, name: str, value: Any, attrs) -> Dict[str, Any]:
from . import models from . import models
most_tags = models.BabyBuddyTag.objects.order_by( most_tags = models.BabyBuddyTag.objects.order_by("-last_used").all()[:256]
'-last_used'
).all()[:256]
result = super().get_context(name, value, attrs) result = super().get_context(name, value, attrs)
tag_names = set(x['name'] for x in (result.get('widget', {}).get('value', None) or [])) tag_names = set(
quick_suggestion_tags = [ x["name"] for x in (result.get("widget", {}).get("value", None) or [])
t for t in most_tags )
if t.name not in tag_names quick_suggestion_tags = [t for t in most_tags if t.name not in tag_names][:5]
][:5]
result['widget']['tag_suggestions'] = { result["widget"]["tag_suggestions"] = {
'quick': [ "quick": [
self.__unpack_tag(t) for t in quick_suggestion_tags self.__unpack_tag(t)
for t in quick_suggestion_tags
if t.name not in tag_names if t.name not in tag_names
], ],
'most': [ "most": [self.__unpack_tag(t) for t in most_tags],
self.__unpack_tag(t) for t in most_tags
]
} }
return result return result