コード例 #1
0
def adhoc_charge():
    if not current_user.isSuperUser:
        abort(401)

    amount = request.json['amount']
    subject = request.json['subject']
    message = request.json['message']

    users = User.query.order_by(User.firstname.asc()).all()
    cost_per_player = float("{0:.2f}".format(float(amount) / float(len(users))))

    """Add expense entry"""
    expense_entry = Expense(amount, cost_per_player, "Deduction_AdHoc", len(users))
    db.session.add(expense_entry)
    db.session.commit()

    email_ids = []
    for user in users:
        email_ids.append(user.email)
        user.balance = float("{0:.2f}".format(user.balance - cost_per_player))

        transaction = Transaction(user.email, cost_per_player, user.balance, "Deduct_AdHoc")
        db.session.add(transaction)
        db.session.commit()

    SendGrid.send_email(email_ids, "*****@*****.**", subject, message)

    """Insert a log"""
    description = 'An AdHoc amount of $' + str(amount) + ' is charged for reason: ' + subject
    log = Log(current_user.email, description)
    db.session.add(log)
    db.session.commit()
    return "Success"
コード例 #2
0
def remove_sunday_absentee():
    if not current_user.isAdmin:
        abort(401)

    email = request.args.get('email')
    user = User.query.get(email)
    if not user:
        abort(404)

    user.isSundayAbsent = False
    user.sundayAbsentWeeks = 0
    user.futureSundayAbsentWeeks = 0
    db.session.commit()

    """Insert a log"""
    description = 'Deleted Sunday absentee - ' + user.firstname + ' ' + user.lastname
    log = Log(current_user.email, description)
    db.session.add(log)
    db.session.commit()

    super_user = User.query.filter_by(isSuperUser=True).first()

    """Send an email"""
    subject = 'Badminton: You are playing again on Sunday!!!'
    message = 'Hi,\r\n\nYou are removed from Sunday absents list by ' + current_user.firstname + ' ' + \
              current_user.lastname + '. From this point onwards you are assumed to be playing on Sundays ' + \
              'and you are charged for Sunday games. If you can\'t play in future please ' + \
              'register your absense using the badminton app.\r\n\n' + 'If you have any issues please contact ' + \
              super_user.firstname + ' ' + super_user.lastname + ' at: ' + super_user.email + \
              '\r\n\nThanks\r\nSydney Badminton Group'
    SendGrid.send_email(user.email, "*****@*****.**", subject, message)

    return "Success"
コード例 #3
0
def main():
    to = []
    to.append("malli.arjun@gmail")
    to.append("*****@*****.**")
    to.append("*****@*****.**")
    to.append("*****@*****.**")
    SendGrid.send_email(to, "*****@*****.**", "Subject", "Body")
コード例 #4
0
def run_shuttle_expense():
    if not current_user.isSuperUser:
        abort(401)

    cost = float(request.args.get('cost'))

    if cost <= 0:
        abort(400)

    active_users = User.query.filter(or_(User.isSaturdayAbsent == False, User.isSundayAbsent == False))\
        .order_by(User.firstname.asc())

    if not active_users or active_users.count() < 1:
        abort(404)

    shuttle_cost_per_player = float("{0:.2f}".format(
        float(cost) / float(active_users.count())))
    """Add expense entry"""
    expenseEntry = Expense(cost, shuttle_cost_per_player, "Deduction_Shuttles",
                           active_users.count())
    db.session.add(expenseEntry)
    db.session.commit()
    """Insert a log"""
    description = 'Shuttle expense for $' + str(cost) + ' is run'
    log = Log(current_user.email, description)
    db.session.add(log)
    db.session.commit()

    bodyText = 'Hi everyone,\r\n\nA total of $' + str(cost) + ' is charged for shuttles and it is charged ' + \
               'equally among ' + str(active_users.count()) + ' active players.' + \
               '\r\n\nBalances of players are as follows after deduction:\r\n\n'

    email_ids = []
    for user in active_users:
        user.balance = float("{0:.2f}".format(user.balance -
                                              shuttle_cost_per_player))
        db.session.commit()

        transaction = Transaction(user.email, shuttle_cost_per_player,
                                  user.balance, "Deduct_Shuttle")
        db.session.add(transaction)
        db.session.commit()

        email_ids.append(user.email)
        bodyText += user.firstname + ' ' + user.lastname + ' : ' + '$' + str(
            user.balance) + '\r\n'

    bodyText += '\nThanks\r\nSydney Badminton Group'
    SendGrid.send_email(email_ids, "*****@*****.**",
                        "Badminton: Balances after Shuttles expenses",
                        bodyText)

    return "Success"
コード例 #5
0
def main():
    with app.app_context():
        """Get all users whose balance is less than or equal $10"""
        users = User.query.filter(User.balance <= 10)

        for user in users:
            subject = 'Badminton: Your balance is low please top up'
            message = 'Hi ' + user.firstname + \
                      ',\r\n\nYour balance is currently running low and it is ' + str(user.balance) + \
                      '$. Please top up your account.\r\n\nThanks\r\nSydney Badminton Group'
            SendGrid.send_email(user.email, "*****@*****.**", subject,
                                message)
コード例 #6
0
def send_payment_notification_to_super_user():
    amount = request.args.get('amount')

    if amount < 0:
        abort(400)

    super_user = User.query.filter_by(isSuperUser=True).first()

    if not super_user:
        abort(404)

    SendGrid.send_email(super_user.email, "*****@*****.**",
                        current_user.firstname + " " + current_user.lastname + " has made $" + amount + " payment",
                        current_user.firstname + " " + current_user.lastname + " has made $" + amount + " payment")
    return "Success"
コード例 #7
0
def topup_group():
    if not current_user.isSuperUser:
        abort(401)

    email = request.json['email']
    amount = float(request.json['amount'])

    if amount <= 0:
        abort(400)

    group_owner = User.query.get(email)

    if not group_owner:
        abort(404)

    users = User.query.filter_by(group=group_owner.group)
    if not users or users.count() < 1:
        abort(404)
    """Insert a log"""
    description = 'Topup for $' + str(
        amount) + ' is run for user group - ' + str(group_owner.group)
    log = Log(current_user.email, description)
    db.session.add(log)
    db.session.commit()

    topupAmount = float("{0:.2f}".format(amount / float(users.count())))
    bodyText = 'Hi everyone,\r\n\nYour group is topped up with $' + str(topupAmount) + '.' +\
               '\r\n\nAfter this top up, balances of your group members are as follows :\r\n\n'
    email_ids = []
    email_ids.append(current_user.email)
    for user in users:
        user.balance = float("{0:.2f}".format(user.balance + topupAmount))
        db.session.commit()

        transaction = Transaction(user.email, topupAmount, user.balance,
                                  "Top_Up")
        db.session.add(transaction)
        db.session.commit()

        email_ids.append(user.email)
        bodyText += user.firstname + ' ' + user.lastname + ' : ' + '$' + str(
            user.balance) + '\r\n'

    bodyText += '\nThanks\r\nSydney Badminton Group'
    SendGrid.send_email(email_ids, "*****@*****.**",
                        "Badminton: Balances after top up", bodyText)

    return "Success"
コード例 #8
0
def main():
    with app.app_context():
        users = User.query.order_by(User.firstname.asc()).all()

        for user in users:
            forgot_password_token = uuid.uuid1()
            user.forgotPasswordToken = str(forgot_password_token)
            db.session.commit()

            subject = "Badminton: set password to access your account"
            message = 'Hi ' + user.firstname + ",\r\n\n" + \
                      'In order to access your account on new Badminton website you need to click the below link' + \
                      ' to set your password.\r\n\n' + \
                      'http://' + app.config['HOST_NAME'] + "/resetPassword?token=" + user.forgotPasswordToken + \
                      '\r\n\nPlease bookmark following URL for easy access to the website in future.\r\n\n' + \
                      'http://' + app.config['HOST_NAME'] + \
                      '\r\n\nThanks\r\n Maddy'
            SendGrid.send_email(user.email, "*****@*****.**", subject, message)
            print 'Sent an email to: ' + user.firstname
