mirror of
				https://github.com/ae-utbm/sith.git
				synced 2025-10-31 00:53:08 +00:00 
			
		
		
		
	Migrate albums and pictures to their own tables
This commit is contained in:
		| @@ -88,9 +88,9 @@ class PageAdmin(admin.ModelAdmin): | ||||
|  | ||||
| @admin.register(SithFile) | ||||
| class SithFileAdmin(admin.ModelAdmin): | ||||
|     list_display = ("name", "owner", "size", "date", "is_in_sas") | ||||
|     list_display = ("name", "owner", "size", "date") | ||||
|     autocomplete_fields = ("parent", "owner", "moderator") | ||||
|     search_fields = ("name", "parent__name") | ||||
|     search_fields = ("name",) | ||||
|  | ||||
|  | ||||
| @admin.register(OperationLog) | ||||
|   | ||||
| @@ -97,7 +97,7 @@ class SithFileController(ControllerBase): | ||||
|     ) | ||||
|     @paginate(PageNumberPaginationExtra, page_size=50) | ||||
|     def search_files(self, search: Annotated[str, annotated_types.MinLen(1)]): | ||||
|         return SithFile.objects.filter(is_in_sas=False).filter(name__icontains=search) | ||||
|         return SithFile.objects.filter(name__icontains=search) | ||||
|  | ||||
|  | ||||
| @api_controller("/group") | ||||
|   | ||||
| @@ -110,7 +110,6 @@ class Command(BaseCommand): | ||||
|         p.save(force_lock=True) | ||||
|  | ||||
|         club_root = SithFile.objects.create(name="clubs", owner=root) | ||||
|         sas = SithFile.objects.create(name="SAS", owner=root) | ||||
|         main_club = Club.objects.create( | ||||
|             id=1, name="AE", address="6 Boulevard Anatole France, 90000 Belfort" | ||||
|         ) | ||||
| @@ -693,14 +692,7 @@ class Command(BaseCommand): | ||||
|         # SAS | ||||
|         for f in self.SAS_FIXTURE_PATH.glob("*"): | ||||
|             if f.is_dir(): | ||||
|                 album = Album( | ||||
|                     parent=sas, | ||||
|                     name=f.name, | ||||
|                     owner=root, | ||||
|                     is_folder=True, | ||||
|                     is_in_sas=True, | ||||
|                     is_moderated=True, | ||||
|                 ) | ||||
|                 album = Album(name=f.name) | ||||
|                 album.clean() | ||||
|                 album.save() | ||||
|                 for p in f.iterdir(): | ||||
| @@ -708,17 +700,13 @@ class Command(BaseCommand): | ||||
|                     pict = Picture( | ||||
|                         parent=album, | ||||
|                         name=p.name, | ||||
|                         file=file, | ||||
|                         original=file, | ||||
|                         owner=root, | ||||
|                         is_folder=False, | ||||
|                         is_in_sas=True, | ||||
|                         is_moderated=True, | ||||
|                         mime_type="image/webp", | ||||
|                         size=file.size, | ||||
|                     ) | ||||
|                     pict.file.name = p.name | ||||
|                     pict.full_clean() | ||||
|                     pict.original.name = pict.name | ||||
|                     pict.generate_thumbnails() | ||||
|                     pict.full_clean() | ||||
|                     pict.save() | ||||
|  | ||||
|         img_skia = Picture.objects.get(name="skia.jpg") | ||||
|   | ||||
							
								
								
									
										27
									
								
								core/migrations/0048_remove_sithfiles.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								core/migrations/0048_remove_sithfiles.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,27 @@ | ||||
