Fix MySQL migration (#57)

* Use CharField, not TextField for MySQL compat.

* black formatting
This commit is contained in:
Scot Hacker 2019-03-25 07:43:53 -07:00 committed by GitHub
parent 70cac8b4e9
commit f6d79879ae
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 21 deletions

View file

@ -41,13 +41,10 @@ def message_text(message):
def format_task_title(format_string, message): def format_task_title(format_string, message):
return format_string.format( return format_string.format(subject=message["subject"], author=message["from"])
subject=message["subject"],
author=message["from"],
)
DJANGO_TODO_THREAD = re.compile(r'<thread-(\d+)@django-todo>') DJANGO_TODO_THREAD = re.compile(r"<thread-(\d+)@django-todo>")
def parse_references(task_list, references): def parse_references(task_list, references):
@ -61,10 +58,7 @@ def parse_references(task_list, references):
continue continue
thread_id = int(match.group(1)) thread_id = int(match.group(1))
new_answer_thread = Task.objects.filter( new_answer_thread = Task.objects.filter(task_list=task_list, pk=thread_id).first()
task_list=task_list,
pk=thread_id
).first()
if new_answer_thread is not None: if new_answer_thread is not None:
answer_thread = new_answer_thread answer_thread = new_answer_thread
@ -97,20 +91,26 @@ def insert_message(task_list, message, priority, task_title_format):
f"[From: {message['from']}]" f"[From: {message['from']}]"
) )
message_id = message["message-id"] # Due to limitations in MySQL wrt unique_together and TextField (grrr),
# we must use a CharField rather than TextField for message_id.
# In the unlikeley event that we get a VERY long inbound
# message_id, truncate it to the max_length of a MySQL CharField.
original_message_id = message["message-id"]
message_id = (
(original_message_id[:252] + "...")
if len(original_message_id) > 255
else original_message_id
)
message_from = message["from"] message_from = message["from"]
text = message_text(message) text = message_text(message)
related_messages, answer_thread = \ related_messages, answer_thread = parse_references(task_list, message.get("references", ""))
parse_references(task_list, message.get("references", ""))
# find the most relevant task to add a comment on. # find the most relevant task to add a comment on.
# among tasks in the selected task list, find the task having the # among tasks in the selected task list, find the task having the
# most email comments the current message references # most email comments the current message references
best_task = ( best_task = (
Task.objects.filter( Task.objects.filter(task_list=task_list, comment__email_message_id__in=related_messages)
task_list=task_list, comment__email_message_id__in=related_messages
)
.annotate(num_comments=Count("comment")) .annotate(num_comments=Count("comment"))
.order_by("-num_comments") .order_by("-num_comments")
.only("id") .only("id")
@ -127,7 +127,7 @@ def insert_message(task_list, message, priority, task_title_format):
best_task = Task.objects.create( best_task = Task.objects.create(
priority=priority, priority=priority,
title=format_task_title(task_title_format, message), title=format_task_title(task_title_format, message),
task_list=task_list task_list=task_list,
) )
logger.info("using task: %r", best_task) logger.info("using task: %r", best_task)
@ -139,8 +139,9 @@ def insert_message(task_list, message, priority, task_title_format):
logger.info("created comment: %r", comment) logger.info("created comment: %r", comment)
def tracker_consumer(producer, group=None, task_list_slug=None, def tracker_consumer(
priority=1, task_title_format="[MAIL] {subject}"): producer, group=None, task_list_slug=None, priority=1, task_title_format="[MAIL] {subject}"
):
task_list = TaskList.objects.get(group__name=group, slug=task_list_slug) task_list = TaskList.objects.get(group__name=group, slug=task_list_slug)
for message in producer: for message in producer:
try: try:

View file

@ -1,4 +1,4 @@
# Generated by Django 2.1.4 on 2018-12-21 14:01 # Generated by Django 2.1.7 on 2019-03-24 22:50
from django.conf import settings from django.conf import settings
from django.db import migrations, models from django.db import migrations, models
@ -18,7 +18,7 @@ class Migration(migrations.Migration):
migrations.AddField( migrations.AddField(
model_name="comment", model_name="comment",
name="email_message_id", name="email_message_id",
field=models.TextField(blank=True, null=True), field=models.CharField(blank=True, max_length=255, null=True),
), ),
migrations.AlterField( migrations.AlterField(
model_name="comment", model_name="comment",

View file

@ -130,7 +130,7 @@ class Comment(models.Model):
task = models.ForeignKey(Task, on_delete=models.CASCADE) task = models.ForeignKey(Task, on_delete=models.CASCADE)
date = models.DateTimeField(default=datetime.datetime.now) date = models.DateTimeField(default=datetime.datetime.now)
email_from = models.CharField(max_length=320, blank=True, null=True) email_from = models.CharField(max_length=320, blank=True, null=True)
email_message_id = models.TextField(blank=True, null=True) email_message_id = models.CharField(max_length=255, blank=True, null=True)
body = models.TextField(blank=True) body = models.TextField(blank=True)