Skip to content

Commit

Permalink
Posting new emails works
Browse files Browse the repository at this point in the history
  • Loading branch information
thenoviceoof committed Jan 22, 2012
1 parent 73136cd commit 4a8feb4
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 52 deletions.
2 changes: 1 addition & 1 deletion appengine_config.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
def webapp_add_wsgi_middleware(app): def webapp_add_wsgi_middleware(app):
app = SessionMiddleware(app, app = SessionMiddleware(app,
cookie_key="d41d8cd98f00b204e9800998ecf8427e", cookie_key="d41d8cd98f00b204e9800998ecf8427e",
lifetime=datetime.timedelta(hours=1)) lifetime=datetime.timedelta(hours=100))
return app return app
130 changes: 81 additions & 49 deletions flyer.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -45,6 +45,37 @@ def add_notify(title, body):
else: else:
session["notify"] = cur session["notify"] = cur


def get_or_make(Obj, key_name):
"""Retrieves or creates the object keyed on key_name
Returns tuple with object and boolean indicating whether it was created"""
def txn(key_name):
made = False
entity = Obj.get_by_key_name(key_name)
if entity is None:
entity = Obj(key_name=key_name)
entity.put()
made = True
return (entity, made)
return db.run_in_transaction(txn, key_name)

def check_admin(club_id):
"""See if we have admin access to a certain club's pages"""
session = get_current_session()
if not(session.is_active()):
raise Exception("Session is not active")
token_id = session.get("user", None)
if not(token_id):
raise Exception("No user in the session")
token = Token.get_by_key_name(token_id)
club = Club.get_by_key_name(club_id)
# do a joint query
query = TokenToClub.all()
query.filter("token =", token)
query.filter("club =", club)
joint = query.fetch(1)
if not(joint):
raise Exception("Not an admin")

################################################################################ ################################################################################


# either front page or choose organization # either front page or choose organization
Expand Down Expand Up @@ -86,31 +117,36 @@ def get(self):
class Flyer(BaseHandler): class Flyer(BaseHandler):
# serves up the flyer upload form # serves up the flyer upload form
def get(self, club_id): def get(self, club_id):
# check credentials
check_admin(club_id)

club = Club.get(club_id) club = Club.get(club_id)


values = {"name": club.name} values = {"name": club.name}
self.response.out.write(template.render("templates/upload.html", values)) self.response.out.write(template.render("templates/upload.html", values))


# handles the flyer upload # handles the flyer upload
def post(self, club_id): def post(self, club_id):
# check credentials
check_admin(club_id)

# get the club # get the club
club = Club.get(club_id) club = Club.get(club_id)


# make a flyer # make a flyer
flyer = None flyer = None
while not(flyer): while not(flyer):
# randomly generate a flyer key # randomly generate a flyer key
# ?? should we do this? don't want to leak insert times flyer_key = generate_hash(club_id)[:6]
flyer_key = generate_hash(club_id)[:5] flyer, made = get_or_make(Flyer, flyer_key)
flyer = Flyer.get_or_insert(flyer_key) if not(made):
if flyer.name:
flyer = None flyer = None
name = self.request.get("name") name = self.request.get("name")
# check if the filename is a pdf # check if the filename is a pdf
if name[-3:] != "pdf": if name[-3:] != "pdf":
# !!! replace this with something more useful # !!! replace this with something more useful
raise Exception("File must be a PDF") raise Exception("File must be a PDF")
flyer.name = name flyer.name = name[:-4]
pdf = self.request.get("flyer") pdf = self.request.get("flyer")
flyer.flyer = db.Blob(pdf) flyer.flyer = db.Blob(pdf)
flyer.put() flyer.put()
Expand Down Expand Up @@ -179,15 +215,7 @@ def post(self):
# convert to slug # convert to slug
clubslug = slugify(clubname) clubslug = slugify(clubname)
# basics of get_or_insert, with insertion # basics of get_or_insert, with insertion
def txn(key_name): club, made = get_or_make(Club, clubslug)
made = False
entity = Club.get_by_key_name(key_name)
if entity is None:
entity = Club(key_name=key_name)
entity.put()
made = True
return (entity, made)
club, made = db.run_in_transaction(txn, clubslug)
if not(made): if not(made):
# generate an error # generate an error
add_notify("Error", "That particular name is taken. Sorry!") add_notify("Error", "That particular name is taken. Sorry!")
Expand All @@ -205,56 +233,61 @@ def txn(key_name):


class ClubEdit(BaseHandler): class ClubEdit(BaseHandler):
def get(self, club_id): def get(self, club_id):
# !!! check credentials # check credentials
check_admin(club_id)
session = get_current_session()

# getting the editor # getting the editor
club = Club.get_by_key_name(club_id) club = Club.get_by_key_name(club_id)
emails = list(club.emails) email_refs = club.emails
vals = {"emails": emails, "club": club.name} # prefetch the emails
emails = [e.email
for e in prefetch_refprop(email_refs, EmailToClub.email)]
vals = {"emails": emails,
"club": club.name,
"notifications": session["notify"]}
session["notify"] = None
self.response.out.write(template.render("templates/club_edit.html", self.response.out.write(template.render("templates/club_edit.html",
vals)) vals))


