From 950ad4cc4c061517eaba5acd1dbde65a5a4de432 Mon Sep 17 00:00:00 2001 From: "Brian S. Stephan" Date: Fri, 15 May 2015 17:02:13 -0500 Subject: [PATCH] markov: get_or_create_target_context properly --- dr_botzo/markov/lib.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/dr_botzo/markov/lib.py b/dr_botzo/markov/lib.py index 6c8e310..370a602 100644 --- a/dr_botzo/markov/lib.py +++ b/dr_botzo/markov/lib.py @@ -88,8 +88,15 @@ def get_or_create_target_context(target_name): """Return the context for a provided nick/channel, creating missing ones.""" # find the stuff, or create it - target, c = MarkovTarget.objects.get_or_create(name=target_name) try: + target = MarkovTarget.objects.get(name=target_name) + return target.context + 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 + context, c = MarkovContext.objects.get_or_create(name=target_name) + target, c = MarkovTarget.objects.get_or_create(name=target_name, context=context) + return target.context except MarkovContext.DoesNotExist: # make a context --- lacking a good idea, just create one with this target name until configured otherwise