Exemple #1
0
 def get_queryset(self):
     queryset = super(TeamUserViewSet, self).get_queryset()
     if 'team' in self.request.query_params:
         if self.request.query_params['team'].isdigit():
             # queryset = UserTeam.objects.all().using('db1').filter(team=self.request.query_params['team'])\
             #     .select_related('user')
             # qs = UserTeam.objects.all().using('db2').filter(team=self.request.query_params['team'])\
             #     .select_related('user')
             # # queryset = (qs | queryset).distinct()\
             # #     .filter(team=self.request.query_params['team']).prefetch_related('user')
             # print(self.kwargs)
             # return list(set(chain(qs, queryset)))
             queryset = queryset.filter(
                 team=self.request.query_params['team']).prefetch_related(
                     'user')
             if test_connection_to_db('db2'):
                 queryset = queryset.using('db2')
             else:
                 queryset = queryset.using('db1')
     else:
         if self.request.user.id % 2 == 0:
             if test_connection_to_db('db1'):
                 queryset = queryset.using('db1')
             else:
                 queryset = queryset.using('db2')
         else:
             if test_connection_to_db('db2'):
                 queryset = queryset.using('db2')
             else:
                 queryset = queryset.using('db1')
         queryset = queryset.filter(
             user=self.request.user).prefetch_related('team')
     return queryset
Exemple #2
0
 def perform_create(self, serializer):
     if test_connection_to_db('db1') and test_connection_to_db('db2'):
         if 'game' in self.request.query_params:
             # if hash(Game.objects.get(id=self.request.query_params['game']).name) % 2 == 0:
             #     serializer.save(game_id=self.request.query_params['game'], using='db1')
             # else:
             #     serializer.save(game_id=self.request.query_params['game'], using='db2')
             if int(self.request.query_params['game']) % 2 == 0:
                 serializer.save(game_id=self.request.query_params['game'],
                                 using='db2')
             else:
                 serializer.save(game_id=self.request.query_params['game'],
                                 using='db1')
             # time = datetime.datetime.now()
             # int(time.strftime("%Y%m%d%H%M%S"))
             # random.randint(0, 100)
         elif 'team' in self.request.query_params:
             # print(serializer.validated_data)
             # if hash(serializer.validated_data['game'].name) % 2 == 0:
             #     serializer.save(team_id=self.request.query_params['team'], using='db1')
             # else:
             #     serializer.save(team_id=self.request.query_params['team'], using='db2')
             if serializer.validated_data['game'].id % 2 == 0:
                 serializer.save(team_id=self.request.query_params['team'],
                                 using='db2')
             else:
                 serializer.save(team_id=self.request.query_params['team'],
                                 using='db1')
Exemple #3
0
 def get_queryset(self):
     queryset = super(TeamViewSet, self).get_queryset()
     if test_connection_to_db('db2'):
         if test_connection_to_db('db1'):
             queryset = queryset.using('db2')
         else:
             queryset = queryset.using('db1')
     else:
         queryset = queryset.using('db1')
     return queryset
Exemple #4
0
 def get_queryset(self):
     queryset = super(MatchViewSet, self).get_queryset()
     ran_db = random.randint(1, 2)
     if test_connection_to_db('db1'):
         if test_connection_to_db('db2'):
             queryset = queryset.using('db' + str(ran_db))
         else:
             queryset = queryset.using('db1')
     else:
         queryset = queryset.using('db2')
     return queryset