def post(self, club_id): def post(self, club_id):
# !!! check credentials # check credentials
check_admin(club_id)


# get club # get club
club = Club.get_by_key_name(club_id) club = Club.get_by_key_name(club_id)
# add emails # add emails
email_block = self.request.get("newemails") email_block = self.request.get("newemails")
emails = [r for e in re.split("\s\,", email_block) if r] emails = [e for e in re.split("[\s\,\n]", email_block) if e]
for email in emails: for email in emails:
email_obj = Email.get_or_insert(email) # search for the email
if not(email.email): query = Email.all()
email_obj.email = email query.filter('email = ', email)
email_obj.put() email_obj = query.fetch(1)
join = EmailToClub(email=email_obj, club=club) # if there's not already an email_obj, create it
join.put() while not(email_obj):
email_key = generate_hash(email)[:6]
email_obj, made = get_or_make(Email, email_key)
if not(made):
email_obj = None
else:
email_obj.email = email
email_obj.put()
# make sure this pair is unique
query = EmailToClub.all()
query.filter('email =', email_obj)
query.filter('club =', club)
join = query.fetch(1)
if not(join):
join = EmailToClub(email=email_obj, club=club)
join.put()


# !!! remove emails # !!! remove emails
# !!! update attached messages # !!! update attached messages


# !!! figure out what to do with this code
email_list = self.request.get("email_list")
club = Club.get(club)
emails = email_list.split(",")
cur_emails = [e.email for e in club.emails.fetch(100)]
add_emails = [e for e in emails if not(e in cur_emails)]
rem_emails = [e for e in cur_emails if not(e in emails)]
# accumulate var
email_rels = []
for email_addr in add_emails:
email = Email.get_or_insert(email_addr)
email_rel = Email2Club(email = email, club = club)
email_rels.append(email_rel)
db.put(email_rels)
for email_addr in rem_emails:
# !!!
pass

# create message # create message
vals = {"emails":"###", add_notify("Notice", "Emails added")
"message":"Created successfully"} self.redirect("/club/%s" % club.slug)
self.response.out.write(template.render("templates/club_edit.html",
vals))


class AttachGoogleAccount(BaseHandler): class AttachGoogleAccount(BaseHandler):
# for allowing expedient usage: auto-sign in users # for allowing expedient usage: auto-sign in users
Expand All @@ -263,8 +296,7 @@ def get(self):
self.redirect("/") self.redirect("/")


class StopClubMail(BaseHandler): class StopClubMail(BaseHandler):
# !!! have to add a club_id def get(self, club_id, email_id):
def get(self, email_id):
email = Email.get(email_id) email = Email.get(email_id)
q = Job.all() q = Job.all()
q.filter("email =", email) q.filter("email =", email)
Expand Down
4 changes: 4 additions & 0 deletions models.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class Email(db.Model):
enable = db.BooleanProperty(default=True) enable = db.BooleanProperty(default=True)


class Flyer(db.Model): class Flyer(db.Model):
# stores the hash in a easy to access place
id = db.StringProperty()
# a flyer belongs to only one club # a flyer belongs to only one club
club = db.ReferenceProperty(Club) club = db.ReferenceProperty(Club)
# human-readable handle - used in nag emails # human-readable handle - used in nag emails
Expand All @@ -44,6 +46,8 @@ class Flyer(db.Model):


# a many-many link between flyers and emails # a many-many link between flyers and emails
class Job(db.Model): class Job(db.Model):
# stores the hash in a easy to access place
id = db.StringProperty()
# references # references
flyer = db.ReferenceProperty(Flyer, collection_name="jobs") flyer = db.ReferenceProperty(Flyer, collection_name="jobs")
email = db.ReferenceProperty(Email, collection_name="jobs") email = db.ReferenceProperty(Email, collection_name="jobs")
Expand Down
5 changes: 3 additions & 2 deletions templates/club_edit.html
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ <h2 id="noemails">oops, no members yet! Add some below!</h2>
<div id="newwrapper"> <div id="newwrapper">
<h2>Add emails to the list</h2> <h2>Add emails to the list</h2>
<p>Copy paste the list of emails below</p> <p>Copy paste the list of emails below</p>
<form id="newmembers" method="post" action="/new-club"> <form id="newmembers" method="post">
<textarea name="newemails"></textarea> <textarea name="newemails"></textarea>
<button>Add emails</button> <button>Add emails</button>
</form> </form>
Expand All @@ -33,6 +33,7 @@ <h2>Add emails to the list</h2>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">
</script> </script>
<script type="text/javascript"> <script type="text/javascript">
$("#newmembers").submit(); // !!! $("#newmembers").submit(function(){
}); // !!!
</script> </script>
{% endblock %} {% endblock %}

0 comments on commit 4a8feb4

Please sign in to comment.