Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. # utility functions go here
  2. import random
  3. import os
  4. from uuid import uuid4
  5.  
  6. import constants
  7. from models import Raffle, User
  8. from flask_mail import Message
  9. from flask_mail import Mail
  10. from mail import mail
  11.  
  12. def generate_raffles(count):
  13. for i in xrange(count):
  14. colour = constants.COLORS[i % constants.COLORS_LEN]
  15. uniq = uuid4().hex
  16. uniq_p1, uniq_p2 = uniq[:4], uniq[4:8]
  17. yield (colour, uniq_p1, uniq_p2)
  18.  
  19.  
  20. def seed_raffles_into_db(max_raffles=constants.MAX_RAFFLES):
  21. if is_inited():
  22. print 'Raffles have already been seeded...'
  23. return False
  24. from app import db
  25. print 'Seeding raffles...'
  26. for raffle_colour, raffle_up1, raffle_up2 in generate_raffles(max_raffles):
  27. raffle = Raffle(
  28. colour=raffle_colour,
  29. up1=raffle_up1,
  30. up2=raffle_up2,
  31. )
  32. print "Adding", raffle
  33. db.session.add(raffle)
  34.  
  35. db.session.commit()
  36. mark_as_inited()
  37. return True
  38.  
  39.  
  40. def get_unused_raffles(raffle_count):
  41. return (
  42. Raffle.query.filter_by(
  43. user=None
  44. ).limit(
  45. constants.RAFFLE_PADDING + raffle_count
  46. )
  47. ).all()
  48.  
  49.  
  50. def mark_as_inited():
  51. open(constants.INIT_FILE_PATH, 'w').close()
  52.  
  53.  
  54. def is_inited():
  55. return os.path.exists(constants.INIT_FILE_PATH)
  56.  
  57.  
  58. def assign_raffles_to_user(raffle_count, user):
  59. from app import db
  60. raffles = get_unused_raffles(raffle_count)
  61.  
  62. for raffle in random.sample(raffles, raffle_count):
  63. print "Assigning {0} to {1}".format(raffle, user)
  64. msg = Message('Raffle assigned', sender = 'osman.soloking009@outlook.com', recipients = [user.email])
  65. msg.body = myRaffle = "Assigning {0} to {1}".format(raffle, user)
  66. mail.send(msg)
  67. raffle.user = user
  68.  
  69.  
  70.  
  71.  
  72.  
  73. db.session.commit()
  74. return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement