def searchmember(): """ Search for a member in the DB. The service searches multiple fields, including first name, last name, and handle. Return any matching member in an array. Accepted methods: GET Inputs: search_str The string to search Returns: {"rows": [{"firstName": "string", "string": "DFM001", "lastName": "string", "paymentAmount": float, "middleInitial": "string", "contact_email": "string", "onAutoPay": bool, "active": bool, "paypal_email": "string", "id": int}]} or error """ try: # Will throw KeyError if vars not present search_str = request.args['search_str'] # Will throw KeyError if present but empty if not search_str: raise KeyError except KeyError: return DenhacJsonLibrary.ReplyWithError("search_str is required!") memberDb = DenhacMemberDb() resultList = memberDb.searchMemberName(search_str) return DenhacJsonLibrary.ObjToJson(dict(rows=resultList))
def before_request(): """ Check for an active session and for proper permissions. This function is implicitly called at the beginning of every app.route call below. It returns http 500 with a permissions error if the user does not have the proper level of access. """ admin_services = [] manager_services = [ 'memberpaymentreport', 'createMember', 'readMembers', 'readMember', 'createpayment', 'searchmember', 'getopenbalances', 'getmember', 'getmemberbalance', 'importpaypaldata', 'createmember', 'editmember' ] # Ensure member is logged in to access any APIs if 'logged_in' not in session and request.endpoint != 'login' and request.endpoint != 'main': return DenhacJsonLibrary.ReplyWithError("You must be logged in.") # Ensure that admin services have admin privileges if request.endpoint in admin_services and not session['isAdmin']: return DenhacJsonLibrary.ReplyWithError( "You do not have permissions for this service.") # Ensure that manager services have manager privileges if request.endpoint in manager_services and not session['isManager']: return DenhacJsonLibrary.ReplyWithError( "You do not have permissions for this service.")
def createpayment(): """ Create a payment against a specific member's account. Access Level: Manager Accepted methods: GET Inputs: member_id The member ID in the DB amount The amount to credit(+) or debit(-) payment_type_id Links to an ID in the payment_type table notes The memo field for the transaction Returns: {"created": "True"} or error """ try: # Will throw KeyError if vars not present member_id = request.args['member_id'] amount = request.args['amount'] payment_type_id = request.args['payment_type_id'] notes = request.args['notes'] # Will throw KeyError if present but empty if not member_id or not amount or not payment_type_id or not notes: raise KeyError except KeyError: return DenhacJsonLibrary.ReplyWithError( "member_id and amount and payment_type_id and notes are required!") memberDb = DenhacMemberDb() memberDb.createPayment(member_id, amount, payment_type_id, notes) return DenhacJsonLibrary.ObjToJson(dict(created="True"))
def login(): """ Login a user against the configured LDAP server, and save session data if valid. Access Level: All Accepted methods: POST Inputs: username password Returns: {"logged_in": "True", "username": <username>} or error """ username = None password = None try: if request.method == 'POST': # Will throw KeyError if vars not present username = request.form['username'] password = request.form['password'] if not username or not password: # Detect the condition if a var is there but empty raise KeyError except KeyError: return DenhacJsonLibrary.ReplyWithError( "Username and Password are required!") # myLdap = DenhacLdapLibrary() # try: # myLdap.ldapBind(username, password) try: myFreeIpa = ipa(username=username, password=password, domain=envproperties.ldap_domain, server=envproperties.ldap_server, sslverify=False, logger=app.logger) except: antiBruteForceLogic() return DenhacJsonLibrary.ReplyWithError( "Invalid username or password.") session['logged_in'] = True session['username'] = username session.pop('login_tries', None) # Get this member's permissions from DB, store them in session[], and then check them in @app.before_request() memberDb = DenhacMemberDb() member = memberDb.getMemberByADUsername(username) session['isAdmin'] = member['isAdmin'] session['isManager'] = member['isManager'] return DenhacJsonLibrary.ObjToJson( dict(logged_in="True", username=username))
def getmember(member_id): """ Return a single member row by ID. Access Level: Manager Accepted methods: GET Inputs: member_id Returns: {"row": {"businessNumber": "string", "prox_card_id": "string", "ad_username": "******", "driver_license": "string", "paypal_email": "string", "id": int, "city": "string", "paymentAmount": float, "isAdmin": int, "emerAddress2": "string", "emerAddress1": "string", "phoneNumber": "string", "onAutoPay": int, "emerRelation1": "string", "emerRelation2": "string", "streetAddress2": "string", "streetAddress1": "string", "isManager": int, "zipCode": "string", "contact_email": , "active": int, "emerPhone1": "string", "emerPhone2": "string", "firstName": "string", "gnuCashId": "string", "lastName": "string", "middleInitial": "string", "birthdate": "string", "join_date": "string", "emerContact2": "string", "emerContact1": "string", "balance": float, "medicalConditionList": "string"}} or error """ memberDb = DenhacMemberDb() member = memberDb.getMember(member_id) if member: return DenhacJsonLibrary.ObjToJson(dict(row=member[0])) return DenhacJsonLibrary.ObjToJson(dict())
def getopenbalances(): """ Return an array of members and the balance owed for each. Access Level: Manager Accepted methods: GET Inputs: none Returns: {"rows": [{"businessNumber": "string", "prox_card_id": "string", "ad_username": "******", "driver_license": "string", "paypal_email": "string", "id": int, "city": "string", "paymentAmount": float, "isAdmin": int, "emerAddress2": "string", "emerAddress1": "string", "phoneNumber": "string", "onAutoPay": int, "emerRelation1": "string", "emerRelation2": "string", "streetAddress2": "string", "streetAddress1": "string", "isManager": int, "zipCode": "string", "contact_email": , "active": int, "emerPhone1": "string", "emerPhone2": "string", "firstName": "string", "gnuCashId": "string", "lastName": "string", "middleInitial": "string", "birthdate": "string", "join_date": "string", "emerContact2": "string", "emerContact1": "string", "balance": float, "medicalConditionList": "string"}]} or error """ memberDb = DenhacMemberDb() resultList = memberDb.getOpenBalances() return DenhacJsonLibrary.ObjToJson(dict(rows=resultList))
def editmember(member_id): """ Edit a member in the DB. Access Level: Manager Accepted methods: POST Inputs: ***See the function setFieldsArray() above*** Returns: {"success": "True"} or error """ fields = setFieldsArray() memberDb = DenhacMemberDb() memberDb.editMember(member_id, fields) return DenhacJsonLibrary.ObjToJson(dict(success=True))
def createmember(): """ Create a new member in the DB. Access Level: Manager Accepted methods: POST Inputs: ***See the function setFieldsArray() above*** Returns: {"success": "True"} or error """ fields = setFieldsArray() memberDb = DenhacMemberDb() memberDb.createMember(fields) return DenhacJsonLibrary.ObjToJson(dict(success=True))
def getmemberbalance(member_id): """ Return a single member balance by ID with all transaction rows Access Level: Manager Accepted methods: GET Inputs: member_id Returns: {"balance": float, "rows": [{"amount": float, "transaction_date": "string", "type": "string", "notes": "string"}]} or error """ memberDb = DenhacMemberDb() resultList = memberDb.getMemberBalance(member_id) balance = 0.0 for row in resultList: balance += float(row['amount']) return DenhacJsonLibrary.ObjToJson(dict(rows=resultList, balance=balance))
def exception_handler(error): """Catch any unhandled exception and reply with http 500""" app.logger.error(error) return DenhacJsonLibrary.ReplyWithError("ERROR: " + repr(error))
def internal_error(exception): """Catch any unhandled error and reply with http 500""" app.logger.error(exception) return DenhacJsonLibrary.ReplyWithError("Internal Server Error")
def importpaypaldata(): """DEPRECATING SOON. DO NOT USE.""" try: # If post, they're sending us the file. if request.method == 'POST': response = "" payment_type_ignore_list = [ 'Withdraw Funds to Bank Account', 'Invoice Sent', 'Request Received', 'Payment Sent', 'Temporary Hold', 'Debit Card Purchase', 'General Withdrawal' ] memberDb = DenhacMemberDb() numPayments = 0 numUnapplied = 0 totalDues = 0.0 totalFees = 0.0 filedata = request.form['filedata'] memreader = csv.DictReader(io.StringIO(filedata)) for row in memreader: (payment_type, from_email, to_email, name, gross, date, fee) = (str(row['Type']), str(row['From Email Address']), str(row['To Email Address']), str(row['Name']), str(row['Gross']), str(row['Date']), str(row['Fee'])) notes = "Paypal Payment: " + str(date) # Check Payment Type if payment_type in payment_type_ignore_list: response += 'IGNORING Payment of Type: ' + payment_type + '\n' continue # Sometimes Paypal has the From and To email addresses backwards; I don't know why. email = from_email if email == '*****@*****.**': email = to_email # Ok, by this point we should have a payment. Apply it. try: # If we're missing the email address (for ex. from a website payment), ignore it and process next row if not email: raise IndexError member = memberDb.getMemberByPaypalEmail(email) memberDb.createPayment(member['id'], gross, 3, notes) app.logger.error('Payment Applied: ' + member['lastName'] + ', Amount: ' + gross) response += 'Payment Applied: ' + member[ 'lastName'] + ', Amount: ' + gross + '\n' totalDues += float(gross) totalFees += float(fee) numPayments += 1 except IndexError: response += '================================================\n' response += 'Payment NOT APPLIED! Type: ' + payment_type + ', Name: ' + name + ', Amount: ' + gross + ', Date: ' + date + ', From Email: ' + from_email + '\n' response += 'Better do it manually or someone might be mad...\n' response += '================================================\n' app.logger.error('Payment NOT APPLIED! Type: ' + payment_type + ', Name: ' + name + ', Amount: ' + gross + ', Date: ' + date + ', From Email: ' + from_email) numUnapplied += 1 except: return DenhacJsonLibrary.ReplyWithError( "ERROR: Something failed for some reason. Check the server logs for more details. Error: " + sys.exc_info()[0] + sys.exc_info()[1] + sys.exc_info()[2]) response += 'Done!' response += '================================================\n' response += '# of Applied Payments: ' + str(numPayments) + '\n' response += '# of Unapplied Payments: ' + str( numUnapplied ) + ' <--- ****** Enter these transactions into the Member DB manually ******\n' response += 'Total Dues Collected: ' + str(totalDues) + '\n' response += 'Total Paypal Fees Paid: ' + str( totalFees ) + ' <--- ****** Enter this into WaveApps manually ******\n' response += '================================================\n' return DenhacJsonLibrary.ObjToJson( dict(response=response, numPayments=numPayments, numUnapplied=numUnapplied, totalDues=totalDues, totalFees=totalFees)) except: return DenhacJsonLibrary.ReplyWithError( "ERROR: Something failed for some reason. Check the server logs for more details. Error: " + sys.exc_info()[0] + sys.exc_info()[1] + sys.exc_info()[2])
def getpaymenttypes(): """Return a list of valid payment types from the DB.""" memberDb = DenhacMemberDb() resultList = memberDb.getPaymentTypes() return DenhacJsonLibrary.ObjToJson(dict(rows=resultList))
def logout(): """Logout the requested user.""" session.pop('logged_in', None) return DenhacJsonLibrary.ObjToJson(dict(logged_out="True"))
def hello(): """Simple hello world test service.""" return DenhacJsonLibrary.ObjToJson(dict(msg="Goodbye, cruel world."))