Пример #1
0
    def test_example_sentence_re(self):
        test_data = self.get_test_data()

        test_body = """Мне 18 лет. Я — студентка Новосибирского пединститута. 
        Две лекции, две пары подряд. Преподаватель немного опаздывает. Так хочется спать…"""

        test_data['text_sections'][0]['body'] = test_body

        self.create_text(test_data=test_data)

        self.assertTrue(TextSection.objects.count())

        text_section = TextSection.objects.all()[0]

        TextPhraseTranslation.create(
            text_phrase=TextWord.create(phrase='опаздывает', instance=0, text_section=text_section),
            phrase='be late',
            correct_for_context=True
        )

        text_phrase = TextPhrase.objects.get(phrase='опаздывает', text_section=text_section)

        self.assertTrue(text_phrase)

        self.assertEquals('Преподаватель немного опаздывает.', text_phrase.sentence)
Пример #2
0
    def test_delete_text_with_one_student_flashcard(self):
        test_data = self.get_test_data()

        test_data['text_sections'][0]['body'] = 'заявление неделю Число'

        text = self.create_text(test_data=test_data)

        text_sections = text.sections.all()

        заявление = TextWord.create(phrase='заявление', instance=0, text_section=text_sections[0])

        TextPhraseTranslation.create(
            text_phrase=заявление,
            phrase='statement',
            correct_for_context=True
        )

        test_student = Student.objects.filter()[0]

        test_student.add_to_flashcards(заявление)

        student_flashcard_session, _ = StudentFlashcardSession.objects.get_or_create(student=test_student)

        text.delete()

        # session is deleted if there's a single flashcard
        self.assertFalse(StudentFlashcardSession.objects.filter(pk=student_flashcard_session.pk).exists())
