def count_statistics_words_update(task): lock = FileLock("/home/senderma/projects/banalnosti/count_statistics_words") with lock: if len(StatisticsWords.objects.filter(gameID = task.gameID, tourID = task.tourID)) == 0: for tourID in range(0, task.tourID + 1): verbosePlayers = Words.objects.filter(gameID = task.gameID, tourID = tourID).values('user').annotate(countWords=Count('user')).order_by() for verbosePlayer in verbosePlayers: wordsPlayer = Words.objects.filter(gameID = task.gameID, tourID = tourID, user = verbosePlayer['user']).order_by('-id') if verbosePlayer['countWords'] > 10 or len(set(wordsPlayer)) != len(wordsPlayer): wordsPlayer = map(lambda x: x.word, wordsPlayer[:10]) wordsPlayer = list(set(wordsPlayer)) Words.objects.filter(gameID = task.gameID, tourID = tourID, user = verbosePlayer['user']).delete() for word in wordsPlayer: wordNew = Words(gameID = task.gameID, tourID = tourID, user = User.objects.get(id = verbosePlayer['user']), word = word) wordNew.save() uniqueWords = Words.objects.filter(gameID = task.gameID, tourID = tourID).values('word').annotate(count=Count('word')).order_by() for word in uniqueWords: statisticsWordsNew = StatisticsWords(gameID = task.gameID, tourID = tourID, word = word['word'], count = word['count'], score = word['count'] - 1, legal = 1) statisticsWordsNew.save()