Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
State machine for handlers and chats. This way only some handler migh…
…t apply
- Loading branch information
Showing
13 changed files
with
791 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# -*- coding: utf-8 -*- | ||
# Generated by Django 1.9.4 on 2016-03-29 10:16 | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('microbot', '0007_auto_20160328_1108'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='ChatState', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('chat', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to='microbot.Chat', verbose_name='Chat')), | ||
], | ||
options={ | ||
'verbose_name': 'Chats States', | ||
}, | ||
), | ||
migrations.CreateModel( | ||
name='State', | ||
fields=[ | ||
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('name', models.CharField(db_index=True, max_length=255, verbose_name='State name')), | ||
('bot', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='states', to='microbot.Bot', verbose_name='Bot')), | ||
], | ||
options={ | ||
'verbose_name': 'State', | ||
'verbose_name_plural': 'States', | ||
}, | ||
), | ||
migrations.AddField( | ||
model_name='chatstate', | ||
name='state', | ||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='chat', to='microbot.State', verbose_name='State'), | ||
), | ||
migrations.AddField( | ||
model_name='handler', | ||
name='source_states', | ||
field=models.ManyToManyField(related_name='source_handlers', to='microbot.State', verbose_name='Source States'), | ||
), | ||
migrations.AddField( | ||
model_name='handler', | ||
name='target_state', | ||
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='target_handlers', to='microbot.State', verbose_name='Target State'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# -*- coding: utf-8 -*- | ||
from django.db import models | ||
from django.utils.encoding import python_2_unicode_compatible | ||
from django.utils.translation import ugettext_lazy as _ | ||
import logging | ||
from microbot.models import Chat | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
@python_2_unicode_compatible | ||
class State(models.Model): | ||
name = models.CharField(_('State name'), db_index=True, max_length=255) | ||
bot = models.ForeignKey('Bot', verbose_name=_('Bot'), related_name='states') | ||
|
||
class Meta: | ||
verbose_name = _('State') | ||
verbose_name_plural = _('States') | ||
|
||
def __str__(self): | ||
return "%s" % self.name | ||
|
||
|
||
@python_2_unicode_compatible | ||
class ChatState(models.Model): | ||
chat = models.OneToOneField(Chat, db_index=True, verbose_name=_('Chat')) | ||
state = models.ForeignKey(State, verbose_name=_('State'), related_name='chat') | ||
|
||
class Meta: | ||
verbose_name = _('Chat State') | ||
verbose_name = _('Chats States') | ||
|
||
def __str__(self): | ||
return "(%s:%s)" % (str(self.chat.id), self.state.name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# coding=utf-8 | ||
from factory import DjangoModelFactory, SubFactory, Sequence | ||
from microbot.models import State, ChatState | ||
from microbot.test.factories import ChatLibFactory, BotFactory | ||
|
||
class StateFactory(DjangoModelFactory): | ||
class Meta: | ||
model = State | ||
bot = SubFactory(BotFactory) | ||
name = Sequence(lambda n: 'state_%d' % n) | ||
|
||
class ChatStateFactory(DjangoModelFactory): | ||
class Meta: | ||
model = ChatState | ||
chat = SubFactory(ChatLibFactory) | ||
state = SubFactory(StateFactory) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# coding=utf-8 | ||
from factory import DjangoModelFactory, Sequence, SubFactory | ||
from factory.fuzzy import FuzzyText | ||
from microbot.models import User, Chat, Message, Update | ||
from django.utils import timezone | ||
|
||
class UserAPIFactory(DjangoModelFactory): | ||
class Meta: | ||
model = User | ||
id = Sequence(lambda n: n+1) | ||
first_name = Sequence(lambda n: 'first_name_%d' % n) | ||
last_name = Sequence(lambda n: 'last_name_%d' % n) | ||
username = Sequence(lambda n: 'username_%d' % n) | ||
|
||
class ChatAPIFactory(DjangoModelFactory): | ||
class Meta: | ||
model = Chat | ||
id = Sequence(lambda n: n+1) | ||
type = "private" | ||
title = Sequence(lambda n: 'title_%d' % n) | ||
username = Sequence(lambda n: 'username_%d' % n) | ||
first_name = Sequence(lambda n: 'first_name_%d' % n) | ||
last_name = Sequence(lambda n: 'last_name_%d' % n) | ||
|
||
class MessageAPIFactory(DjangoModelFactory): | ||
class Meta: | ||
model = Message | ||
message_id = Sequence(lambda n: n+1) | ||
from_user = SubFactory(UserAPIFactory) | ||
date = timezone.now() | ||
chat = SubFactory(ChatAPIFactory) | ||
text = FuzzyText() | ||
|
||
class UpdateAPIFactory(DjangoModelFactory): | ||
class Meta: | ||
model = Update | ||
update_id = Sequence(lambda n: n+1) | ||
message = SubFactory(MessageAPIFactory) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.