Пример #3
0
    def put(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
        try:
            translation_merge_params = json.loads(request.body.decode('utf8'))

            jsonschema.validate(translation_merge_params,
                                TextPhraseTranslation.to_merge_json_schema())

        except json.JSONDecodeError as decode_error:
            return HttpResponse(json.dumps(
                {'errors': {
                    'json': str(decode_error)
                }}),
                                status=400)

        except jsonschema.ValidationError as validation_error:
            return HttpResponse(json.dumps(
                {'errors': {
                    'json': str(validation_error)
                }}),
                                status=400)

        try:
            text_phrases = []

            for text_phrase in translation_merge_params['words']:

                # There is a notion "word_type" that comes from the frontend but causes an error on the back.
                # Since it's not actually a part of a TextPhrase it throws an exception. If one were to implement
                # "Save for All" they'd need to send "multiple" or something from the frontend. They they would
                # need to do something similar with the grouped words table- in particular change the textptr to
                # the new TextPhrase and have the translation point to that new TextPhrase too -- I think.
                try:
                    del (text_phrase['word_type'])
                except:
                    # It's not passed by the test suite.
                    pass

                text_phrase = TextPhrase.objects.get(**text_phrase)

                with transaction.atomic():
                    text_phrase.translations.filter().delete()

                    for translation in translation_merge_params[
                            'translations']:
                        translation['text_phrase'] = text_phrase
                        TextPhraseTranslation.create(**translation)

                    text_phrases.append(text_phrase)

            response = []

            for text_phrase in text_phrases:
                response.append(text_phrase.to_translations_dict())

            return HttpResponse(json.dumps(response), status=200)

        except (DatabaseError, ObjectDoesNotExist) as e:
            return HttpResponseServerError(
                json.dumps({'errors': 'something went wrong'}))
Пример #4
0
    def post(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
        if 'pk' not in kwargs:
            return HttpResponseNotAllowed(
                permitted_methods=self.allowed_methods)

        try:
            text_word_add_translation_params = json.loads(
                request.body.decode('utf8'))

            jsonschema.validate(text_word_add_translation_params,
                                TextPhraseTranslation.to_add_json_schema())

        except json.JSONDecodeError as decode_error:
            return HttpResponse(json.dumps(
                {'errors': {
                    'json': str(decode_error)
                }}),
                                status=400)

        except jsonschema.ValidationError as validation_error:
            return HttpResponse(json.dumps(
                {'errors': {
                    'json': str(validation_error)
                }}),
                                status=400)

        try:
            with transaction.atomic():
                text_phrase = TextPhrase.objects.get(id=kwargs['pk'])

                text_word_add_translation_params['text_phrase'] = text_phrase

                if 'correct_for_context' in text_word_add_translation_params and text_word_add_translation_params[
                        'correct_for_context']:
                    TextPhraseTranslation.objects.filter(
                        text_phrase=text_phrase).update(
                            correct_for_context=False)

                text_phrase_translation = TextPhraseTranslation.create(
                    **text_word_add_translation_params)

                return HttpResponse(
                    json.dumps({
                        'text_word':
                        text_phrase.child_instance.to_translations_dict(),
                        'translation':
                        text_phrase_translation.to_dict()
                    }))

        except (TextPhrase.DoesNotExist, DatabaseError):
            return HttpResponseServerError(
                json.dumps({'errors': 'something went wrong'}))
Пример #5
0
    def put(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
        try:
            translation_merge_params = json.loads(request.body.decode('utf8'))

            jsonschema.validate(translation_merge_params,
                                TextPhraseTranslation.to_merge_json_schema())

        except json.JSONDecodeError as decode_error:
            return HttpResponse(json.dumps(
                {'errors': {
                    'json': str(decode_error)
                }}),
                                status=400)

        except jsonschema.ValidationError as validation_error:
            return HttpResponse(json.dumps(
                {'errors': {
                    'json': str(validation_error)
                }}),
                                status=400)

        try:
            text_phrases = []

            for text_phrase in translation_merge_params['words']:
                text_phrase = TextPhrase.objects.get(**text_phrase)

                with transaction.atomic():
                    text_phrase.translations.filter().delete()

                    for translation in translation_merge_params[
                            'translations']:
                        translation['text_phrase'] = text_phrase
                        TextPhraseTranslation.create(**translation)

                    text_phrases.append(text_phrase)

            response = []

            for text_phrase in text_phrases:
                response.append(text_phrase.to_translations_dict())

            return HttpResponse(json.dumps(response), status=200)

        except (DatabaseError, ObjectDoesNotExist) as e:
            return HttpResponseServerError(
                json.dumps({'errors': 'something went wrong'}))
Пример #6
0
    def test_delete_text_with_multiple_student_flashcards(self):
        test_data_one = self.get_test_data()
        test_data_two = self.get_test_data()

        test_data_one['text_sections'][0]['body'] = 'заявление'
        test_data_two['text_sections'][0]['body'] = 'неделю'

        text_one = self.create_text(test_data=test_data_one)
        text_two = self.create_text(test_data=test_data_two)

        text_one_section = text_one.sections.all()
        text_two_section = text_two.sections.all()

        заявление = TextWord.create(phrase='заявление', instance=0, text_section=text_one_section[0])
        неделю = TextWord.create(phrase='неделю', instance=0, text_section=text_two_section[0])

        TextPhraseTranslation.create(
            text_phrase=заявление,
            phrase='statement',
            correct_for_context=True
        )

        TextPhraseTranslation.create(
            text_phrase=неделю,
            phrase='a week',
            correct_for_context=True
        )

        test_student = Student.objects.filter()[0]

        test_student.add_to_flashcards(заявление)
        test_student.add_to_flashcards(неделю)

        student_flashcard_session, _ = StudentFlashcardSession.objects.get_or_create(student=test_student)

        text_one.delete()

        # session isn't deleted if there's more than one flashcard
        self.assertTrue(StudentFlashcardSession.objects.filter(pk=student_flashcard_session.pk).exists())

        student_flashcard_session.refresh_from_db()

        self.assertTrue(student_flashcard_session.current_flashcard)

        self.assertTrue(student_flashcard_session.current_flashcard.phrase, неделю)
Пример #7
0
    def put(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
        text_phrase_translation = None

        if 'tr_pk' not in kwargs:
            return HttpResponseNotAllowed(
                permitted_methods=self.allowed_methods)

        try:
            text_translation_update_params = json.loads(
                request.body.decode('utf8'))

            jsonschema.validate(text_translation_update_params,
                                TextPhraseTranslation.to_update_json_schema())

        except json.JSONDecodeError as decode_error:
            return HttpResponse(json.dumps(
                {'errors': {
                    'json': str(decode_error)
                }}),
                                status=400)

        except jsonschema.ValidationError as validation_error:
            return HttpResponse(json.dumps(
                {'errors': {
                    'json': str(validation_error)
                }}),
                                status=400)

        try:
            text_phrase_translation = TextPhraseTranslation.objects.get(
                id=kwargs['tr_pk'])

            with transaction.atomic():
                # set all other translations to correct_for_context=False
                TextPhraseTranslation.objects.filter(
                    text_phrase=text_phrase_translation.text_phrase).update(
                        correct_for_context=False)

                TextPhraseTranslation.objects.filter(
                    pk=kwargs['tr_pk']).update(
                        **text_translation_update_params)

            text_phrase_translation.refresh_from_db()

            return HttpResponse(
                json.dumps({
                    'text_word':
                    text_phrase_translation.text_phrase.child_instance.
                    to_translations_dict(),
                    'translation':
                    text_phrase_translation.to_dict()
                }))

        except ObjectDoesNotExist:
            return HttpResponseServerError(
                json.dumps({'errors': 'something went wrong'}))
Пример #8
0
    def text_section_parse_word_definitions(
            self,
            message: Dict,
            *args,
            log_msgs: List[AnyStr] = None,
            **kwargs) -> Tuple[TextSection, List[AnyStr]]:
        text_section = TextSection.objects.get(pk=message['text_section_pk'])

        text_section_words = list(text_section.words)

        def log(msg: AnyStr, msgs: Union[List[AnyStr], None]) -> List[AnyStr]:
            logger.debug(msg)

            if msgs:
                msgs.append(msg)

            return msgs

        log_msgs = log(
            f'Parsing {len(text_section_words)} word definitions for '
            f'text section pk={message["text_section_pk"]}', log_msgs)

        word_data, word_freqs = text_section.parse_word_definitions()

        for word in word_data:
            for i, word_instance in enumerate(word_data[word]):
                with transaction.atomic():
                    text_word, text_word_created = TextWord.objects.get_or_create(
                        text_section=text_section,
                        phrase=word,
                        instance=i,
                        **word_instance['grammemes'])

                    if text_word_created:
                        # populate translations
                        log_msgs = log(
                            f'created a new word "{text_word.phrase}" '
                            f'(pk: {text_word.pk}, instance: {text_word.instance}) '
                            f'for section pk {text_section.pk}', log_msgs)

                        text_word.save()

                        if len(word_instance['translations']):
                            for j, translation in enumerate(
                                    word_instance['translations']):
                                if translation.phrase:
                                    text_word_definition = TextPhraseTranslation.create(
                                        text_phrase=text_word,
                                        phrase=translation.phrase.text,
                                        correct_for_context=(True if j == 0
                                                             else False))

                                    text_word_definition.save()

                            log_msgs = log(
                                f'created '
                                f'{len(word_instance["translations"])} translations '
                                f'for text word pk {text_word.pk}', log_msgs)

        log_msgs = log(
            f'Finished parsing translations for text section pk={message["text_section_pk"]}',
            log_msgs)

        text_section.save()

        return text_section, log_msgs
Пример #9
0
    def setUp(self):
        super(TestFlashcardStateMachine, self).setUp()

        self.instructor = self.new_instructor_client(Client())
        self.student = self.new_student_client(Client())

        self.text_test = TestText()

        self.text_test.instructor = self.instructor
        self.text_test.student = self.student

        self.test_student = Student.objects.filter()[0]

        test_data = self.get_test_data()

        test_data['text_sections'][0]['body'] = 'заявление неделю Число'
        test_data['text_sections'][1][
            'body'] = 'заявление неделю стрельбы вещи'

        self.text = self.text_test.create_text(test_data=test_data)

        text_sections = self.text.sections.all()

        self.text_phrases = []

        TextPhraseTranslation.create(text_phrase=TextWord.create(
            phrase='заявление', instance=0, text_section=text_sections[0]),
                                     phrase='statement',
                                     correct_for_context=True)

        TextPhraseTranslation.create(text_phrase=TextWord.create(
            phrase='неделю', instance=0, text_section=text_sections[0]),
                                     phrase='week',
                                     correct_for_context=True)

        TextPhraseTranslation.create(text_phrase=TextWord.create(
            phrase='стрельбы', instance=0, text_section=text_sections[1]),
                                     phrase='shooting',
                                     correct_for_context=True)

        TextPhraseTranslation.create(text_phrase=TextWord.create(
            phrase='Число', instance=0, text_section=text_sections[0]),
                                     phrase='number',
                                     correct_for_context=True)

        TextPhraseTranslation.create(text_phrase=TextWord.create(
            phrase='вещи', instance=0, text_section=text_sections[1]),
                                     phrase='number',
                                     correct_for_context=True)

        self.text_phrases.append(TextPhrase.objects.get(phrase='заявление'))
        self.text_phrases.append(TextPhrase.objects.get(phrase='неделю'))
        self.text_phrases.append(TextPhrase.objects.get(phrase='стрельбы'))
        self.text_phrases.append(TextPhrase.objects.get(phrase='Число'))
        self.text_phrases.append(TextPhrase.objects.get(phrase='вещи'))

        self.test_flashcards = []

        self.test_flashcards.append(
            self.test_student.add_to_flashcards(self.text_phrases[0]))
        self.test_flashcards.append(
            self.test_student.add_to_flashcards(self.text_phrases[1]))

        self.test_flashcards.append(
            self.test_student.add_to_flashcards(self.text_phrases[2]))
        self.test_flashcards.append(
            self.test_student.add_to_flashcards(self.text_phrases[3]))
        self.test_flashcards.append(
            self.test_student.add_to_flashcards(self.text_phrases[4]))