Ejemplo n.º 1
0
def add():
    if 'username' not in session:
        return redirect(url_for('login'))
    error = ''
    if request.method == 'POST':
        try:
            donation = float(request.form['value'])
        except ValueError:
            pass
        else:
            if request.form['donor'] != '' and donation > 0:
                if not Donor.select().where(Donor.name == request.form['donor']):
                    Donor(
                        name=request.form['donor']
                    ).save()
                Donation(
                    donor=request.form['donor'],
                    value=donation
                ).save()
                return redirect(url_for('all'))
        error = 'A donor name and a positive, non-zero donation amount are required.'
    return render_template(
        'new_donation.jinja2',
        error=error,
        request=request,
        username=session['username'] if 'username' in session.keys() else ''
    )
Ejemplo n.º 2
0
def create():

    if 'username' not in session:
        return redirect(url_for('login'))

    if request.method == 'POST':

        try:
            n = request.form['name']
            v = request.form['donation']
            n = Donor(name=n)
            n.save()
            Donation(donor=n, value=v).save()

            return redirect(url_for('all_donations'))

        except:
            n = request.form['name']
            #print("exception", n, v)
            return render_template('lookup.jinja2',
                                   donor=n,
                                   donations=Donation.select())

    else:
        return render_template('create.jinja2')
Ejemplo n.º 3
0
def create_donation():
    # redirect the user to login page if not logged in.
    if 'username' not in session:
        return redirect(url_for('login'))

    if request.method == "POST":
        try:
            the_donor = Donor.select()\
                             .where(Donor.name == request.form['name'])\
                             .get()
        except Donor.DoesNotExist:
            # solution1: if donor doesn't exist, then re-display the donation
            # creation form and inject a message describing the error.
            #return render_template('create.jinja2',
            #                       error='Donor does not exist.')

            # solution2: if donor doesn't exist, then create a new donor with
            # the given name, along with the indicated donation.
            the_donor = Donor(name=request.form['name'],
                              password=pbkdf2_sha256.hash(request.form['name']))
            the_donor.save()
        Donation(value=request.form['amount'], donor=the_donor).save()
        return redirect(url_for('home'))

    return render_template('create.jinja2')
Ejemplo n.º 4
0
def delete_donor():
    if 'username' not in session:
        return redirect(url_for('login'))

    if request.method == 'POST':

        name = request.form['name']
        print(f"+++ {name} to delete")
        if name is None:
            return render_template('delete.jinja2')
        else:
            try:
                donor = Donor.select().where(Donor.name == name).get()
                # query = Note.delete().where(Note.id > 3)
                # n = query.execute()

                query1 = Donation.delete().where(Donation.donor_id == donor.id)
                query2 = Donor.delete().where(Donor.name == donor.name)
                m = query1.execute()
                n = query2.execute()
            except Donor.DoesNotExist:  # no donor in db
                return render_template('delete.jinja2',
                                       error="Donor not found.",
                                       not_found=name)
            except psycopg2.errors.InFailedSqlTransaction:
                return render_template(
                    'delete.jinja2',
                    error="Heroku - psycopg2.errors.InFailedSqlTransaction")
            return redirect(url_for('all_donors'))
    else:
        return render_template('delete.jinja2')
Ejemplo n.º 5
0
def donate():
    """ let a user create a new donor / donation """
    if request.method == 'POST':
        donor_name = request.form['name']
        amount = request.form['value']
        if donor_name and amount:
            donor_count = Donor.select() \
                            .where(Donor.name == request.form['name']) \
                            .count()

            if donor_count == 0:
                donor = Donor.create(name=request.form['name'])
            else:
                donor = Donor.get(name=request.form['name'])

            if int(amount) <= 1000000 and int(amount) > 0:
                Donation.create(donor=donor, value=request.form['value'])
                _donations = Donation.select()
                return render_template('donations.jinja2',
                                       donations=_donations)

            flash('Input amount within an acceptable range (0 to 1000000)')
            return render_template('add_donation.jinja2')
    elif request.method == 'GET':
        if session.get('username'):
            return render_template('add_donation.jinja2')

    return render_template('login.jinja2')
