~widelands-dev/widelands-website/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from ...models import Tribe as TribeModel

from django.core.management.base import BaseCommand, CommandError
from django.conf import settings

import os
import shutil
from os import path
from widelandslib.tribe import *
from widelandslib.make_flow_diagram import make_graph
from wlhelp.models import Tribe
from glob import glob
import json


class Command(BaseCommand):
    help =\
        """Update the overview pdfs of all tribes in a current checkout"""

    def handle(self, json_directory=os.path.normpath(settings.MEDIA_ROOT + '/map_object_info'), **kwargs):
        source_file = open(os.path.normpath(
            json_directory + '/tribes.json'), 'r')
        tribesinfo = json.load(source_file)

        print 'updating pdf files for all tribes'

        for t in tribesinfo['tribes']:
            tribename = t['name']
            print '  updating pdf file for tribe ', tribename
            gdir = make_graph(tribename)
            pdffile = path.join(gdir, tribename + '.pdf')
            giffile = path.join(gdir, tribename + '.gif')

            targetdir = path.normpath(path.join(settings.MEDIA_ROOT, 'wlhelp',
                                                'network_graphs', tribename))

            try:
                os.makedirs(targetdir)
            except OSError:
                pass

            shutil.copy(pdffile, targetdir)
            shutil.copy(giffile, targetdir)

            tribe = Tribe.objects.get(name=tribename)
            if tribe:
                tribe.network_pdf_url = path.normpath(
                    '%s/%s/%s' % (settings.MEDIA_URL, targetdir[len(settings.MEDIA_ROOT):], tribename + '.pdf'))
                tribe.network_gif_url = path.normpath(
                    '%s/%s/%s' % (settings.MEDIA_URL, targetdir[len(settings.MEDIA_ROOT):], tribename + '.gif'))
                tribe.save()
            else:
                print 'Could not set tribe urls'

            shutil.rmtree(gdir)