2016-10-31 22:11:25 +01:00
|
|
|
from orator.orm import Model
|
|
|
|
from orator.orm import belongs_to
|
2016-11-01 22:01:40 +01:00
|
|
|
from orator.orm import has_many
|
|
|
|
|
2016-11-13 12:52:16 +01:00
|
|
|
from src.utils import random_element
|
2016-11-12 13:37:14 +01:00
|
|
|
import src.entity.reply
|
|
|
|
import src.entity.word
|
2016-11-07 21:06:30 +01:00
|
|
|
|
2016-10-31 22:11:25 +01:00
|
|
|
|
|
|
|
class Pair(Model):
|
2016-11-12 23:25:58 +01:00
|
|
|
__fillable__ = ['chat_id', 'first_id', 'second_id']
|
2016-11-12 15:53:18 +01:00
|
|
|
__timestamps__ = ['created_at']
|
2016-11-01 22:01:40 +01:00
|
|
|
|
2016-10-31 22:11:25 +01:00
|
|
|
@has_many
|
|
|
|
def replies(self):
|
2016-11-12 13:37:14 +01:00
|
|
|
return src.entity.reply.Reply
|
2016-10-31 22:11:25 +01:00
|
|
|
|
2016-11-02 18:53:19 +01:00
|
|
|
@belongs_to
|
|
|
|
def first(self):
|
2016-11-12 13:37:14 +01:00
|
|
|
return src.entity.word.Word
|
2016-11-02 18:53:19 +01:00
|
|
|
|
|
|
|
@belongs_to
|
|
|
|
def second(self):
|
2016-11-12 13:37:14 +01:00
|
|
|
return src.entity.word.Word
|
2016-11-02 18:53:19 +01:00
|
|
|
|
2016-11-01 22:01:40 +01:00
|
|
|
@staticmethod
|
2016-11-12 23:25:58 +01:00
|
|
|
def get_random_pair(chat_id, first_id, second_id_list):
|
2016-11-13 12:52:16 +01:00
|
|
|
pairs = Pair\
|
2016-11-12 23:25:58 +01:00
|
|
|
.with_({
|
|
|
|
'replies': lambda q: q.order_by('count', 'desc').limit(3)
|
|
|
|
})\
|
2016-11-02 18:53:19 +01:00
|
|
|
.where('chat_id', chat_id)\
|
|
|
|
.where('first_id', first_id)\
|
2016-11-12 23:25:58 +01:00
|
|
|
.where_in('second_id', second_id_list)\
|
2016-11-13 12:52:16 +01:00
|
|
|
.limit(3)\
|
|
|
|
.get()\
|
|
|
|
.all()
|
|
|
|
|
|
|
|
return random_element(pairs)
|