Ejemplo n.º 6
0
def create():
    # get requests land us at /create
    if request.method == 'GET':
        return render_template('create.jinja2')
    # post request should query db for the donor, add donation, redir to /all

    if request.method == 'POST':
        # want to make sure there is an acceptable value entered:
        try:
            value = int(request.form['amount'])

        except ValueError:
            render_template('create.jinja2')
        if value <= 0:
            render_template('create.jinja2')

        else:
            donor_name = request.form['name']
            if donor_name:
                try:
                    donor = Donor.select().where(
                        Donor.name == donor_name).get()

                except Donor.DoesNotExist as e:
                    donor = Donor(name=donor_name)
                    donor.save()

                donation = Donation(value=value, donor=donor)
                donation.save()
                return redirect(url_for('all'))
            else:
                return render_template('create.jinja2')

    return render_template('create.jinja2', create=create)
Ejemplo n.º 7
0
def add():
    # there are too many return statements up in here and I need to
    # clean that ish up.
    # By default we return the main page with all donations.
    output = redirect(url_for('all'))
    if request.method == 'POST':
        # Let's make sure the donation amount is a positive integer...
        less_than_0_error = "Please enter a donation amount greater than 0."
        try:
            amount = int(request.form['amount'])
        except ValueError:
            output = render_template('add.jinja2', error=less_than_0_error)
        if amount <= 0:
            output = render_template('add.jinja2', error=less_than_0_error)
        else:
            name = request.form['name']
            if name:
                try:
                    donor = Donor.select().where(Donor.name == name).get()
                except Donor.DoesNotExist:
                    donor = Donor(name)
                    donor.save()
                donation = Donation(donor=donor, value=amount)
                donation.save()
                output = redirect(url_for('all'))
            else:
                output = render_template('add.jinja2',
                                         error='Please enter a donor name.')
    else:
        output = render_template('add.jinja2')

    return output
Ejemplo n.º 8
0
def new_donor():
    # print(session)
    if 'username' not in session:
        return redirect(url_for('login'))

    if request.method == 'POST':

        name = request.form['name']
        if name:
            try:
                donor = Donor(name=name)
                donor.save()
            except peewee.IntegrityError:
                return render_template(
                    'new.jinja2', error=f"Donor {name} already in Database")
            except psycopg2.errors.UniqueViolation:
                return render_template(
                    'new.jinja2', error=f"Donor {name} already in Database")
            else:
                return redirect(url_for('all_donors'))
        else:
            return render_template('new.jinja2',
                                   error="Please enter donor name")
    else:
        return render_template('new.jinja2')
Ejemplo n.º 9
0
def donor():
    donors = Donor.select()
    if request.method == 'GET':
        return render_template('donor.jinja2')
    donor_name = request.form['donor']
    Donor(name=donor_name).save()
    return redirect(url_for('home'))
Ejemplo n.º 10
0
def create():
    if "username" not in session:
        return redirect(url_for("login"))

    if request.method == "POST":
        name = request.form["name"]

        if name is None or name.isspace() or name == "":
            msg = "Donor cannot be empty or whitespace."
            return render_template("create.jinja2", error=msg)

        try:
            donor = Donor.select().where(Donor.name == name).get()
        except Donor.DoesNotExist:
            donor = Donor(name=request.form["name"])
            donor.save()

        try:
            donation = int(request.form["donation"])

            if donation <= 0:
                raise ValueError()

            donation = Donation(donor=donor, value=donation)
            donation.save()
        except ValueError:
            msg = "Donation must be a non-negative number greater than 0"
            return render_template("create.jinja2", error=msg)

        if request.form["save"] == "Save Donation":
            return redirect(url_for("all"))

    return render_template("create.jinja2")
Ejemplo n.º 11
0
def main():
    """Main function for populating the database"""
    donors = [('Toni Morrison', random.sample(range(100, 25000), 3)),
              ('Mike McHargue', random.sample(range(100, 25000), 3)),
              ("Flannery O'Connor", random.sample(range(100, 25000), 3)),
              ('Angelina Davis', random.sample(range(100, 25000), 3)),
              ('Bell Hooks', random.sample(range(100, 25000), 3))]

    db.connect()
    db.drop_tables([Donor, Donation])
    db.create_tables([Donor, Donation])
    bcrypt = Bcrypt()

    for donor, donations in donors:
        try:
            Donor.create(name=donor,
                         email='.'.join(donor.lower().split()) + '@gmail.com',
                         password=bcrypt.generate_password_hash('password'),
                         total=sum(donations),
                         average=sum(donations) / len(donations))
        except:
            db.rollback()

    for donor, donations in donors:
        for donation in donations:
            try:
                Donation.create(donor=donor, amount=donation)
            except:
                db.rollback()
