|
| 1 | +# Copyright The IETF Trust 2019, All Rights Reserved |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | + |
| 5 | +from __future__ import absolute_import, print_function, unicode_literals |
| 6 | + |
| 7 | +import os |
| 8 | + |
| 9 | +from collections import Counter |
| 10 | + |
| 11 | +from django.core.management.base import BaseCommand |
| 12 | + |
| 13 | +from ietf.doc.models import DocEvent |
| 14 | +from ietf.meeting.models import Meeting, SessionPresentation |
| 15 | +from ietf.person.models import Person |
| 16 | + |
| 17 | +from ietf.secr.proceedings.proc_utils import is_powerpoint, post_process |
| 18 | + |
| 19 | +class Command(BaseCommand): |
| 20 | + help = ('Fix uploaded_filename and generate pdf from pptx') |
| 21 | + |
| 22 | + def add_arguments(self, parser): |
| 23 | + parser.add_argument('--dry-run', action='store_true', dest='dry-run', default=False, help='Report on changes that would be made without making them') |
| 24 | + |
| 25 | + def handle(self, *args, **options): |
| 26 | + ietf105 = Meeting.objects.get(number=105) |
| 27 | + slides_path = os.path.join(ietf105.get_materials_path(),'slides') |
| 28 | + system_person = Person.objects.get(name="(System)") |
| 29 | + counts = Counter() |
| 30 | + |
| 31 | + for sp in SessionPresentation.objects.filter(session__meeting__number=105,document__type='slides'): #.filter(document__name='slides-105-manet-dlep-multicast-support-discussion'): |
| 32 | + slides = sp.document |
| 33 | + self.stderr.write("DEBUG: Processing %s" % slides) |
| 34 | + if not os.path.exists(os.path.join(slides_path,slides.uploaded_filename)): |
| 35 | + name, ext = os.path.splitext(slides.uploaded_filename) |
| 36 | + target_filename = '%s-%s%s' % (name[:name.rfind('-ss')], slides.rev,ext) |
| 37 | + if os.path.exists(os.path.join(slides_path,target_filename)): |
| 38 | + slides.uploaded_filename = target_filename |
| 39 | + if not options['dry-run']: |
| 40 | + e = DocEvent.objects.create(doc=slides, rev=slides.rev, by=system_person, type='changed_document', desc='Corrected uploaded_filename') |
| 41 | + slides.save_with_history([e]) |
| 42 | + counts['uploaded_filename repair succeeded'] += 1 |
| 43 | + |
| 44 | + else: |
| 45 | + self.stderr.write("Unable to repair %s" % slides) |
| 46 | + counts['uploaded_filename repair failed'] += 1 |
| 47 | + continue |
| 48 | + else: |
| 49 | + counts['uploaded_filename already ok'] += 1 |
| 50 | + |
| 51 | + if is_powerpoint(slides): |
| 52 | + base, _ = os.path.splitext(slides.uploaded_filename) |
| 53 | + if os.path.exists(os.path.join(slides_path,base+'.pdf')): |
| 54 | + self.stderr.write("PDF already exists for %s " % slides) |
| 55 | + counts['PDF already exists for a repaired file'] += 1 |
| 56 | + else: |
| 57 | + if not options['dry-run']: |
| 58 | + post_process(slides) |
| 59 | + counts['PDF conversions'] += 1 |
| 60 | + |
| 61 | + if options['dry-run']: |
| 62 | + self.stdout.write("This is a dry-run. Nothing has actually changed. In a normal run, the output would say the following:") |
| 63 | + |
| 64 | + for label,count in counts.iteritems(): |
| 65 | + self.stdout.write("%s : %d" % (label,count) ) |
| 66 | + |
| 67 | + |
0 commit comments