Skip to content
Snippets Groups Projects
Commit 34700549 authored by Lars Kruse's avatar Lars Kruse
Browse files

restore_tags: fix style issues by applying "black" formatting

parent 370ad05e
Branches
No related tags found
No related merge requests found
......@@ -9,13 +9,15 @@ from grouprise.features.content.models import Version
class Command(BaseCommand):
TAGS_PATTERN = rb'COPY public\.tags_tag.*?;\s*(.*?)\\\.'
LINKS_PATTERN = rb'COPY public\.tags_tagged.*?;\s*(.*?)\\\.'
TAGS_PATTERN = rb"COPY public\.tags_tag.*?;\s*(.*?)\\\."
LINKS_PATTERN = rb"COPY public\.tags_tagged.*?;\s*(.*?)\\\."
help = 'Restores tags from a grouprise 2.x database dump.'
help = "Restores tags from a grouprise 2.x database dump."
def add_arguments(self, parser):
parser.add_argument('dump_file', help='filename of version 2.x SQL database dump')
parser.add_argument(
"dump_file", help="filename of version 2.x SQL database dump"
)
def handle(self, *args, **options):
def get_data(pattern_str):
......@@ -23,37 +25,52 @@ class Command(BaseCommand):
match = pattern.search(mm)
if match:
for dataset in match[1].splitlines():
yield dataset.decode().split(sep='\t')
yield dataset.decode().split(sep="\t")
else:
raise CommandError('Dump file does not contain tag data')
raise CommandError("Dump file does not contain tag data")
dump_file = options.get('dump_file')
dump_file = options.get("dump_file")
with open(dump_file, 'r') as f:
with open(dump_file, "r") as f:
with mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) as mm:
tags = {int(data[0]): {'name': data[1], 'slug': data[2]}
for data in get_data(self.TAGS_PATTERN)}
links = [{'tagged': int(data[1]), 'tagged_type': int(data[2]), 'tag': int(data[3])}
for data in get_data(self.LINKS_PATTERN)]
tags = {
int(data[0]): {"name": data[1], "slug": data[2]}
for data in get_data(self.TAGS_PATTERN)
}
links = [
{
"tagged": int(data[1]),
"tagged_type": int(data[2]),
"tag": int(data[3]),
}
for data in get_data(self.LINKS_PATTERN)
]
for link in links:
content_type = ContentType.objects.get_for_id(link['tagged_type'])
content_type = ContentType.objects.get_for_id(link["tagged_type"])
try:
model = content_type.get_object_for_this_type(id=link['tagged'])
model = content_type.get_object_for_this_type(id=link["tagged"])
except:
print('Warning: Tagged object does no longer exist (type {}, id {})'
.format(link['tagged_type'], link['tagged']))
print(
"Warning: Tagged object does no longer exist (type {}, id {})".format(
link["tagged_type"], link["tagged"]
)
)
continue
if isinstance(model, Contribution):
model = model.container
elif isinstance(model, Version):
model = model.content
if hasattr(model, 'tags'):
tag_name = tags[link['tag']]['name']
if hasattr(model, "tags"):
tag_name = tags[link["tag"]]["name"]
if len(tag_name) <= 100:
model.tags.add(tag_name)
else:
print('Warning: Tag name is too long: {}'.format(tag_name))
print("Warning: Tag name is too long: {}".format(tag_name))
else:
print('Warning: Model of type {} is not taggable'.format(model.__class__.__name__))
print(
"Warning: Model of type {} is not taggable".format(
model.__class__.__name__
)
)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment