diff --git a/sync/models/data_access.py b/sync/models/data_access.py index 761f5be..59f7a1e 100644 --- a/sync/models/data_access.py +++ b/sync/models/data_access.py @@ -21,6 +21,12 @@ class DataAccess(BaseModel): def create_revoke_access_log(self): self.create_access_log(self.shared_with.all(), 'REVOKE_ACCESS') + def concerned_users(self): + users = list(self.shared_with.all()) + if self.related_user: + users.append(self.related_user) + return users + def create_access_log(self, users, operation): """Create an access log for a list of users """ model_class = sync_registry.get_model(self.model_name) diff --git a/sync/signals.py b/sync/signals.py index 1900236..1aed363 100644 --- a/sync/signals.py +++ b/sync/signals.py @@ -27,7 +27,7 @@ def synchronization_prepare(sender, instance, created, **kwargs): signal = kwargs.get('signal') - # avoid crash in manage.py createsuperuser + delete user in the admin + # avoid crash in manage.py createsuperuser + delete user in the admin if isinstance(instance, User) and (instance._state.db is None or signal == pre_delete): return @@ -161,10 +161,10 @@ def detect_foreign_key_changes(sender, instance, device_id): for data_access in data_access_list: if old_value: model_name = old_value.__class__.__name__ - save_model_log(data_access.shared_with.all(), 'REVOKE_ACCESS', model_name, old_value.id, old_value.get_store_id(), device_id) + save_model_log(data_access.concerned_users(), 'REVOKE_ACCESS', model_name, old_value.id, old_value.get_store_id(), device_id) if new_value: model_name = new_value.__class__.__name__ - save_model_log(data_access.shared_with.all(), 'GRANT_ACCESS', model_name, new_value.id, new_value.get_store_id(), device_id) + save_model_log(data_access.concerned_users(), 'GRANT_ACCESS', model_name, new_value.id, new_value.get_store_id(), device_id) # REVOKE access for old_value and GRANT new_value print(f"Foreign key changed in {sender.__name__}: " diff --git a/tournaments/models/court.py b/tournaments/models/court.py index c5dc8bf..e601e6a 100644 --- a/tournaments/models/court.py +++ b/tournaments/models/court.py @@ -5,7 +5,7 @@ from . import BaseModel, Club class Court(BaseModel): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=True) index = models.IntegerField(default=0) - club = models.ForeignKey(Club, on_delete=models.CASCADE, related_name='courts') + club = models.ForeignKey(Club, on_delete=models.SET_NULL, related_name='courts', null=True) name = models.CharField(max_length=50, null=True, blank=True) exit_allowed = models.BooleanField(default=False) indoor = models.BooleanField(default=False) diff --git a/tournaments/models/date_interval.py b/tournaments/models/date_interval.py index 6593812..c06f573 100644 --- a/tournaments/models/date_interval.py +++ b/tournaments/models/date_interval.py @@ -4,7 +4,7 @@ from . import BaseModel class DateInterval(BaseModel): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=True) - event = models.ForeignKey('Event', on_delete=models.CASCADE, related_name='date_intervals') + event = models.ForeignKey('Event', on_delete=models.SET_NULL, related_name='date_intervals', null=True) court_index = models.IntegerField() start_date = models.DateTimeField() end_date = models.DateTimeField() diff --git a/tournaments/models/draw_log.py b/tournaments/models/draw_log.py index afda9cc..4714018 100644 --- a/tournaments/models/draw_log.py +++ b/tournaments/models/draw_log.py @@ -4,7 +4,7 @@ import uuid class DrawLog(SideStoreModel): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=True) - tournament = models.ForeignKey('Tournament', on_delete=models.CASCADE, related_name='draw_logs') + tournament = models.ForeignKey('Tournament', on_delete=models.SET_NULL, related_name='draw_logs', null=True) draw_date = models.DateTimeField() draw_seed = models.IntegerField() draw_match_index = models.IntegerField() diff --git a/tournaments/models/group_stage.py b/tournaments/models/group_stage.py index d83be58..b17a124 100644 --- a/tournaments/models/group_stage.py +++ b/tournaments/models/group_stage.py @@ -9,7 +9,7 @@ from django.utils import timezone class GroupStage(SideStoreModel): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=True) - tournament = models.ForeignKey(Tournament, on_delete=models.CASCADE, related_name='group_stages') + tournament = models.ForeignKey(Tournament, on_delete=models.SET_NULL, related_name='group_stages', null=True) index = models.IntegerField(default=0) size = models.IntegerField(default=4) format = models.IntegerField(default=FederalMatchCategory.NINE_GAMES, choices=FederalMatchCategory.choices, null=True, blank=True) diff --git a/tournaments/models/match.py b/tournaments/models/match.py index 18ea87c..f6cb0e2 100644 --- a/tournaments/models/match.py +++ b/tournaments/models/match.py @@ -10,8 +10,8 @@ from ..utils.extensions import format_seconds class Match(SideStoreModel): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=True) - round = models.ForeignKey(Round, null=True, blank=True, on_delete=models.CASCADE, related_name='matches') - group_stage = models.ForeignKey(GroupStage, null=True, blank=True, on_delete=models.CASCADE, related_name='matches') + round = models.ForeignKey(Round, null=True, blank=True, on_delete=models.SET_NULL, related_name='matches') + group_stage = models.ForeignKey(GroupStage, null=True, blank=True, on_delete=models.SET_NULL, related_name='matches') name = models.CharField(max_length=200, null=True, blank=True) start_date = models.DateTimeField(null=True, blank=True) end_date = models.DateTimeField(null=True, blank=True) diff --git a/tournaments/models/player_registration.py b/tournaments/models/player_registration.py index 9c81030..687f071 100644 --- a/tournaments/models/player_registration.py +++ b/tournaments/models/player_registration.py @@ -4,7 +4,7 @@ import uuid class PlayerRegistration(SideStoreModel): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=True) - team_registration = models.ForeignKey(TeamRegistration, on_delete=models.CASCADE, related_name='player_registrations') + team_registration = models.ForeignKey(TeamRegistration, on_delete=models.SET_NULL, related_name='player_registrations', null=True) first_name = models.CharField(max_length=50, blank=True) last_name = models.CharField(max_length=50, blank=True) licence_id = models.CharField(max_length=50, null=True, blank=True) diff --git a/tournaments/models/round.py b/tournaments/models/round.py index d3fa5ee..4a58db6 100644 --- a/tournaments/models/round.py +++ b/tournaments/models/round.py @@ -4,9 +4,9 @@ import uuid class Round(SideStoreModel): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=True) - tournament = models.ForeignKey(Tournament, on_delete=models.CASCADE, related_name='rounds') + tournament = models.ForeignKey(Tournament, on_delete=models.SET_NULL, related_name='rounds', null=True) index = models.IntegerField(default=0) - parent = models.ForeignKey('self', blank=True, null=True, on_delete=models.CASCADE, related_name='children') + parent = models.ForeignKey('self', blank=True, null=True, on_delete=models.SET_NULL, related_name='children') format = models.IntegerField(default=FederalMatchCategory.NINE_GAMES, choices=FederalMatchCategory.choices, null=True, blank=True) start_date = models.DateTimeField(null=True, blank=True) group_stage_loser_bracket = models.BooleanField(default=False) diff --git a/tournaments/models/team_registration.py b/tournaments/models/team_registration.py index b44ba24..d38ba4d 100644 --- a/tournaments/models/team_registration.py +++ b/tournaments/models/team_registration.py @@ -6,7 +6,7 @@ from django.utils import timezone class TeamRegistration(SideStoreModel): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=True) - tournament = models.ForeignKey(Tournament, on_delete=models.CASCADE, related_name='team_registrations') + tournament = models.ForeignKey(Tournament, on_delete=models.SET_NULL, related_name='team_registrations', null=True) group_stage = models.ForeignKey(GroupStage, null=True, blank=True, on_delete=models.SET_NULL, related_name='team_registrations') registration_date = models.DateTimeField(null=True, blank=True) call_date = models.DateTimeField(null=True, blank=True) diff --git a/tournaments/models/team_score.py b/tournaments/models/team_score.py index f570bc3..e5c37be 100644 --- a/tournaments/models/team_score.py +++ b/tournaments/models/team_score.py @@ -4,8 +4,8 @@ import uuid class TeamScore(SideStoreModel): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=True) - match = models.ForeignKey(Match, on_delete=models.CASCADE, related_name="team_scores") - team_registration = models.ForeignKey(TeamRegistration, on_delete=models.CASCADE, null=True, blank=True, related_name="team_scores") + match = models.ForeignKey(Match, on_delete=models.SET_NULL, related_name="team_scores", null=True) + team_registration = models.ForeignKey(TeamRegistration, on_delete=models.SET_NULL, null=True, blank=True, related_name="team_scores") score = models.CharField(max_length=50, null=True, blank=True) walk_out = models.IntegerField(null=True, blank=True) # TODO type of WO: forfeit, injury... lucky_loser = models.IntegerField(null=True, blank=True) diff --git a/tournaments/models/tournament.py b/tournaments/models/tournament.py index f724175..2b917c8 100644 --- a/tournaments/models/tournament.py +++ b/tournaments/models/tournament.py @@ -20,7 +20,7 @@ class TeamSortingType(models.IntegerChoices): class Tournament(BaseModel): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=True) - event = models.ForeignKey(Event, blank=True, null=True, on_delete=models.CASCADE, related_name="tournaments") + event = models.ForeignKey(Event, blank=True, null=True, on_delete=models.SET_NULL, related_name="tournaments") name = models.CharField(max_length=200, null=True, blank=True) start_date = models.DateTimeField() end_date = models.DateTimeField(null=True, blank=True) diff --git a/tournaments/signals.py b/tournaments/signals.py index cdc123c..01f8e02 100644 --- a/tournaments/signals.py +++ b/tournaments/signals.py @@ -20,8 +20,8 @@ def generate_unique_code(): return code @receiver(post_save, sender=Club) -def assign_unique_code(sender, instance, created, **kwargs): - if created and not instance.broadcast_code: +def assign_unique_code(sender, instance, **kwargs): + if not instance.broadcast_code: instance.broadcast_code = generate_unique_code() instance.save()