Ejemplo n.º 12
0
def create():
    """If the handler receives a GET request,
    then it should render the template for the donation creation page.

    If the handler receives a POST request (a form submission),
    then it should attempt to retrieve the name of the donor and the
    amount of the donation from the form submission.
    It should retrieve the donor from the database with the indicated name,
    and create a new donation with the indicated donor and donation amount.
    Then it should redirect the visitor to the home page.
    """

    if request.method == 'POST':
        donor_name = request.form['name']

        try:
            # add the donor to the db if the record doesn't exist
            donor = Donor.create(name=donor_name)
        except IntegrityError:
            # if donor already exists, retrieve donor
            donor = Donor.get(Donor.name == donor_name)

        # add donation to db
        Donation.create(donor=donor, value=request.form['value'])

        return redirect(url_for('all_donations'))

    else:
        return render_template('create.jinja2')
Ejemplo n.º 13
0
def create():
    if request.method == 'POST':
        donor = Donor(name=request.form['name'])
        donor.save()
        Donation(donor=donor, value=request.form['amount']).save()
        return redirect(url_for('home'))
    else:
        return render_template('create.jinja2')
Ejemplo n.º 14
0
def add_donation():
    if request.method == 'POST':
        new_donor = Donor(name=request.form['donor'])
        new_donor.save()
        donation = Donation(donor=new_donor, value=request.form['value'])
        donation.save()
        return redirect(url_for('list_all'))
    return render_template('add.jinja2')
Ejemplo n.º 15
0
def donate():
    if request.method == "POST":
        amount = float(request.form['amount'])
        donor = Donor(name=request.form['donor'].title())
        donor.save()
        Donation(value=amount, donor=donor).save()
        return redirect(url_for('all'))
    else:
        return render_template('donate.jinja2')
Ejemplo n.º 16
0
def create():
    if request.method == "POST":
        try:
            donor = Donor(name=request.form["name"])
            donor.save()
            return redirect(url_for("create_donation", donor_name=donor.name))
        except peewee.IntegrityError:
            return redirect(url_for("create_donation", donor_name=donor.name))
    else:
        return render_template('create.jinja2')
Ejemplo n.º 17
0
def create():
    if request.method == 'GET':
        return render_template('create.jinja2')
    elif request.method == 'POST':
        donor_name = Donor.get_or_none(Donor.name == request.form['name'])
        if donor_name is None:
            donor_name = Donor(name=request.form['name'])
            donor_name.save()
        Donation(donor=donor_name, value=request.form['amount']).save()
        return redirect(url_for('all'))
Ejemplo n.º 18
0
def select_single():
    if request.method == 'POST':
        donor = Donor.select().where(Donor.name == request.form['name']).get()
        donations = Donation.select().join(Donor).where(
            Donor.name == donor.name)
        return render_template('view_single.jinja2',
                               donor=donor,
                               donations=donations)
    else:
        return render_template('select_single.jinja2', donors=Donor.select())
Ejemplo n.º 19
0
def add_donation():
    if 'username' not in session:
        return redirect(url_for('login'))

    if request.method == 'POST':
        donor = Donor(name=request.form['name'])
        donor.save()
        Donation(donor=donor, value=int(request.form['donation'])).save()
        return redirect(url_for('all'))
    else:
        return render_template('add_donation.jinja2')
Ejemplo n.º 20
0
def create():
    """Create Donation (http://localhost:6738/create/)"""
    if request.method == 'POST':
        donor = Donor(name=request.form['name'])
        donor.save()

        donation = Donation(donor=donor, value=int(request.form['donation']))
        donation.save()

        return redirect(url_for('all'))
    else:
        return render_template('create.html')
