~widelands-dev/widelands-website/trunk

« back to all changes in this revision

Viewing changes to wlmaps/forms.py

  • Committer: kaputtnik
  • Date: 2019-07-21 08:18:21 UTC
  • mfrom: (542.2.8 move_maps_minimap)
  • Revision ID: kaputtnik-20190721081821-mjqitcya1qdozbqg
Cleanups for wlmaps: minimaps are stored in wlmaps/minimaps; deleting a maps deletes the corresponding files; Implemented the new value 'needs_version_after'

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 
12
12
from wlmaps.models import Map
13
13
import os
 
14
import shutil
 
15
 
14
16
 
15
17
class UploadMapForm(ModelForm):
16
18
    """
59
61
            os.chdir(settings.WIDELANDS_SVN_DIR)
60
62
            check_call(['wl_map_info', saved_file])
61
63
 
62
 
            # TODO(shevonar): delete file because it will be saved again when
63
 
            # the model is saved. File should not be saved twice
 
64
            # Deleting the file because it will be saved again when
 
65
            # the model is saved.
64
66
            default_storage.delete(saved_file)
65
67
            os.chdir(old_cwd)
66
68
        except CalledProcessError:
86
88
        self.instance.descr = mapinfo['description']
87
89
        self.instance.hint = mapinfo['hint']
88
90
        self.instance.world_name = mapinfo['world_name']
 
91
        self.instance.wl_version_after = mapinfo['needs_widelands_version_after']
89
92
 
90
 
        # mapinfo["minimap"] is an absolute path.
91
 
        # We partition it to get the correct file path
92
 
        minimap_path = mapinfo['minimap'].partition(settings.MEDIA_ROOT)[2]
93
 
        self.instance.minimap = '/' + minimap_path
 
93
        # mapinfo["minimap"] is the absolute path containing the path where it
 
94
        # is saved, extract the name
 
95
        minimap_name = mapinfo['minimap'].rpartition('/')[2]
 
96
        minimap_upload_to = self.instance._meta.get_field('minimap').upload_to
 
97
        # Set the destination relative to MEDIA_ROOT
 
98
        minimap_path = os.path.join(minimap_upload_to, minimap_name)
 
99
        self.instance.minimap = minimap_path
 
100
        # Move the minimap file (.png) from wlmaps/maps to wlmaps/minimaps
 
101
        shutil.move(mapinfo['minimap'], os.path.join(
 
102
            settings.MEDIA_ROOT, minimap_path))
94
103
 
95
104
        # the json file is no longer needed
96
105
        default_storage.delete(saved_file + '.json')