Esempio n. 1
0
 def get(self, request, *args, **kwargs):
     check_authorized_user_inside_request(request)
     user_dict = UserDictionary(request.user)
     if request.query_params:
         type_words = request.query_params.get('type_words', None)
         page_num = int(request.query_params.get('target_page', 1))
         items_per_page = int(request.query_params.get('items_per_page', 15))
         if type_words == self.ACTUAL_WORDS:
             total_count, words = user_dict.get_actual_words(page_num,
                                                             items_per_page)
             serializer = UserForeignWordListSerializer(data={
                 'total_count': total_count,
                 'foreign_words': words
             })
             if serializer.is_valid(raise_exception=True):
                 return Response(data=serializer.data,
                                 status=status.HTTP_200_OK)
         if type_words == self.LEARNED_WORDS:
             total_count, words = user_dict.get_learned_words(page_num,
                                                              items_per_page)
             serializer = UserForeignWordListSerializer(data={
                 'total_count': total_count,
                 'foreign_words': words
             })
             if serializer.is_valid(raise_exception=True):
                 return Response(data=serializer.data,
                                 status=status.HTTP_200_OK)
     return Response(status=status.HTTP_400_BAD_REQUEST)
Esempio n. 2
0
 def _get_export_words_to_anki_response(self, user, words_id_list):
     response = HttpResponse(content_type='text/csv', charset='utf-8')
     response[
         'Content-Disposition'] = 'attachment; filename="anki_export.csv"'
     writer = UserWordsToCsvFileWriter(response)
     user_dict = UserDictionary(user)
     words = user_dict.get_actual_words_by_id_list(words_id_list)
     writer.write_user_word_list(words)
     return response
Esempio n. 3
0
    def get(self, request, *args, **kwargs):
        if not request.query_params or not request.query_params.get('action'):
            return Response(status=status.HTTP_400_BAD_REQUEST)

        action = request.query_params.get('action', None)
        if action == 'translate':
            try:
                source = request.query_params.get('source', '')
                translation = get_translation(source)
                if is_user_authorized_inside_request(request):
                    user_dict = UserDictionary(request.user)
                    translation = user_dict.mark_exist_translations(translation)
                ser_translation = TranslatorResultSerializer(translation)
                return Response(
                    data=ser_translation.data,
                    content_type=JSON_CONTENT_TYPE,
                    status=status.HTTP_200_OK
                )
            except Exception as ex:
                logger.exception(ex)
                return Response(status=status.HTTP_404_NOT_FOUND)
        else:
            return Response(status=status.HTTP_400_BAD_REQUEST)
Esempio n. 4
0
 def post(self, request, *args, **kwargs):
     check_authorized_user_inside_request(request)
     try:
         if request.query_params:
             action = request.query_params.get('action', None)
             user_dict = UserDictionary(request.user)
             if action == self.ACTION_DELETE:
                 user_dict.delete_word_by_id(request.data.get('word_id'))
                 return Response(status=status.HTTP_200_OK)
             elif action == self.ACTION_SEND_TO_LEARNED:
                 value = True if request.query_params.get(
                     'value') == 'true' else False
                 user_dict.mark_word_is_learned(request.data.get('word_id'),
                                                value)
                 return Response(status=status.HTTP_200_OK)
             elif action == self.ACTION_ADD_TO_LEARNED:
                 user_dict.add_word_to_learned(request.data.get('word_name'))
                 return Response(status=status.HTTP_200_OK)
             elif action == self.ACTION_DEL_FROM_LEARNED:
                 user_dict.del_word_from_learned(request.data.get('word_name'))
                 return Response(status=status.HTTP_200_OK)
             elif action == self.ACTION_ANKI_EXPORT:
                 words_id_list = request.data.get('exported_ids', None)
                 if words_id_list:
                     return self._get_export_words_to_anki_response(
                         request.user, words_id_list)
             elif action == self.ACTION_MARK_WORDS_EXPORTED:
                 words_id_list = request.data.get('exported_ids', None)
                 if words_id_list:
                     user_dict = UserDictionary(request.user)
                     user_dict.mark_actual_words_exported(words_id_list)
                     return Response(status=status.HTTP_200_OK)
             elif action == self.ACTION_MARK_WORDS_STATUS:
                 word_list = request.data.get('words', None)
                 if word_list:
                     user_dict = UserDictionary(request.user)
                     serializer = SubtitleWordListSerializer(data=word_list)
                     if serializer.is_valid(raise_exception=True):
                         word_list = serializer.data
                         marked_words = user_dict.mark_words_status(word_list)
                         serializer = SubtitleWordListSerializer(marked_words)
                         return Response(
                             data=serializer.data,
                             status=status.HTTP_200_OK
                         )
             return Response(status=status.HTTP_400_BAD_REQUEST)
         else:
             serializer = UserForeignWordSerializer(data=request.data)
             if serializer.is_valid(raise_exception=True):
                 serializer.save(user=request.user)
                 return Response(status=status.HTTP_200_OK)
             return Response(status=status.HTTP_400_BAD_REQUEST)
     except Exception as ex:
         logger.exception(ex)
         return Response(status=status.HTTP_400_BAD_REQUEST)