Refactor tag crud operations

This commit is contained in:
Peter Stockings
2023-12-09 17:19:49 +11:00
parent 4da03ad2ad
commit fd6ca240ee
4 changed files with 20 additions and 23 deletions

20
db.py
View File

@@ -268,10 +268,10 @@ class DataBase():
def get_tags_for_person(self, person_id): def get_tags_for_person(self, person_id):
return self.execute(""" return self.execute("""
SELECT SELECT
T.tag_id AS "TagId", T.tag_id as "tag_id",
T.person_id AS "PersonId", T.person_id as "person_id",
T.name AS "TagName", T.name AS "tag_name",
T.filter AS "TagFilter" T.filter AS "tag_filter"
FROM FROM
Tag T Tag T
WHERE WHERE
@@ -298,10 +298,10 @@ class DataBase():
def get_tags_for_dashboard(self): def get_tags_for_dashboard(self):
return self.execute(""" return self.execute("""
SELECT SELECT
T.tag_id AS "TagId", T.tag_id AS "tag_id",
T.person_id AS "PersonId", T.person_id AS "person_id",
T.name AS "TagName", T.name AS "tag_name",
T.filter AS "TagFilter" T.filter AS "tag_filter"
FROM FROM
Tag T Tag T
WHERE WHERE
@@ -311,12 +311,12 @@ class DataBase():
def add_or_update_tag_for_dashboard(self, tag_name, tag_filter): def add_or_update_tag_for_dashboard(self, tag_name, tag_filter):
# check if a tag exists for dashboard with the same tag_name # check if a tag exists for dashboard with the same tag_name
tag = self.execute('SELECT tag_id AS "TagId" FROM Tag WHERE person_id IS NULL AND name=%s LIMIT 1', [ tag = self.execute('SELECT tag_id AS "tag_id" FROM Tag WHERE person_id IS NULL AND name=%s LIMIT 1', [
tag_name], one=True) tag_name], one=True)
if tag: if tag:
# update the tag # update the tag
self.execute('UPDATE Tag SET filter=%s WHERE tag_id=%s', [ self.execute('UPDATE Tag SET filter=%s WHERE tag_id=%s', [
tag_filter, tag['TagId']], commit=True) tag_filter, tag['tag_id']], commit=True)
else: else:
self.execute('INSERT INTO Tag (name, filter) VALUES (%s, %s)', [ self.execute('INSERT INTO Tag (name, filter) VALUES (%s, %s)', [
tag_name, tag_filter], commit=True) tag_name, tag_filter], commit=True)

View File

@@ -91,9 +91,8 @@
</div> </div>
</div> </div>
</div> </div>
{% with person_id=None, tags=tags %}
{% include 'partials/tags.html' %} {{ render_partial('partials/tags.html',person_id=None, tags=tags) }}
{% endwith %}
</div> </div>

View File

@@ -5,18 +5,18 @@
class="[word-wrap: break-word] my-[5px] mr-4 flex h-[32px] cursor-pointer items-center justify-between rounded-[16px] border border-[#9fa6b2] bg-[#eceff1] bg-[transparent] py-0 px-[12px] text-[13px] font-normal normal-case leading-loose text-[#4f4f4f] shadow-none transition-[opacity] duration-300 ease-linear hover:border-[#9fa6b2] hover:!shadow-none dark:text-neutral-200" class="[word-wrap: break-word] my-[5px] mr-4 flex h-[32px] cursor-pointer items-center justify-between rounded-[16px] border border-[#9fa6b2] bg-[#eceff1] bg-[transparent] py-0 px-[12px] text-[13px] font-normal normal-case leading-loose text-[#4f4f4f] shadow-none transition-[opacity] duration-300 ease-linear hover:border-[#9fa6b2] hover:!shadow-none dark:text-neutral-200"
data-te-ripple-color="dark"> data-te-ripple-color="dark">
<span hx-get="{{ url_for('goto_tag') }}" {% if person_id %} <span hx-get="{{ url_for('goto_tag') }}" {% if person_id %}
hx-vals='{"filter": "{{ t["TagFilter"] }}", "person_id": "{{ person_id }}"}' {% else %} hx-vals='{"filter": "{{ t["tag_filter"] }}", "person_id": "{{ person_id }}"}' {% else %}
hx-vals='{"filter": "{{ t["TagFilter"] }}"}' {% endif%} hx-target="#container" hx-push-url="true">{{ hx-vals='{"filter": "{{ t["tag_filter"] }}"}' {% endif%} hx-target="#container" hx-push-url="true">{{
t['TagName'] }}</span> t['tag_name'] }}</span>
<span <span
class="float-right w-4 cursor-pointer pl-[8px] text-[16px] text-[#afafaf] opacity-[.53] transition-all duration-200 ease-in-out hover:text-[#8b8b8b] dark:text-neutral-400 dark:hover:text-neutral-100" class="float-right w-4 cursor-pointer pl-[8px] text-[16px] text-[#afafaf] opacity-[.53] transition-all duration-200 ease-in-out hover:text-[#8b8b8b] dark:text-neutral-400 dark:hover:text-neutral-100"
hx-get="{{ url_for('delete_tag', tag_id=t['TagId']) }}" {% if person_id %} hx-get="{{ url_for('delete_tag', tag_id=t['tag_id']) }}" {% if person_id %}
hx-vals='{"filter": "{{ t["TagFilter"] }}", "person_id": "{{ person_id }}"}' {% else %} hx-vals='{"filter": "{{ t["tag_filter"] }}", "person_id": "{{ person_id }}"}' {% else %}
hx-vals='{"filter": "{{ t["TagFilter"] }}"}' {% endif%} hx-target="#container" hx-push-url="true" _="on htmx:confirm(issueRequest) hx-vals='{"filter": "{{ t["tag_filter"] }}"}' {% endif%} hx-target="#container" hx-push-url="true" _="on htmx:confirm(issueRequest)
halt the event halt the event
call Swal.fire({title: 'Confirm', text:'Are you sure you want to delete {{ t['TagName'] }} tag?'}) call Swal.fire({title: 'Confirm', text:'Are you sure you want to delete {{ t['tag_name'] }} tag?'})
if result.isConfirmed issueRequest()"> if result.isConfirmed issueRequest()">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5"
stroke="currentColor" class="h-3 w-3"> stroke="currentColor" class="h-3 w-3">

View File

@@ -98,9 +98,7 @@
</div> </div>
</div> </div>
{% with person_id=person['PersonId'], tags=tags %} {{ render_partial('partials/tags.html',person_id=person['PersonId'], tags=tags) }}
{% include 'partials/tags.html' %}
{% endwith %}
<div class="flex flex-col mt-3 w-screen sm:w-full overflow-auto"> <div class="flex flex-col mt-3 w-screen sm:w-full overflow-auto">
<div class="overflow-x-auto rounded-lg"> <div class="overflow-x-auto rounded-lg">