コード例 #9
0
def forgot_password():
    email = request.args.get('email')

    user = User.query.get(email)

    if not user:
        abort(404)

    forgot_password_token = uuid.uuid1()
    user.forgotPasswordToken = str(forgot_password_token)
    db.session.commit()

    subject = "Badminton: password reset request"
    message = "You're receiving this email because you requested a password reset for the user " + user.firstname + \
              " " + user.lastname + ".\r\n\nPlease click the following link to reset the password,\r\n\n" + \
              str(request.url_root) + "resetPassword?token=" + user.forgotPasswordToken + \
              '\r\n\nThanks\r\nSydney Badminton Group'
    SendGrid.send_email(user.email, "*****@*****.**", subject, message)
    return "Success"
コード例 #10
0
def send_an_email():
    if not current_user.isSuperUser:
        abort(401)

    subject = request.json['subject']
    message = request.json['message']

    users = User.query.order_by(User.firstname.asc()).all()

    email_ids = []
    for user in users:
        email_ids.append(user.email)

    SendGrid.send_email(email_ids, "*****@*****.**", subject, message)
    """Insert a log"""
    description = 'An email is sent to everyone with subject: ' + subject
    log = Log(current_user.email, description)
    db.session.add(log)
    db.session.commit()
    return "Success"
コード例 #11
0
def create_a_new_user():
    if not current_user.isSuperUser:
        abort(401)

    firstname = request.args.get('firstname')
    lastname = request.args.get('lastname')
    email = request.args.get('email')
    balance = request.args.get('balance')
    saturday_absent_weeks = request.args.get('saturdayAbsentWeeks')
    sunday_absent_weeks = request.args.get('sundayAbsentWeeks')
    is_admin = bool(json.loads(request.args.get('isAdmin')))

    user = User.query.get(email)
    if user:
        abort(409)

    users = User.query.order_by(User.group.desc()).all()
    group = users[0].group + 1

    user = User(firstname, lastname, email, email+str(balance), balance, group, True, is_admin,
                saturday_absent_weeks, 0, sunday_absent_weeks, 0)
    user.forgotPasswordToken = str(uuid.uuid1())
    db.session.add(user)
    db.session.commit()

    email_ids = [current_user.email, email]
    subject = 'Badminton: Your account is provisioned'
    message = 'Hi ' + firstname + ',\r\n\nGood news! Your account has been provisioned. Please click the below ' + \
              'link to set your password.\r\n\n' + str(request.url_root) + "resetPassword?token=" + \
              user.forgotPasswordToken + '\r\n\nAfter setting the password please click the below link to ' + \
              'access your account. Please bookmark this link.\r\n\n' + str(request.url_root) + \
              '\r\n\nThanks\r\nSydney Badminton Group'
    SendGrid.send_email(email_ids, "*****@*****.**", subject, message)

    """Insert a log"""
    description = 'A new user account is created for ' + user.firstname + ' ' + user.lastname
    log = Log(current_user.email, description)
    db.session.add(log)
    db.session.commit()
    return "Success"
コード例 #12
0
def delete_a_user():
    if not current_user.isSuperUser:
        abort(401)

    email = request.args.get('email')
    user = User.query.get(email)
    if not user:
        abort(404)

    db.session.delete(user)
    db.session.commit()

    email_ids = [current_user.email, email]
    subject = 'Badminton: Your account has been deleted'
    message = 'Hi ' + user.firstname + ',\r\n\nAs requested, your account has been deleted from badminton group.' + \
              '\r\n\nThanks\r\nSydney Badminton Group'
    SendGrid.send_email(email_ids, "*****@*****.**", subject, message)
    """Insert a log"""
    description = 'User account for ' + user.firstname + ' ' + user.lastname + ' has been deleted.'
    log = Log(current_user.email, description)
    db.session.add(log)
    db.session.commit()
    return "Success"
