Reputation:
Consider the following:
I'm making a secret santa script for my family for next year using python, and although I have figured out how to draw names (see code below), I need to execute it just once. I'm planning to use the flask framework to create a page counts down the days until the draw (which I've figured out as well), but how do i make it so it does the draw, just once, and not everytime somebody logs on to that page? Get what I mean?
Anyway, I'll show you my code here:
# imports & initial Flask set up above
def do_matchup(names, draw, matches=None):
if matches is None:
matches = []
while names:
member = names.pop()
recipient = choice(draw)
if recipient != member:
matches.append("%s=%s" % (member, recipient))
draw.remove(recipient)
else:
names.append(member)
return matches
@app.route("/")
def index():
now = dt.datetime.now()
draw_date = dt.datetime.strptime('10/1/2013', '%m/%d/%Y')
days = draw_date - now
family = ["member1", "member2", "member3", "member4", "member5", "Me"]
hat_names = [name for name in family]
matchup = do_matchup(family, hat_names)
return render_template("base.html", now=now,
draw_date=draw_date,
days=days,
matchup=matchup)
The template is a basic html page that says {% if now < draw_date %} "There are x-amount of days until the draw", {% else %} "Show the draw results".
Every time the page is loaded, it does a new draw. How do I make it so it just does the draw once and not give every family member different results?
Upvotes: 0
Views: 100
Reputation: 92647
If you are not using a database and just need this small amount of saved results, you could just pickle the matchup to a file. Then in your index you just check if the file exists and if so, read the file and return that matchup. Otherwise you generate a new matchup, save it, and return results.
Upvotes: 1
Reputation: 49886
You need to have a separate way of running the shuffle - e.g. by visiting a particular page. That code should also save the shuffle to the database. Another way is to check if there is already a shuffle for the current year, and only to generate and save it if not.
Fundamentally, the answer is to save your shuffle in e.g. a database.
Upvotes: 1