From 77e1e126c309fcc4df2700f2834eddd1c98186c4 Mon Sep 17 00:00:00 2001 From: roseva1 Date: Mon, 13 Nov 2023 15:00:56 -0500 Subject: [PATCH] batch bookmark model added --- turkle/admin.py | 1 - turkle/models.py | 5 ++++ turkle/templates/turkle/base.html | 5 +++- turkle/templates/turkle/index.html | 40 +++++++++++++++++++++++++++ turkle/views.py | 44 +++++++++++++++++++++++++++--- 5 files changed, 89 insertions(+), 6 deletions(-) diff --git a/turkle/admin.py b/turkle/admin.py index 146e413..3464758 100644 --- a/turkle/admin.py +++ b/turkle/admin.py @@ -27,7 +27,6 @@ from django.urls import path, reverse from django.utils import timezone from django.utils.html import format_html, format_html_join -from django.utils.translation import ngettext from guardian.admin import GuardedModelAdmin from guardian.shortcuts import (assign_perm, get_groups_with_perms, get_users_with_perms, remove_perm) diff --git a/turkle/models.py b/turkle/models.py index 089fb74..e97b0b0 100644 --- a/turkle/models.py +++ b/turkle/models.py @@ -938,3 +938,8 @@ def completed_assignments(self): def most_recent(self): return self.last_finished_time most_recent.admin_order_field = 'last_finished_time' + +class Bookmark(models.Model): + user = models.ForeignKey(User, on_delete=models.CASCADE) + batch = models.ForeignKey(Batch, on_delete=models.CASCADE) + bookmarked = models.BooleanField(default=False) \ No newline at end of file diff --git a/turkle/templates/turkle/base.html b/turkle/templates/turkle/base.html index 25e0067..c43b602 100644 --- a/turkle/templates/turkle/base.html +++ b/turkle/templates/turkle/base.html @@ -62,5 +62,8 @@ {% block footer %} {% endblock %} + + {% block script %} + {% endblock %} - + \ No newline at end of file diff --git a/turkle/templates/turkle/index.html b/turkle/templates/turkle/index.html index 06dd2a4..699f0b9 100644 --- a/turkle/templates/turkle/index.html +++ b/turkle/templates/turkle/index.html @@ -39,6 +39,7 @@ Project Batch Batch Published + Batch Bookmarked Tasks Available @@ -48,6 +49,9 @@ {{ batch_row.project_name }} {{ batch_row.batch_name }} {{ batch_row.batch_published }} + + + {{ batch_row.assignments_available }} @@ -74,3 +78,39 @@

{% endblock %} + +{% block script %} + + +{% endblock %} \ No newline at end of file diff --git a/turkle/views.py b/turkle/views.py index 7e74c36..36290e1 100644 --- a/turkle/views.py +++ b/turkle/views.py @@ -5,7 +5,7 @@ import urllib from django.conf import settings -from django.contrib import messages +from django.contrib import messages, auth from django.contrib.auth import get_user_model from django.core.exceptions import ObjectDoesNotExist from django.db import connection, transaction @@ -17,13 +17,12 @@ from django.utils.dateparse import parse_date from django.utils.datastructures import MultiValueDictKeyError -from .models import Task, TaskAssignment, Batch, Project +from .models import Task, TaskAssignment, Batch, Project, Bookmark, User User = get_user_model() logger = logging.getLogger(__name__) - def handle_db_lock(func): """Decorator that catches database lock errors from sqlite""" @wraps(func) @@ -41,6 +40,12 @@ def wrapper(request, *args, **kwargs): return wrapper +def get_user(request): + if not hasattr(request, '_cached_user'): + request._cached_user = auth.get_user(request) + return request._cached_user + + def index(request): """ Security behavior: @@ -63,21 +68,52 @@ def index(request): available_task_counts = Batch.available_task_counts_for(batch_query, request.user) + if request.POST: + def process_bookmark(query_dict): + """Create or update bookmark status for a batch & user + """ + batch_name = query_dict.get('rowId') + bookmark_status = query_dict.get('bookmarked') == 'true' + batch = Batch.objects.get(name=batch_name) + bookmark, created = Bookmark.objects.get_or_create( + user=get_user(request), + batch=batch, + defaults={'bookmarked': bookmark_status} + ) + if not created: + bookmark.bookmarked = bookmark_status + bookmark.save() + + if request.POST: + print(request.POST) + if 'bookmarking' in request.POST.get('action'): + process_bookmark(request.POST) + batch_rows = [] for batch in batch_query.values('created_at', 'id', 'name', 'project__name'): - total_tasks_available = available_task_counts[batch['id']] + def get_bookmark_status(batch): + """Access batch bookmark status for a user + """ + bookmark = Bookmark.objects.filter( + user=get_user(request), + batch__name=batch['name'] + ).values_list('bookmarked', flat=True).first() + return 'checked' if bookmark else '' + total_tasks_available = available_task_counts[batch['id']] if total_tasks_available > 0: batch_rows.append({ 'project_name': batch['project__name'], 'batch_name': batch['name'], 'batch_published': batch['created_at'], 'assignments_available': total_tasks_available, + 'selected': get_bookmark_status(batch), 'preview_next_task_url': reverse('preview_next_task', kwargs={'batch_id': batch['id']}), 'accept_next_task_url': reverse('accept_next_task', kwargs={'batch_id': batch['id']}) }) + return render(request, 'turkle/index.html', { 'abandoned_assignments': abandoned_assignments, 'batch_rows': batch_rows