コード例 #13
0
def run_expense(day):
    if day != "Saturday" and day != "Sunday":
        return

    courts_cost = CourtsCost.query.filter_by(day=day).first()

    if day == "Saturday":
        players = User.query.filter_by(isSaturdayAbsent=False).order_by(
            User.firstname.asc())
        absent_players = User.query.filter_by(isSaturdayAbsent=True).order_by(
            User.firstname.asc())
    elif day == "Sunday":
        players = User.query.filter_by(isSundayAbsent=False).order_by(
            User.firstname.asc())
        absent_players = User.query.filter_by(isSundayAbsent=True).order_by(
            User.firstname.asc())
    db.session.close()

    email_ids = []
    body_text = ""
    if players.count() > 0:
        cost_per_player = float("{0:.2f}".format(
            float(courts_cost.cost) / float(players.count())))
        """Add expense entry"""
        expence_description = ("Deduction_Sunday",
                               "Deduction_Saturday")[day == "Saturday"]
        expenseEntry = Expense(courts_cost.cost, cost_per_player,
                               expence_description, players.count())
        db.session.add(expenseEntry)
        db.session.commit()

        body_text += 'Hi everyone,\r\n\nA total of ' + str(players.count()) + ' players attended the play on ' \
                    + day + ' and courts booking cost = $' + str(courts_cost.cost) + '. So cost per player = $' + \
                    str(cost_per_player) + ".\r\n\nBalances of played players are as follows:\r\n\n"

        for player in players:
            player.balance = float("{0:.2f}".format(player.balance -
                                                    cost_per_player))

            transaction = Transaction(player.email, cost_per_player,
                                      player.balance,
                                      ("Deduct_Sun",
                                       "Deduct_Sat")[day == "Saturday"])
            db.session.add(transaction)
            db.session.commit()

            email_ids.append(player.email)
            body_text += player.firstname + ' ' + player.lastname + ' : ' + '$' + str(
                player.balance) + '\r\n'

    body_text += '\nBalances of absent players are as follows\r\n\n'

    for absent_player in absent_players:
        if day == "Saturday":
            absent_player.saturdayAbsentWeeks -= 1
            if absent_player.saturdayAbsentWeeks <= 0:
                absent_player.saturdayAbsentWeeks = 0
                absent_player.isSaturdayAbsent = False
        elif day == "Sunday":
            absent_player.sundayAbsentWeeks -= 1
            if absent_player.sundayAbsentWeeks <= 0:
                absent_player.sundayAbsentWeeks = 0
                absent_player.isSundayAbsent = False
        db.session.commit()

        email_ids.append(absent_player.email)
        body_text += absent_player.firstname + ' ' + absent_player.lastname + ' : ' + '$' + str(
            absent_player.balance) + '\r\n'
    """Check if user is absent in future, if so set the absent weeks, absent flag
        and set future absent weeks to zero"""
    for player in players:
        future_absent_weeks = (
            player.futureSundayAbsentWeeks,
            player.futureSaturdayAbsentWeeks)[day == "Saturday"]
        if future_absent_weeks > 0:
            if day == "Saturday":
                player.futureSaturdayAbsentWeeks = 0
                player.isSaturdayAbsent = True
                player.saturdayAbsentWeeks = future_absent_weeks
            elif day == "Sunday":
                player.futureSundayAbsentWeeks = 0
                player.isSundayAbsent = True
                player.sundayAbsentWeeks = future_absent_weeks
        db.session.commit()

    body_text += '\nThanks\r\nSydney Badminton Group'
    SendGrid.send_email(email_ids, "*****@*****.**",
                        "Badminton: Balances after " + day + "'s play",
                        body_text)

    return