Exemple #5
0
 def get_queryset(self):
     queryset = super(GameTeamViewSet, self).get_queryset()
     if 'game' in self.request.query_params:
         if self.request.query_params['game'].isdigit():
             queryset = queryset.filter(
                 game_id=self.request.query_params['game'])
             if test_connection_to_db('db2'):
                 if test_connection_to_db('db1'):
                     if int(self.request.query_params['game']) % 2 == 0:
                         queryset = queryset.using('db1')
                     else:
                         queryset = queryset.using('db2')
                 else:
                     queryset = queryset.using('db2')
             else:
                 queryset = queryset.using('db1')
             queryset = queryset.select_related('team', 'game')
         else:
             return GameTeam.objects.none()
     elif 'team' in self.request.query_params:
         if self.request.query_params['team'].isdigit():
             # queryset = GameTeam.objects.all().using('db1')
             # qs = GameTeam.objects.all().using('db2')
             # queryset = (qs | queryset).distinct()
             if test_connection_to_db('db2'):
                 if test_connection_to_db('db1'):
                     if int(self.request.query_params['team']) % 2 == 0:
                         queryset = queryset.using('db1')
                     else:
                         queryset = queryset.using('db2')
                 else:
                     queryset = queryset.using('db2')
             else:
                 queryset = queryset.using('db1')
             queryset = queryset.filter(
                 team_id=self.request.query_params['team']).select_related(
                     'game')
         else:
             return GameTeam.objects.none()
     else:
         return GameTeam.objects.none()
     return queryset
Exemple #6
0
 def get_queryset(self):
     if test_connection_to_db('db2'):
         queryset = Game.objects.using('db2')
     else:
         queryset = Game.objects.using('db1')
     # qs = Game.objects.using('db2')
     # queryset = (qs | queryset).distinct()
     if 'match' in self.request.query_params:
         if self.request.query_params['match'].isdigit:
             queryset = queryset.filter(
                 match_id=self.request.query_params['match'])
             if test_connection_to_db('db2'):
                 if test_connection_to_db('db1'):
                     if int(self.request.query_params['match']) % 2 == 0:
                         queryset = queryset.using('db1')
                     else:
                         queryset = queryset.using('db2')
                 else:
                     queryset = queryset.using('db2')
             else:
                 queryset = queryset.using('db1')
     return queryset
Exemple #7
0
 def perform_create(self, serializer):
     if test_connection_to_db('db1') and test_connection_to_db('db2'):
         if 'team' in self.request.query_params:
             if self.request.query_params['team'].isdigit:
                 # if hash(self.request.user.username) % 2 == 0:
                 #     serializer.save(team_id=self.request.query_params['team'], using='db1')
                 # else:
                 #     serializer.save(team_id=self.request.query_params['team'], using='db2')
                 if self.request.user.id % 2 == 0:
                     serializer.save(
                         team_id=self.request.query_params['team'],
                         using='db2')
                 else:
                     serializer.save(
                         team_id=self.request.query_params['team'],
                         using='db1')
         else:
             # if hash(self.request.user.id) % 2 == 0:
             #     serializer.save(user=self.request.user, using='db1')
             # else:
             #     serializer.save(user=self.request.user, using='db2')
             serializer.save(user=self.request.user, using='all')
Exemple #8
0
 def db_for_read(self, model, **hints):
     print(hints, model._meta.verbose_name)
     # if 'instance' in hints:
     #     if model._meta.app_label == 'game' or model._meta.app_label == 'team':
     #         if hash(hints['instance'].name) % 2:
     #             print('db1', 'write')
     #             return 'db1'
     #     else:
     #         if hints['instance'].id % 2:
     #             print('db1')
     #             return 'db1'
     # return 'db2'
     if model._meta.verbose_name == 'access token':
         if not test_connection_to_db('db1'):
             return 'sl1'
     return None
Exemple #9
0
 def perform_update(self, serializer):
     if test_connection_to_db('db1') and test_connection_to_db('db2'):
         db = self.get_object().id
         serializer.save(using=db % 2)
Exemple #10
0
 def perform_create(self, serializer):
     if test_connection_to_db('db1') and test_connection_to_db('db2'):
         if 'match' in self.request.query_params:
             if self.request.query_params['match'].isdigit():
                 serializer.save(
                     match_id=self.request.query_params['match'])
Exemple #11
0
 def perform_update(self, serializer):
     if test_connection_to_db('db1') and test_connection_to_db('db2'):
         serializer.save(using='all')