Skip to content

Commit

Permalink
Add image field to note
Browse files Browse the repository at this point in the history
  • Loading branch information
cdubz committed Oct 20, 2023
1 parent ed18fc8 commit 941e4c7
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class Meta:
class NoteSerializer(CoreModelSerializer, TaggableSerializer):
class Meta:
model = models.Note
fields = ("id", "child", "note", "time", "tags")
fields = ("id", "child", "note", "image", "time", "tags")


class SleepSerializer(CoreModelWithDurationSerializer, TaggableSerializer):
Expand Down
1 change: 1 addition & 0 deletions api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ def test_get(self):
"id": 1,
"child": 1,
"note": "Fake note.",
"image": None,
"time": "2017-11-17T22:45:00-05:00",
"tags": [],
},
Expand Down
1 change: 1 addition & 0 deletions core/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ class HeightAdmin(ImportExportMixin, ExportActionMixin, admin.ModelAdmin):
class NoteImportExportResource(ImportExportResourceBase):
class Meta:
model = models.Note
exclude = ("image",)


@admin.register(models.Note)
Expand Down
2 changes: 2 additions & 0 deletions core/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ class NoteForm(CoreModelForm, TaggableModelForm):
class Meta:
model = models.Note
fields = ["child", "note", "time", "tags"]
if settings.BABY_BUDDY["ALLOW_UPLOADS"]:
fields.insert(2, "image")
widgets = {
"child": ChildRadioSelect,
"time": DateTimeInput(),
Expand Down
19 changes: 19 additions & 0 deletions core/migrations/0031_note_image.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.2.5 on 2023-10-20 23:37

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("core", "0030_weightpercentile_weightpercentile_unique_age_sex"),
]

operations = [
migrations.AddField(
model_name="note",
name="image",
field=models.ImageField(
blank=True, null=True, upload_to="notes/images/", verbose_name="Image"
),
),
]
3 changes: 3 additions & 0 deletions core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,9 @@ class Note(models.Model):
time = models.DateTimeField(
blank=False, default=timezone.localtime, verbose_name=_("Time")
)
image = models.ImageField(
blank=True, null=True, upload_to="notes/images/", verbose_name=_("Image")
)
tags = TaggableManager(blank=True, through=Tagged)

objects = models.Manager()
Expand Down
9 changes: 8 additions & 1 deletion core/templates/core/note_list.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends 'babybuddy/page.html' %}
{% load datetime i18n widget_tweaks %}
{% load datetime i18n imagekit widget_tweaks %}

{% block title %}{% trans "Notes" %}{% endblock %}

Expand All @@ -26,6 +26,7 @@ <h1>
{% if not unique_child %}
<th>{% trans "Child" %}</th>
{% endif %}
<th>{% trans "Image" %}</th>
<th>{% trans "Note" %}</th>
<th>{% trans "Tags" %}</th>
</tr>
Expand Down Expand Up @@ -54,6 +55,12 @@ <h1>
{% if not unique_child %}
<td><a href="{% url 'core:child' note.child.slug %}">{{ note.child }}</a></td>
{% endif %}
<td>
{% if note.image %}
{% thumbnail '40x40' note.image as thumb %}
<a href="{{ note.image.url }}" target="_blank"><img src="{{ thumb.url }}" class="img-fluid" /></a>

Check warning

Code scanning / CodeQL

Potentially unsafe external link Medium

External links without noopener/noreferrer are a potential security risk.
{% endif %}
</td>
<td>{{ note.note }}</td>
<td>
{% include "core/render_tag_list.html" with tags=note.tags.all %}
Expand Down

0 comments on commit 941e4c7

Please sign in to comment.