Skip to content

Commit

Permalink
feat: Add journalize function in tasks collector
Browse files Browse the repository at this point in the history
  • Loading branch information
dragonee committed Oct 12, 2024
1 parent a10d4ef commit 852cec3
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
3 changes: 2 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,5 @@
- [ ] Journal with reflections from daily reflection from past 7 days
- [ ] Split/merge observations
- [ ] fixme: seems like situation field needs to be validated for changes with strip()
- [ ] + add a migration removing recontextualized
- [ ] + add a migration removing recontextualized
- [x] create a view that can migrate all observation updates into journal
18 changes: 18 additions & 0 deletions tasks/apps/tree/observation_operations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.db import transaction

from .models import JournalAdded, Event


def migrate_observation_updates_to_journal(observation, thread_id):
with transaction.atomic():
for update in observation.observationupdated_set.all():
JournalAdded.objects.create(
published=update.published,
thread_id=thread_id,
comment=update.comment,
)

for event in Event.objects.filter(event_stream_id=observation.event_stream_id):
event.delete()

observation.delete()
1 change: 1 addition & 0 deletions tasks/apps/tree/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
re_path(r'^observations/(?P<observation_id>[a-f0-9\-]+)/$', views.observation_edit, name='public-observation-edit'),
path('observations/', views.ObservationListView.as_view(), name='public-observation-list'),
path('observations/closed/', views.ObservationClosedListView.as_view(), name='public-observation-list-closed'),
path('observations/<int:observation_id>/journalize/', views.migrate_observation_updates_to_journal, name='public-observation-journalize'),
path('diary/<int:year>/<int:month>/', views.JournalArchiveMonthView.as_view(month_format="%m"), name='public-diary-archive-month'),
path('diary/', views.JournalCurrentMonthArchiveView.as_view(month_format="%m"), name='public-diary-archive-current-month'),
path('events/', views.EventCurrentMonthArchiveView.as_view(month_format="%m"), name='public-event-archive-current-month'),
Expand Down
19 changes: 19 additions & 0 deletions tasks/apps/tree/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

from .utils.itertools import itemize

from .observation_operations import migrate_observation_updates_to_journal as _migrate_observation_updates_to_journal

class ObservationPagination(PageNumberPagination):
page_size = 10
Expand Down Expand Up @@ -822,3 +823,21 @@ def get_context_data(self, **kwargs):

class HabitListView(ListView):
model = Habit



@api_view(['POST'])
def migrate_observation_updates_to_journal(request, observation_id):
observation = get_object_or_404(Observation, pk=observation_id)
thread = Thread.objects.get(name='Daily')

_migrate_observation_updates_to_journal(observation, thread.id)

if request.htmx:
response = RestResponse({'ok': True}, status=status.HTTP_200_OK)
response['HX-Redirect'] = reverse('public-observation-list')

return response

return redirect(reverse('public-observation-list'))

3 changes: 3 additions & 0 deletions tasks/templates/tree/observation_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ <h3>Updates</h3>
<button class="close" hx-post="{% url 'public-observation-close' instance.pk %}">
Close observation
</button>
<button class="close" hx-post="{% url 'public-observation-journalize' instance.pk %}">
Convert to journal
</button>
</div>
{% endif %}
</div>
Expand Down

0 comments on commit 852cec3

Please sign in to comment.