def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) result = serializer.save() headers = self.get_success_headers(serializer.data) if result: return CommonResponse('MUSIC_SONG_COMMENTS_LIKES_CREATED', status.HTTP_200_OK, True, '성공적으로 댓글 좋아요를 눌렀습니다.', headers=headers) else: return CommonResponse('MUSIC_SONG_COMMENTS_LIKES_DELETED', status.HTTP_200_OK, False, '성공적으로 댓글 좋아요를 취소했습니다.', headers=headers)
def list(self, request, *args, **kwargs): response = cache.get(self.cache_key) if not response: response = super().list(request, args, kwargs).data cache.set(self.cache_key, response, timeout=self.cache_timeout) return CommonResponse('INTRO_QUESTION_LIST', status.HTTP_200_OK, response, '성공적으로 질문을 조회했습니다.')
def update(self, request, *args, **kwargs): object = self.get_object() serializer = self.get_serializer(object, data=request.data, partial=True) serializer.is_valid(raise_exception=True) serializer.save() response = serializer.validated_data return CommonResponse('MUSIC_SONG_COMMENTS_UPDATED', status.HTTP_200_OK, response, '성공적으로 댓글을 수정됐습니다.')
def yearly(self, request, *args, **kwargs): now = timezone.localtime() year_start = now.replace(month=1, day=1, hour=0, minute=0, second=0, microsecond=0) year_end = year_start.replace(year=year_start.year+1) songs = self.get_queryset() \ .filter(created_at__range=(year_start, year_end)) \ .values('url') \ .annotate(count_viewed=Count('url')) \ .order_by('-count_viewed') return CommonResponse('MUSIC_SONG_MONTHLY_LIST', status.HTTP_200_OK, songs, '성공적으로 금년 신청곡 통계를 조회했습니다.')
def monthly(self, request, *args, **kwargs): now = timezone.localtime() month_start = now.replace(day=1, hour=0, minute=0, second=0, microsecond=0) month_end = now.replace(day=28, hour=0, minute=0, second=0, microsecond=0) + timedelta(days=4) month_end -= timedelta(days=month_end.day) songs = self.get_queryset() \ .filter(created_at__range=(month_start, month_end)) \ .values('url') \ .annotate(count_viewed=Count('url')) \ .order_by('-count_viewed') return CommonResponse('MUSIC_SONG_MONTHLY_LIST', status.HTTP_200_OK, songs, '성공적으로 금달 신청곡 통계를 조회했습니다.')
def weekly(self, request, *args, **kwargs): now = timezone.localtime() week_start = (now - timedelta(days = now.weekday())).replace(hour = 0, minute = 0, second = 0, microsecond = 0) week_end = (now + timedelta(days = 7 - now.weekday())).replace(hour=0, minute=0, second=0, microsecond=0) # latest_song_per_url = self.get_queryset().filter(url=OuterRef('url')).order_by('-id')[0] filtered_queryset = self.get_queryset() \ .filter(created_at__range=(week_start, week_end)) songs = self.get_queryset() \ .filter(created_at__range=(week_start, week_end)) \ .values('url') \ .annotate(count_viewed=Count('url')) \ .order_by('-count_viewed') return CommonResponse('MUSIC_SONG_WEEKLY_LIST', status.HTTP_200_OK, songs, '성공적으로 금주 신청곡 통계를 조회했습니다.')
def song_play(request): songs = Song.objects.filter(is_played=False) \ .order_by('-count_likes') \ .select_related('user') if not songs: raise CommonAPIException('MUSIC_SONG_PLAY_EMPTY', status.HTTP_200_OK, "신청곡 대기열이 비어있습니다!") original_song = songs[0] unplayed_songs = songs.values()[0] unplayed_songs['is_played'] = True serializer = SongSerializer(original_song, data=unplayed_songs, partial=True) serializer.is_valid(raise_exception=True) song_url = play_song(serializer.validated_data['title'], serializer.validated_data['url']) serializer.validated_data['url'] = song_url serializer.save() return CommonResponse('MUSIC_PLAY_SUCCESS', status.HTTP_200_OK, serializer.data, '성공적으로 노래를 재생했습니다.')
def create(self, request, *args, **kwargs): response = super().create(request, args, kwargs).data cache.delete(self.cache_key) return CommonResponse('INTRO_QUESTION_CREATED', status.HTTP_201_CREATED, response, '성공적으로 질문을 등록했습니다.')
def destroy(self, request, *args, **kwargs): super().destroy(request, args, kwargs) return CommonResponse('MUSIC_SONG_COMMENTS_DELETED', status.HTTP_204_NO_CONTENT, True, '성공적으로 댓글을 삭제됐습니다.')
def list(self, request, *args, **kwargs): response = super().list(request, args, kwargs).data return CommonResponse('INTRO_ANSWER_LIST', status.HTTP_200_OK, response, '성공적으로 답변을 조회했습니다.')
def create(self, request, *args, **kwargs): response = super().create(request, args, kwargs).data return CommonResponse('MUSIC_SONG_COMMENTS_CREATED', status.HTTP_201_CREATED, response, '성공적으로 댓글을 등록했습니다.')
def list(self, request, *args, **kwargs): response = super().list(request, args, kwargs).data return CommonResponse('MUSIC_SONG_COMMENTS', status.HTTP_200_OK, response, '성공적으로 댓글을 불러왔습니다.')
def song_clear(request): unplayed_songs = Song.objects.prefetch_related('user').filter(is_played=False) unplayed_songs.delete() return CommonResponse('MUSIC_SONG_CLEAR', status.HTTP_204_NO_CONTENT, True, '신청곡 목록을 모두 삭제했습니다.')
def destroy(self, request, *args, **kwargs): super().destroy(request, args, kwargs) cache.delete(self.cache_key) return CommonResponse('INTRO_QUESTION_DELETED', status.HTTP_204_NO_CONTENT, True, '성공적으로 질문이 삭제됐습니다.')
def partial_update(self, request, *args, **kwargs): response = super().partial_update(request, args, kwargs).data cache.delete(self.cache_key) return CommonResponse('INTRO_QUESTION_UPDATED', status.HTTP_200_OK, response, '성공적으로 질문이 수정됐습니다.')
def create(self, request, *args, **kwargs): request.data['user'] = request.user.pk response = super().create(request, args, kwargs).data return CommonResponse('INTRO_ANSWER_CREATED', status.HTTP_201_CREATED, response, '성공적으로 답변을 등록했습니다.')
def retrieve(self, request, *args, **kwargs): response = super().retrieve(request, args, kwargs).data return CommonResponse('INTRO_QUESTION_DETAIL', status.HTTP_200_OK, response, '성공적으로 질문을 조회했습니다.')
def update(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data, partial=True) serializer.is_valid(raise_exception=True) response = serializer.validated_data return CommonResponse('INTRO_ANSWER_UPDATED', status.HTTP_200_OK, response, '성공적으로 답변이 수정됐습니다.')
def destroy(self, request, *args, **kwargs): super().destroy(request, args, kwargs) return CommonResponse('INTRO_ANSWER_DELETED', status.HTTP_204_NO_CONTENT, True, '성공적으로 답변이 삭제됐습니다.')