def post(self, club_id): # check credentials if not(check_admin(club_id)): add_notify("Error", "You do not have the appropriate permissions") self.redirect("/") return club = Club.get_by_key_name(club_id) # make a flyer flyer, made = None, None while not(flyer) or not(made): # randomly generate a flyer key flyer_key = generate_random_hash(club_id)[:10] flyer, made = get_or_make(Flyer, flyer_key) flyer.id = flyer_key # get the parameters file_obj = self.request.POST["flyer"] file_name = file_obj.filename flyer_name = self.request.get("name") event_date = self.request.get("date") if not(flyer_name): flyer_name = file_name[:-4] # check if the filename is a pdf if file_name[-3:] != "pdf": add_notify("Error", "File is not a pdf") self.redirect("/flyer/%s" % club.slug) return # fetch the blobstore key, save it upload = self.get_uploads("flyer") logging.info(upload) flyer.flyer = upload[0] # set everything else flyer.name = flyer_name flyer.club = club flyer.upload_date = datetime.today() # change the time to right before midnight event_date = datetime.strptime(event_date, "%Y/%m/%d") event_date = event_date.replace(hour=23, minute=59) flyer.event_date = event_date flyer.put() # make a bunch of jobs from the club and flyer emails = [e.email for e in prefetch_refprop(club.emails, EmailToClub.club)] for email in emails: # generate a key job_obj, made = None, None while not(job_obj) or not(made): # randomly generate a key job_key = generate_random_hash(str(email)) job_obj, made = get_or_make(Job, job_key) if made: job_obj.id = job_key job_obj.flyer = flyer job_obj.email = email job_obj.put() # and write out the response self.response.out.write(template.render("templates/finish_upload.html", {}))
def get(self, club_id, email_id): # check credentials if not(check_admin(club_id)): add_notify("Error", "You do not have the appropriate permissions") self.redirect("/") return user = users.get_current_user() club = Club.get_by_key_name(club_id) # get the email to be admin-ified email = Email.get_by_key_name(email_id) if not(email): add_notify("Error", "No email to be promoted!") self.redirect("/club/%s" % club.slug) return # find the link to promote it query = EmailToClub.all() query.filter("email =", email) query.filter("club =", club) link = query.get() if not(link): add_notify("Error", "No email to be promoted!") self.redirect("/club/%s" % club.slug) return if not link.enable: add_notify("Error", "Can't promote a disabled email") self.redirect("/club/%s" % club.slug) return # flip the admin bit link.admin = not(link.admin) link.put() # make sure we have at least one admin query = EmailToClub.all() query.filter("club =", club) query.filter("admin =", True) admin_check = query.get() if not(admin_check): # reverse the admin link.admin = True link.put() # send an email if you've just been promoted if link.admin: domain = "http://%s.appspot.com" % get_application_id() msg = mail.EmailMessage() fromaddr = "noreply@%s.appspotmail.com" % get_application_id() msg.sender = "Flyer Guy <%s>" % fromaddr msg.to = email.email msg.subject = "[Flyer] You've an admin of %s!" % club.name msg.html = template.render("templates/email_admin.html", {"domain":domain, "club":club.name, "email":email.id}) try: msg.send() except apiproxy_errors.OverQuotaError, (message,): # Log the error add_notify("Error", "Could not send email") logging.error("Could not send email") logging.error(message)
def check_admin(club_id): """See if we have admin access to a certain club's pages""" user = users.get_current_user() email = get_email(user) club = Club.get_by_key_name(club_id) if not(user) or not(email) or not(club) or not(email.user_enable): return False # do a joint query query = EmailToClub.all() query.filter("email =", email) query.filter("club =", club) joint = query.get() if not(joint): return False return joint.admin
def get(self, name): c = Club.get_by_key_name(name) if c is None: self.response.set_status(404) t = JINJA2_ENV.get_template('404.html') self.response.out.write(t.render({})) return cal = CalendarWrap(c.display) q = Match.all().filter('home = ', c) for match in q: cal.add(match) q = Match.all().filter('away = ', c) for match in q: cal.add(match) self.response.headers["Content-Type"] = "text/calendar; charset=utf-8" self.response.out.write(cal.to_string())
def get(self, club_id): # check credentials if not(check_admin(club_id)): add_notify("Error", "You do not have the appropriate permissions") self.redirect("/") return club = Club.get_by_key_name(club_id) values = {"name": club.name, "slug": club.slug} session = get_current_session() if session: values["notifications"] = session["notify"] session["notify"] = None # create a blobstore url upload_url = blobstore.create_upload_url('/flyer/%s' % club.slug) values["upload_url"] = upload_url self.response.out.write(template.render("templates/upload.html", values))
def post(self, club_id): # check credentials if not(check_admin(club_id)): add_notify("Error", "You do not have the appropriate permissions") self.redirect("/") return user = users.get_current_user() club = Club.get_by_key_name(club_id) email = get_email(user) # add emails email_block = self.request.get("newemails") emails_raw = [e for e in re.split("[\s\,\n]", email_block) if e] emails = [normalize_email(e) for e in emails_raw] emails = [e for e in emails if e] for email in emails: # add a suffix email = normalize_email(email) # use a hash for emails, assume it is unique email_key = generate_hash(email)[:10] email_obj, made = get_or_make(Email, email_key) if not(made): logging.info("Email already in the system") continue email_obj.email = email email_obj.id = email_key email_obj.put() # make sure this pair is unique query = EmailToClub.all() query.filter('email =', email_obj) query.filter('club =', club) join = query.get() if not(join): join = EmailToClub(email=email_obj, club=club) join.put() # create message if emails: add_notify("Notice", "Emails added") if len(emails) != len(emails_raw): add_notify("Notice", "Not all emails added") self.redirect("/club/%s" % club.slug)
def get(self, club_id): """Get the editor page""" # check credentials if not(check_admin(club_id)): add_notify("Error", "You do not have the appropriate permissions") self.redirect("/") return user = users.get_current_user() club = Club.get_by_key_name(club_id) # prefetch the emails email_refs = club.emails email_refs = [e for e in email_refs] # check enabled emails = [e.email for e in prefetch_refprop(email_refs, EmailToClub.email)] email_addrs = [e.email for e in emails] email_ids = [e.id for e in emails] status = [] for eref in email_refs: if not(eref.enable): status.append(-1) elif eref.admin: status.append(1) else: status.append(0) messages = [e.message for e in email_refs] email_info = zip(email_ids, status, email_addrs, messages) # sort on status (admin first, opt-out last) email_info.sort(key=itemgetter(1), reverse=True) # pack up the values vals = {"emails": email_info, "club": club.name, "clubslug": club.slug} # get the notifications session = get_current_session() if session: vals["notifications"] = session["notify"] session["notify"] = None self.response.out.write(template.render("templates/club_edit.html", vals))
def get(self, club_id, email_id): # check credentials if not(check_admin(club_id)): add_notify("Error", "You do not have the appropriate permissions") self.redirect("/") return club = Club.get_by_key_name(club_id) # get the email link to be deleted email = Email.get_by_key_name(email_id) if not(email): add_notify("Error", "No email to be deleted!") self.redirect("/club/%s" % club.slug) return # find the link query = EmailToClub.all() query.filter("email =", email) query.filter("club =", club) link = query.get() if not(link): add_notify("Error", "No email to be deleted!") self.redirect("/club/%s" % club.slug) return # make sure it's not the last admin query = EmailToClub.all() query.filter("club =", club) admins = query.fetch(2) if len(admins) < 2 and link.admin: add_notify("Error", "Will not delete the last club admin") self.redirect("/club/%s" % club.slug) return # delete the link link.delete() # hail our success add_notify("Notice", "Email deleted") self.redirect("/club/%s" % club.slug)
def get(self, home, away): h = Club.get_by_key_name(home) a = Club.get_by_key_name(away) params = {'home': h, 'away': a} t = JINJA2_ENV.get_template('match.html') self.response.out.write(t.render(params))