def add_word_to_learned(self, word_name): from easy_english.models import UserForeignWord words = UserForeignWord.objects.filter( Q(user=self.user) & Q(foreign_word__iexact=word_name) ).all() if not words: word = UserForeignWord() word.user = self.user word.foreign_word = word_name word.translation = '' word.is_learned = True word.save() else: for w in words: w.is_learned = True w.save()
def create(self, validated_data): from easy_english.models import (UserForeignWord, UserWordContext) foreign_word = UserForeignWord() foreign_word.user = validated_data['user'] foreign_word.foreign_word = validated_data['foreign_word'] foreign_word.translation = validated_data['translation'] foreign_word.save() for c in validated_data['contexts']: context = UserWordContext(context=c, user_word=foreign_word) context.save() threading.Thread( target=self.download_pronunciation, args=(foreign_word,) ).start() return foreign_word