Complete open my2dos tasks

This commit is contained in:
rucki
2026-08-27 09:39:03 +02:00
parent 733568a33e
commit ee332a11a8
10 changed files with 178 additions and 13 deletions
+40 -1
View File
@@ -2,12 +2,42 @@ import re
import secrets
from django.conf import settings
from django.db import models
from django.db.models.signals import post_delete
from django.dispatch import receiver
from django.urls import reverse
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
TAG_RE = re.compile(r'(?<!\w)#([\wäöüÄÖÜß-]+)')
LINK_RE = re.compile(r'\[\[(todo|note|link|journal)?:?(\d+)\]\]', re.I)
FENCE_RE = re.compile(r'^\s*(`{3,}|~{3,})')
INLINE_CODE_RE = re.compile(r'(`+).*?\1', re.S)
def without_markdown_code(text):
"""Remove fenced and inline Markdown code before extracting metadata."""
visible_lines = []
fence = None
for line in (text or '').splitlines():
match = FENCE_RE.match(line)
if fence:
if match and match.group(1)[0] == fence[0] and len(match.group(1)) >= len(fence):
fence = None
visible_lines.append('')
elif match:
fence = match.group(1)
visible_lines.append('')
else:
visible_lines.append(line)
return INLINE_CODE_RE.sub('', '\n'.join(visible_lines))
def delete_unused_tags(tags=None):
"""Delete tags that are no longer used by items or tag-based features."""
candidates = Tag.objects.all() if tags is None else Tag.objects.filter(pk__in=[tag.pk for tag in tags])
candidates.filter(items__isnull=True, kanbans__isnull=True, api_keys__isnull=True).exclude(
name__in=Team.objects.values_list('slug', flat=True),
).delete()
class Tag(models.Model):
@@ -141,9 +171,12 @@ class Item(models.Model):
return self.kind == self.Kind.TODO and not self.is_done and self.due_at and timezone.localdate(self.due_at) == timezone.localdate()
def sync_metadata(self):
tag_names = {m.group(1).lower() for m in TAG_RE.finditer(self.content)}
old_tags = list(self.tags.all())
metadata_text = without_markdown_code(self.content)
tag_names = {m.group(1).lower() for m in TAG_RE.finditer(metadata_text)}
tags = [Tag.objects.get_or_create(name=name)[0] for name in tag_names]
self.tags.set(tags)
delete_unused_tags(old_tags)
ids = [int(m.group(2)) for m in LINK_RE.finditer(f'{self.content}\n{self.comment}')]
self.linked_items.set(Item.objects.filter(id__in=ids).exclude(id=self.id))
@@ -165,6 +198,7 @@ class Kanban(models.Model):
class UserPreference(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, related_name='preferences')
language = models.CharField(max_length=10, blank=True, default='', choices=[('', _('Browser default')), ('de', 'Deutsch'), ('en', 'English')])
default_item_kind = models.CharField(max_length=10, choices=Item.Kind.choices, default=Item.Kind.NOTE)
show_notes = models.BooleanField(default=True)
show_open_todos = models.BooleanField(default=True)
show_done_todos = models.BooleanField(default=False)
@@ -204,3 +238,8 @@ class Attachment(models.Model):
def __str__(self):
return self.file.name
@receiver(post_delete, sender=Item)
def clean_tags_after_item_delete(sender, instance, **kwargs):
delete_unused_tags()