Ejemplo n.º 21
0
def create():
    if request.method == 'POST':
        try:
            donor = Donor.get(Donor.name == request.form['name'])
        except Donor.DoesNotExist:
            donor = Donor(name=request.form['name'])
            donor.save()
        donation = Donation(donor_id=donor.id, value=request.form['donation'])
        donation.save()
        return redirect(url_for('all'))
    else:
        return render_template('create.jinja2')
Ejemplo n.º 22
0
def donate():
    # make a donation and redirect to donor page
    if request.method == 'POST':
        donor = Donor(name=request.form['name'])
        donor.save()

        amount = Donation(donor=donor, value=request.form['amount'])
        amount.save()

        return redirect(url_for('all'))
    else:
        return render_template('donate.jinja2', donations=Donation.select())
Ejemplo n.º 23
0
def donate():
    if request.method == 'POST':
        donor = Donor.select().where(Donor.name == request.form['name'])

        if not donor:
            donor = Donor(name=request.form['name'])
            donor.save()

        Donation(donor=donor, value=request.form['amount']).save()
        return redirect(url_for('home'))

    return render_template('donate.jinja2')
Ejemplo n.º 24
0
def add():
    registered_donors = Donor.select()
    if request.method == 'POST':
        try:
            name_check = Donor.get(Donor.name == (request.form['name'])).id
            Donation(donor=name_check, value=request.form['number']).save()
            return redirect(url_for('donations'))
        except peewee.DoesNotExist:
            return render_template('add.jinja2',
                                   error="Donor name is not registered.")
    else:
        return render_template('add.jinja2')
Ejemplo n.º 25
0
def add_donor():
    if request.method == 'POST':
        donor = Donor(name=request.form['name'], email=request.form['email'])

        for _donor in Donor().select():
            if str(_donor.name).lower() == str(donor.name).lower():
                return render_template('add_donation.jinja2')

        donor.save()
        return render_template('add_donation.jinja2')
    else:
        return render_template('add_donor.jinja2')
Ejemplo n.º 26
0
def new_donor():
    """
    Add a new donor to the Donor table
    :return: The create donor or create donation page
    """
    if request.method == 'POST':
        donor = Donor(name=request.form['donor_name'])
        donor.save()
        message = "The donor {} was created. Do you want to add a donation amount".format(donor.name)
        # Pass the variable message to the create function
        return redirect(url_for('create', message = message))

    return render_template('donor.jinja2')
Ejemplo n.º 27
0
def not_found(name, amount):
    if request.method == 'POST':
        donor = Donor(name=name)
        donor.save()

        Donation(donor=donor, value=amount).save()

        return redirect(url_for('all'))

    else:
        return render_template('donor_not_found.jinja2',
                        name=name,
                        amount=amount)
Ejemplo n.º 28
0
def create():
    if request.method == 'POST':
        donorName = request.form['donor']
        donorDonation = request.form['donation']
        try:
            selectedDonor = Donor.create(name=donorName)
        except peewee.IntegrityError:
            selectedDonor = Donor.get(Donor.name == donorName)
        Donation(donor=selectedDonor, value=donorDonation).save()
        donations = Donation.select()
        return render_template('donations.jinja2', donations=donations)
    else:
        return render_template('create.jinja2')
Ejemplo n.º 29
0
def add_donation():
    if request.method == 'GET':
        # User clicked the "add donation" link to get here
        donors = Donor.select()
        return render_template('add_donation.jinja2', donors=donors)
    elif request.method == 'POST':
        # User pressed the 'add' button
        donor_name = Donor.select().where(
            Donor.name == request.form['name']).get()
        donation_amount = to_float(request.form['donation'])
        if donation_amount > 0.0:
            Donation(donor=donor_name, value=donation_amount).save()
        return redirect(url_for('all'))
Ejemplo n.º 30
0
def all_donors():
    if request.method == 'POST':
        donor = Donor.select().where(
            Donor.name == request.form['donor_name']).get()
        donations = [
            donation for donation in Donation.select()
            if donation.donor_id == donor.id
        ]
        return render_template('donor_info.jinja2',
                               donor=donor,
                               donations=donations)
    else:
        donors = Donor.select()
        return render_template('all_donors.jinja2', donors=donors)