Beispiel #1
0
 def fire_music_tasks():
     from GatewayApp.requesters.music_requester import MusicRequester
     if Queue._music_status == 1:
         return
     else:
         Queue._music_status = 1
     print('Firing music tasks...')
     atasks = list(filter(lambda x: x['type'] == Queue.MUSIC, Queue.queue))
     ar = MusicRequester()
     tasks_to_delete = []
     for task in atasks:
         if task['request'].method == 'POST':
             _, code = ar.post_music(task['request'], task['data'])
         elif task['request'].method == 'PATCH':
             _, code = ar.patch_music(task['request'], task['uuid'],
                                      task['data'])
         elif task['request'].method == 'DELETE':
             _, code = ar.delete_music(task['request'], task['uuid'])
         else:
             code = 501
         if code < 500:
             tasks_to_delete.append(task)
     for task in tasks_to_delete:
         Queue.queue.remove(task)
     print(f'{len(tasks_to_delete)} music tasks fired with success')
Beispiel #2
0
 def _delete_music_from_storie(self, request, storie_json: dict) -> Tuple[dict, int]:
     from GatewayApp.requesters.music_requester import MusicRequester
     if storie_json['music_uuid']:
         del_json, code = MusicRequester().delete_music(request, storie_json['music_uuid'])
         if code not in (204, 404):
             return del_json, code
     return {}, 204
Beispiel #3
0
 def __get_and_set_storie_music(self, storie: dict) -> dict:
     from GatewayApp.requesters.music_requester import MusicRequester
     music_uuid = storie['music_uuid']  # Можеть быть кей еррор, поймаем в гет_сторис
     if music_uuid is not None:
         music_json, music_status = MusicRequester().get_concrete_music(request=None, uuid=music_uuid)
         if music_status != 200:
             from Gateway.settings import DEBUG
             if DEBUG:
                 print(music_json)
             raise MusicGetError(code=music_status, err_json=music_json)
         storie['music'] = music_json
     return storie
Beispiel #4
0
 def post_storie(self, request, data: dict) -> Tuple[dict, int]:
     from GatewayApp.requesters.music_requester import MusicRequester
     from GatewayApp.requesters.stickers_requester import StickersRequester
     # check_json, code = self._check_if_attachments_exist(request, data)
     # Прикрепление юзера и проверка что он вообще есть
     try:
         if not self._check_if_user_exists(request, data['to_user_id']):
             return {'error': 'No user found with given id'}, 404
     except KeyError:
         return {'error': 'No to_user_id key was given'}, 400
     data, code = self._add_from_user_id_to_data(request, data)
     if code != 200:
         return data, code
     # ПОСТ музыки
     try:
         upload_json, code = MusicRequester().post_music(request, data['music'])
         if code != 201:
             return upload_json, code
         data['music_uuid'] = upload_json['uuid']
     except KeyError:
         pass
     # ПОСТ стикера
     try:
         upload_json, code = StickersRequester().post_sticker(request, data['sticker'])
         if code != 201:
             self.__music_rollback(request, data)  # Rollback если в посте стикера ошибка
             return upload_json, code
         data['sticker_uuid'] = upload_json['uuid']
     except KeyError:
         pass
     # Пост самой сторис
     response = self.perform_post_request(self.HOST, data=data)
     if response is None:
         self.__attachments_rollback(request, data)
         return Requester.ERROR_RETURN
     resp_json = self.get_valid_json_from_response(response)
     if response.status_code != 201:
         self.__attachments_rollback(request, data)
         return resp_json, response.status_code
     try:
         resp_json = self.__get_and_set_storie_users(request, resp_json)
     except KeyError:
         return {'error': 'Key error was raised while getting user json!'}, 500
     except UserGetError as e:
         return e.err_msg, e.code
     return resp_json, response.status_code
Beispiel #5
0
 def __music_rollback(self, request, data: dict):
     from GatewayApp.requesters.audio_requester import MusicRequester
     try:
         MusicRequester().delete_audio(request, data['music_uuid'])
     except KeyError:
         pass
Beispiel #6
0
 def _check_if_music_exists(self, music_uuid) -> bool:
     from GatewayApp.requesters.music_requester import MusicRequester
     _, code = MusicRequester().get_concrete_music(request=None, uuid=music_uuid)
     return code == 200
Beispiel #7
0
class BaseMusicView(APIView):
    REQUESTER = MusicRequester()