|
| 1 | +import pathlib |
| 2 | +import uuid |
| 3 | + |
| 4 | +from django.db import models |
| 5 | + |
| 6 | +from kirovy import typing as t |
| 7 | +from kirovy.models import cnc_map, file_base, cnc_user, cnc_game |
| 8 | + |
| 9 | + |
| 10 | +class MapPreview(file_base.CncNetFileBaseModel): |
| 11 | + """An image preview for a C&C Map upload. |
| 12 | +
|
| 13 | + .. note:: |
| 14 | +
|
| 15 | + This class has no user link. The link to the user is via |
| 16 | + :attr:`kirovy.models.map_preview.CncMapPreview.cnc_map`. |
| 17 | +
|
| 18 | + .. note:: |
| 19 | +
|
| 20 | + Map previews are uploaded to the same directory as map files using |
| 21 | + :func:`kirovy.models.cnc_map.CncMap.get_map_directory_path` through this class's |
| 22 | + :attr:`kirovy.models.map_preview.CncMapPreview.cnc_map`. |
| 23 | +
|
| 24 | + """ |
| 25 | + |
| 26 | + cnc_map_file = models.ForeignKey(cnc_map.CncMapFile, on_delete=models.CASCADE) |
| 27 | + """The map file that this preview belongs to. We link to the file so that we can have version-specific previews.""" |
| 28 | + |
| 29 | + ALLOWED_EXTENSION_TYPES = { |
| 30 | + cnc_game.CncFileExtension.ExtensionTypes.IMAGE.value, |
| 31 | + } |
| 32 | + |
| 33 | + is_extracted = models.BooleanField(null=False, blank=False) |
| 34 | + """If true, then this image was extracted from the uploaded map file, usually generated by FinalAlert. |
| 35 | +
|
| 36 | + This will always be false for games released after Yuri's Revenge because Generals and beyond do not pack the |
| 37 | + preview image into the map files. |
| 38 | + """ |
| 39 | + |
| 40 | + def save(self, *args, **kwargs): |
| 41 | + self.name = self.cnc_map_file.name |
| 42 | + self.cnc_game = self.cnc_map_file.cnc_game |
| 43 | + return super().save(*args, **kwargs) |
| 44 | + |
| 45 | + @staticmethod |
| 46 | + def generate_upload_to(instance: "MapPreview", filename: str) -> pathlib.Path: |
| 47 | + """Generate the path to upload map previews to. |
| 48 | +
|
| 49 | + Gets called by :func:`kirovy.models.file_base._generate_upload_to` when ``CncMapFile.save`` is called. |
| 50 | + See [the django docs for file fields](https://docs.djangoproject.com/en/5.0/ref/models/fields/#filefield). |
| 51 | + ``upload_to`` is set in :attr:`kirovy.models.file_base.CncNetFileBaseModel.file`, which calls |
| 52 | + ``_generate_upload_to``, which calls this function. |
| 53 | +
|
| 54 | + :param instance: |
| 55 | + Acts as ``self``. The map preview object that we are creating an upload path for. |
| 56 | + :param filename: |
| 57 | + The filename of the uploaded file. |
| 58 | + :return: |
| 59 | + Path to upload map to relative to :attr:`~kirovy.settings.base.MEDIA_ROOT`. |
| 60 | + """ |
| 61 | + filename = pathlib.Path(filename) |
| 62 | + filename_uuid = str(uuid.uuid4()).replace("-", "") |
| 63 | + final_file_name = f"{instance.name}_{filename_uuid}{filename.suffix}" |
| 64 | + |
| 65 | + # e.g. "yr/maps/CNC_NET_MAP_ID_HEX/ra2_map_file_name_UUID.png |
| 66 | + return pathlib.Path( |
| 67 | + instance.cnc_map_file.cnc_map.get_map_directory_path(), final_file_name |
| 68 | + ) |
0 commit comments