forked from getpatchwork/patchwork
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solve getpatchwork#113 and getpatchwork#57 GitHub issues, fix up returning tags in the API, keep track of tag origin to later be able to add tags to comments in the API. Use relations Tag-Patch and Tag-CoverLetter to avoid duplication of tags for each patch in series. Signed-off-by: Veronika Kabatova <vkabatov@redhat.com>
- Loading branch information
Showing
11 changed files
with
307 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
# -*- coding: utf-8 -*- | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
import re | ||
|
||
|
||
# Django migrations don't allow us to call models' methods because the | ||
# migration will break if the methods change. Therefore we need to use an | ||
# altered copy of all the code needed. | ||
def extract_tags(extract_from, tags): | ||
found_tags = {} | ||
|
||
if not extract_from.content: | ||
return found_tags | ||
|
||
for tag in tags: | ||
regex = re.compile(tag.pattern + r'\s(.*)', re.M | re.I) | ||
found_tags[tag] = regex.findall(extract_from.content) | ||
|
||
return found_tags | ||
|
||
|
||
def add_tags(apps, submission, tag, values, comment=None): | ||
if not values: | ||
# We don't need to delete tags since none exist yet and we can't | ||
# delete comments etc. during the migration | ||
return | ||
|
||
SubmissionTag = apps.get_model('patchwork', 'SubmissionTag') | ||
SubmissionTag.objects.bulk_create([SubmissionTag( | ||
submission=submission, | ||
tag=tag, | ||
value=value, | ||
comment=comment | ||
) for value in values]) | ||
|
||
|
||
def create_all(apps, schema_editor): | ||
Tag = apps.get_model('patchwork', 'Tag') | ||
tags = Tag.objects.all() | ||
|
||
Submission = apps.get_model('patchwork', 'Submission') | ||
for submission in Submission.objects.all(): | ||
extracted = extract_tags(submission, tags) | ||
for tag in extracted: | ||
add_tags(apps, submission, tag, extracted[tag]) | ||
|
||
Comment = apps.get_model('patchwork', 'Comment') | ||
for comment in Comment.objects.all(): | ||
extracted = extract_tags(comment, tags) | ||
for tag in extracted: | ||
add_tags(apps, | ||
comment.submission, | ||
tag, | ||
extracted[tag], | ||
comment=comment) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('patchwork', '0025_add_regex_validators'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='SubmissionTag', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, | ||
primary_key=True, | ||
serialize=False, | ||
verbose_name='ID')), | ||
('value', models.CharField(max_length=255)), | ||
('comment', models.ForeignKey( | ||
to='patchwork.Comment' | ||
null=True | ||
)), | ||
('submission', models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
to='patchwork.Submission' | ||
)), | ||
('tag', models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
to='patchwork.Tag' | ||
)), | ||
], | ||
), | ||
migrations.AlterUniqueTogether( | ||
name='patchtag', | ||
unique_together=set([]), | ||
), | ||
migrations.RemoveField( | ||
model_name='patchtag', | ||
name='patch', | ||
), | ||
migrations.RemoveField( | ||
model_name='patchtag', | ||
name='tag', | ||
), | ||
migrations.RemoveField( | ||
model_name='patch', | ||
name='tags', | ||
), | ||
migrations.DeleteModel( | ||
name='PatchTag', | ||
), | ||
migrations.AddField( | ||
model_name='submission', | ||
name='related_tags', | ||
field=models.ManyToManyField( | ||
through='patchwork.SubmissionTag', | ||
to='patchwork.Tag' | ||
), | ||
), | ||
migrations.AlterUniqueTogether( | ||
name='submissiontag', | ||
unique_together=set([('submission', | ||
'tag', | ||
'value', | ||
'comment')]), | ||
), | ||
migrations.RunPython(create_all, atomic=False), | ||
] |
Oops, something went wrong.