From 333424025b9e14e6d6f748e973cb089a2717e524 Mon Sep 17 00:00:00 2001 From: Brian Stephan Date: Thu, 4 May 2023 17:21:27 -0500 Subject: [PATCH] support markov targets with identical names on different servers markov targets are queried and autogenerated based on chatter, but had a legacy name which is no longer in use for this, preferring the foreign keys to channel and consequently server. the name is really just informative these days, but was still being used to find targets, and thus was breaking when two servers had the same channel name in them. this fixes that --- ircbot/models.py | 2 +- markov/ircplugin.py | 4 ++-- .../migrations/0008_alter_markovtarget_name.py | 18 ++++++++++++++++++ markov/models.py | 4 ++-- 4 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 markov/migrations/0008_alter_markovtarget_name.py diff --git a/ircbot/models.py b/ircbot/models.py index 7abe044..8cc89c7 100644 --- a/ircbot/models.py +++ b/ircbot/models.py @@ -118,7 +118,7 @@ class IrcChannel(models.Model): def __str__(self): """Provide string representation.""" - return "{0:s}".format(self.name) + return "{0:s} on {1:s}".format(self.name, self.server.name) class IrcPlugin(models.Model): diff --git a/markov/ircplugin.py b/markov/ircplugin.py index f226bb3..e21a325 100644 --- a/markov/ircplugin.py +++ b/markov/ircplugin.py @@ -119,12 +119,12 @@ class Markov(Plugin): target_name = target_name.lower() # find the stuff, or create it + channel, c = IrcChannel.objects.get_or_create(name=target_name, server=self.connection.server_config) try: - target = MarkovTarget.objects.get(name=target_name) + target = MarkovTarget.objects.get(channel=channel) except MarkovTarget.DoesNotExist: # we need to create a context and a target, and we have to make the context first # make a context --- lacking a good idea, just create one with this target name until configured otherwise - channel, c = IrcChannel.objects.get_or_create(name=target_name, server=self.connection.server_config) context, c = MarkovContext.objects.get_or_create(name=target_name) target, c = MarkovTarget.objects.get_or_create(name=target_name, context=context, channel=channel) diff --git a/markov/migrations/0008_alter_markovtarget_name.py b/markov/migrations/0008_alter_markovtarget_name.py new file mode 100644 index 0000000..a91a4bf --- /dev/null +++ b/markov/migrations/0008_alter_markovtarget_name.py @@ -0,0 +1,18 @@ +# Generated by Django 3.2.18 on 2023-05-04 22:17 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('markov', '0007_alter_markovtarget_channel'), + ] + + operations = [ + migrations.AlterField( + model_name='markovtarget', + name='name', + field=models.CharField(max_length=200), + ), + ] diff --git a/markov/models.py b/markov/models.py index 56b91bd..8eaf316 100644 --- a/markov/models.py +++ b/markov/models.py @@ -21,7 +21,7 @@ class MarkovContext(models.Model): class MarkovTarget(models.Model): """Define IRC targets that relate to a context, and can occasionally be talked to.""" - name = models.CharField(max_length=200, unique=True) + name = models.CharField(max_length=200) context = models.ForeignKey(MarkovContext, on_delete=models.CASCADE) channel = models.ForeignKey(IrcChannel, on_delete=models.CASCADE) @@ -29,7 +29,7 @@ class MarkovTarget(models.Model): def __str__(self): """Provide string representation.""" - return "{0:s} -> {1:s}".format(self.name, self.context.name) + return "{0:s} -> {1:s}".format(str(self.channel), self.context.name) class MarkovState(models.Model):