|
1 | 1 | # stdlib imports
|
2 | 2 | import os
|
| 3 | +import sys |
3 | 4 |
|
4 | 5 | # django imports
|
5 | 6 | from django.core.management import BaseCommand
|
|
16 | 17 | class Command(BaseCommand):
|
17 | 18 |
|
18 | 19 | def add_arguments(self, parser):
|
19 |
| - parser.add_argument('--system', type=str) |
20 |
| - parser.add_argument('--missing', action='store_true') |
| 20 | + parser.add_argument('system', metavar='S', type=str, nargs='?', |
| 21 | + help='System to force twiter card creation') |
21 | 22 | return
|
22 | 23 |
|
23 | 24 | def handle(self, *args, **options):
|
24 | 25 | template = os.path.join(settings.BASE_DIR, "static", settings.TWITTER_CARD_TEMPLATE)
|
25 | 26 | assert os.path.exists(template), "Missing: " + template
|
26 | 27 |
|
27 | 28 | versions = SystemVersion.objects.filter(is_current=True)
|
| 29 | + force = False |
28 | 30 | if options['system']:
|
29 | 31 | keyword = options['system']
|
30 |
| - |
31 | 32 | if keyword.isdigit():
|
32 | 33 | versions = versions.filter(system__id=int(keyword))
|
33 | 34 | else:
|
34 | 35 | versions = versions.filter(system__name__icontains=keyword)
|
| 36 | + force = True |
35 | 37 |
|
36 | 38 | for ver in versions:
|
37 | 39 | card_img = os.path.join(settings.TWITTER_CARD_ROOT, ver.get_twitter_card_image())
|
38 |
| - if options['missing'] and os.path.exists(card_img): continue |
| 40 | + logo_img = os.path.join(settings.MEDIA_ROOT, ver.logo.name) |
| 41 | + |
| 42 | + # Only create ones that are missing or where the logo |
| 43 | + # is newer than the existing twitter image |
| 44 | + if not force: |
| 45 | + if os.path.exists(card_img) and os.path.exists(logo_img): |
| 46 | + if os.path.getmtime(logo_img) <= os.path.getmtime(card_img): |
| 47 | + #print(logo_img, "=>", os.path.getmtime(logo_img)) |
| 48 | + #print(card_img, "=>", os.path.getmtime(card_img)) |
| 49 | + #self.stdout.write("SKIP: %s -> %s" % (ver.system.name, card_img)) |
| 50 | + continue |
| 51 | + elif os.path.exists(card_img): |
| 52 | + continue |
39 | 53 | ver.create_twitter_card()
|
40 | 54 | self.stdout.write("%s -> %s" % (ver.system.name, card_img))
|
41 |
| - |
42 | 55 | # FOR
|
43 | 56 | return
|
44 | 57 |
|
|
0 commit comments