I have a string sentence structure such as this:
Starting in $Duration%, the $Noun$ will be $Adjective$
I need a view to check which words are in “$$” and replace them with a random word from the database which was imported from a SQL file.
Here are the models/sql files:
from django.db import models
from django.utils import timezone
class Type(models.Model):
type = models.CharField(max_length=50)
value = models.CharField(max_length=1)
class Word(models.Model):
dict = models.CharField(max_length=200)
kind = models.CharField(max_length=1)
def randword(self):
words = self.objects.all()
number = randrang(0,len(words))
return words[number]
class Question(models.Model):
question = models.CharField(max_length=200)
def randquest(self):
quests = self.objects.all()
numbeq = randrang(0, len(quests))
return quests[numbeq]
class Meta(models.Model):
question = models.ForeignKey(Question)
score = models.ForeignKey("Score")
user = models.CharField(max_length=200)
time = models.DateTimeField('date published')
class Score(models.Model):
word = models.ForeignKey(Word)
score = models.IntegerField()
question = models.ManyToManyField(Question, through=Meta)
def __unicode__(self):
return self.score
SQL files
INSERT INTO quest_question (question) VALUES ('Starting in $Duration$, the $Noun$ will be $Adjective$');
and
INSERT INTO quest_word (dict, kind) VALUES ('Coffee Maker', '1');
INSERT INTO quest_word (dict, kind) VALUES ('24 hours', '3');
INSERT INTO quest_word (dict, kind) VALUES ('today', '3');
INSERT INTO quest_word (dict, kind) VALUES ('broken', '2');
INSERT INTO quest_word (dict, kind) VALUES ('Email server', '1');
INSERT INTO quest_word (dict, kind) VALUES ('15 minutes', '3');
INSERT INTO quest_word (dict, kind) VALUES ('tomorrow', '3');
INSERT INTO quest_word (dict, kind) VALUES ('unavailable', '2');
INSERT INTO quest_type (type, value) VALUES ('Noun', '1');
INSERT INTO quest_type (type, value) VALUES ('Adjective', '2');
INSERT INTO quest_type (type, value) VALUES ('Duration', '3');
I started writing the view. Need some help with it:
from quest.models import Word, Type, Score, Question, Meta
from django.utils import timezone
def question(request):
sentence = Question.objects.all()
find_words = re.findall(r"\w+%0-9a-zA-Z%", sentence.question)
If you’re willing to change the notation, you have two options:
Use Python.
Use Django.
The rest is pulling from the database and selecting the words at random, which is the easy part.