| # Generated by Django 4.2.17 on 2025-01-26 15:01 | ||||
|  | ||||
| from typing import TYPE_CHECKING | ||||
|  | ||||
| from django.db import migrations | ||||
| from django.db.migrations.state import StateApps | ||||
|  | ||||
| if TYPE_CHECKING: | ||||
|     import core.models | ||||
|  | ||||
|  | ||||
| def remove_sas_sithfiles(apps: StateApps, schema_editor): | ||||
|     SithFile: type[core.models.SithFile] = apps.get_model("core", "SithFile") | ||||
|     SithFile.objects.filter(is_in_sas=True).delete() | ||||
|  | ||||
|  | ||||
| class Migration(migrations.Migration): | ||||
|     dependencies = [ | ||||
|         ("core", "0047_alter_notification_date_alter_notification_type"), | ||||
|         ("sas", "0007_alter_peoplepicturerelation_picture_and_more"), | ||||
|     ] | ||||
|  | ||||
|     operations = [ | ||||
|         migrations.RunPython( | ||||
|             remove_sas_sithfiles, reverse_code=migrations.RunPython.noop, elidable=True | ||||
|         ) | ||||
|     ] | ||||
							
								
								
									
										9
									
								
								core/migrations/0049_remove_sithfile_is_in_sas.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								core/migrations/0049_remove_sithfile_is_in_sas.py
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | ||||
| # Generated by Django 4.2.17 on 2025-02-14 11:58 | ||||
|  | ||||
| from django.db import migrations | ||||
|  | ||||
|  | ||||
| class Migration(migrations.Migration): | ||||
|     dependencies = [("core", "0048_remove_sithfiles")] | ||||
|  | ||||
|     operations = [migrations.RemoveField(model_name="sithfile", name="is_in_sas")] | ||||
| @@ -863,9 +863,6 @@ class SithFile(models.Model): | ||||
|         on_delete=models.CASCADE, | ||||
|     ) | ||||
|     asked_for_removal = models.BooleanField(_("asked for removal"), default=False) | ||||
|     is_in_sas = models.BooleanField( | ||||
|         _("is in the SAS"), default=False, db_index=True | ||||
|     )  # Allows to query this flag, updated at each call to save() | ||||
|  | ||||
|     class Meta: | ||||
|         verbose_name = _("file") | ||||
| @@ -874,22 +871,10 @@ class SithFile(models.Model): | ||||
|         return self.get_parent_path() + "/" + self.name | ||||
|  | ||||
|     def save(self, *args, **kwargs): | ||||
|         sas = SithFile.objects.filter(id=settings.SITH_SAS_ROOT_DIR_ID).first() | ||||
|         self.is_in_sas = sas in self.get_parent_list() or self == sas | ||||
|         adding = self._state.adding | ||||
|         super().save(*args, **kwargs) | ||||
|         if adding: | ||||
|             self.copy_rights() | ||||
|         if self.is_in_sas: | ||||
|             for user in User.objects.filter( | ||||
|                 groups__id__in=[settings.SITH_GROUP_SAS_ADMIN_ID] | ||||
|             ): | ||||
|                 Notification( | ||||
|                     user=user, | ||||
|                     url=reverse("sas:moderation"), | ||||
|                     type="SAS_MODERATION", | ||||
|                     param="1", | ||||
|                 ).save() | ||||
|  | ||||
|     def is_owned_by(self, user: User) -> bool: | ||||
|         if user.is_anonymous: | ||||
| @@ -902,8 +887,6 @@ class SithFile(models.Model): | ||||
|             return user.is_board_member | ||||
|         if user.is_com_admin: | ||||
|             return True | ||||
|         if self.is_in_sas and user.is_in_group(pk=settings.SITH_GROUP_SAS_ADMIN_ID): | ||||
|             return True | ||||
|         return user.id == self.owner_id | ||||
|  | ||||
|     def can_be_viewed_by(self, user: User) -> bool: | ||||
| @@ -1069,18 +1052,6 @@ class SithFile(models.Model): | ||||
|     def is_file(self): | ||||
|         return not self.is_folder | ||||
|  | ||||
|     @cached_property | ||||
|     def as_picture(self): | ||||
|         from sas.models import Picture | ||||
|  | ||||
|         return Picture.objects.filter(id=self.id).first() | ||||
|  | ||||
|     @cached_property | ||||
|     def as_album(self): | ||||
|         from sas.models import Album | ||||
|  | ||||
|         return Album.objects.filter(id=self.id).first() | ||||
|  | ||||
|     def get_parent_list(self): | ||||
|         parents = [] | ||||
|         current = self.parent | ||||
|   | ||||
| @@ -374,7 +374,7 @@ class FileDeleteView(AllowFragment, CanEditPropMixin, DeleteView): | ||||
| class FileModerationView(AllowFragment, ListView): | ||||
|     model = SithFile | ||||
|     template_name = "core/file_moderation.jinja" | ||||
|     queryset = SithFile.objects.filter(is_moderated=False, is_in_sas=False) | ||||
|     queryset = SithFile.objects.filter(is_moderated=False) | ||||
|     ordering = "id" | ||||
|     paginate_by = 100 | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user