def edit_class(userid, commishclass): if not commishclass.title: raise error.WeasylError("titleInvalid") try: d.execute("UPDATE commishclass SET title = '%s' WHERE (classid, userid) = (%i, %i)", [commishclass.title, commishclass.classid, userid]) except error.PostgresError: raise error.WeasylError("titleExists")
def create_commission_class(userid, title): if not title: raise error.WeasylError("titleInvalid") classid = d.execute("SELECT MAX(classid) + 1 FROM commishclass WHERE userid = %i", [userid], ["element"]) try: d.execute("INSERT INTO commishclass VALUES (%i, %i, '%s')", [classid if classid else 1, userid, title]) except error.PostgresError: raise error.WeasylError("commishclassExists")
def verify(db, userid, token): # Select purchased terms terms = define.execute( db, "SELECT terms FROM premiumpurchase WHERE token = '%s'", [token], ["element"]) if not terms: raise error.WeasylError("tokenInvalid") # Select current terms current = define.execute( db, "SELECT terms FROM userpremium WHERE userid = %i", [userid], ["element"]) # Update premium status if current: define.execute( db, "UPDATE userpremium SET terms = terms + %i WHERE userid = %i", [terms, userid]) else: define.execute(db, "INSERT INTO userpremium VALUES (%i, %i, %i)", [userid, define.get_time(), terms]) define.execute( db, "UPDATE profile SET config = config || 'd' WHERE userid = %i AND config !~ 'd'", [userid]) define.execute(db, "DELETE FROM premiumpurchase WHERE token = '%s'", [token])
def create_price(userid, price, currency="", settings=""): if not price.title: raise error.WeasylError("titleInvalid") elif price.amount_min > _MAX_PRICE: raise error.WeasylError("minamountInvalid") elif price.amount_max > _MAX_PRICE: raise error.WeasylError("maxamountInvalid") elif price.amount_max and price.amount_max < price.amount_min: raise error.WeasylError("maxamountInvalid") elif not d.execute("SELECT EXISTS (SELECT 0 FROM commishclass WHERE (classid, userid) = (%i, %i))", [price.classid, userid], ["bool"]): raise error.WeasylError("classidInvalid") elif not price.classid: raise error.WeasylError("classidInvalid") # Settings are at most one currency class, and optionally an 'a' to indicate an add-on price. # TODO: replace these character codes with an enum. settings = "%s%s" % ("".join(i for i in currency if i in "epycmu")[:1], "a" if "a" in settings else "") # TODO: should have an auto-increment ID priceid = d.execute("SELECT MAX(priceid) + 1 FROM commishprice WHERE userid = %i", [userid], ["element"]) try: d.execute( "INSERT INTO commishprice VALUES (%i, %i, %i, '%s', %i, %i, '%s')", [priceid if priceid else 1, price.classid, userid, price.title, price.amount_min, price.amount_max, settings]) except error.PostgresError: return error.WeasylError("titleExists")
def edit_price(userid, price, currency="", settings="", edit_prices=False, edit_settings=False): currency = "".join(i for i in currency if i in "epycmu") settings = "".join(i for i in settings if i in "a") query = d.execute("SELECT amount_min, amount_max, settings, classid FROM commishprice" " WHERE (priceid, userid) = (%i, %i)", [price.priceid, userid], options="single") if not query: raise error.WeasylError("priceidInvalid") elif price.amount_min > _MAX_PRICE: raise error.WeasylError("minamountInvalid") elif price.amount_max > _MAX_PRICE: raise error.WeasylError("maxamountInvalid") elif price.amount_max and price.amount_max < price.amount_min: raise error.WeasylError("maxamountInvalid") argv = [] statement = ["UPDATE commishprice SET "] if price.title: statement.append("%s title = '%%s'" % ("," if argv else "")) argv.append(price.title) if edit_prices: if price.amount_min != query[0]: statement.append("%s amount_min = %%i" % ("," if argv else "")) argv.append(price.amount_min) if price.amount_max != query[1]: statement.append("%s amount_max = %%i" % ("," if argv else "")) argv.append(price.amount_max) if edit_settings: statement.append("%s settings = '%%s'" % ("," if argv else "")) argv.append("%s%s" % (currency, settings)) if not argv: return statement.append(" WHERE (priceid, userid) = (%i, %i)") argv.extend([price.priceid, userid]) d.execute("".join(statement), argv)
def append(db, email, terms): token = security.generate_key(40) email = emailer.normalize_address(email) if not email: raise error.WeasylError("emailInvalid") define.execute(db, "INSERT INTO premiumpurchase VALUES ('%s', '%s', %i)", [token, email, terms]) emailer.append([email], None, "Weasyl Premium Verification", define.render("email/verify_premium.html", [token, terms]))
def append(mailto, mailfrom, subject, content, displayto=None): """Send an e-mail. `mailto` must be a list of e-mail addresses to send this e-mail to. If `mailfrom` is None, the system email will be designated as the sender. Otherwise, `mailfrom` must be a single e-mail address. The 'To' header of the e-mail will be a comma-separated list of the `mailto` addresses unless `displayto` is not None (in which case it will be set to `displayto`.) """ if not mailfrom: mailfrom = macro.MACRO_EMAIL_ADDRESS mailfrom = normalize_address(mailfrom) subject = subject.strip() content = content.strip() if not mailto: raise error.WeasylError("mailtoInvalid") elif not mailfrom: raise error.WeasylError("mailfromInvalid") elif not content: raise error.WeasylError("contentInvalid") if not subject: subject = "None" message = email.mime.text.MIMEText(content.strip()) if displayto is not None: message["To"] = displayto else: message["To"] = ', '.join(mailto) message["From"] = mailfrom message["Subject"] = subject sendmail_args = ['sendmail'] + list(mailto) proc = subprocess.Popen(sendmail_args, stdin=subprocess.PIPE) proc.communicate(message.as_string()) if proc.returncode: raise subprocess.CalledProcessError(proc.returncode, sendmail_args) define.metric('increment', 'emails')