def get_donation(): if not session.get('current_building_id'): redirect(url_for('donation_address')) form = DonationForm() conn_error = request.args.get('conn_error') validate_campaign_status(current_user.team.campaign) if form.validate_on_submit(): session['current_donation'] = { "amount": form.amount.data, "payment_type": form.payment_type.data, "team_id": current_user.team_id } if form.payment_type.data == 'bit': return redirect(url_for('bit_donation')) elif form.payment_type.data == 'PayPal': # Create a paypal payment and redirect to the authorization process via paypal's api try: payment = pp.create_payment( form.amount.data, f'{HOST_URL}donation_address/donation/paypal/execute_paypal_donation', f'{HOST_URL}donation_address/donation') return redirect(pp.authorize_payment(payment)) except (ConnectionError, RuntimeError): conn_error = True # if there's a connection error / unexpected error, display an error in the donation page generate_automate_report('paypal') if not conn_error: return redirect(url_for('send_invoice')) return render_template('/donation.html', form=form, conn_error=conn_error)
def home(request): context = {} if request.POST: form = DonationForm(request.POST) if form.is_valid(): donation = form.save(commit=False) if 'stripeToken' in request.POST: charged = _charge_card(request.POST) if charged: donation.cleared = donation.received = True donation.save() for sec in form.cleaned_data['sections']: donation.sections.add(sec) context['thanks'] = form.cleaned_data['first_name'] context['postcard'] = form.cleaned_data['postcard'] else: context['show_form'] = True form = DonationForm sections = Section.objects.select_related() section_sponsors = {} for section in sections: section_sponsors[section.slug] = section.donations.all() context['sections'] = sections context['section_sponsors'] = section_sponsors context['form'] = form context['stripe_publishable_key'] = settings.STRIPE_PUBLISHABLE_KEY return render_to_response('index.html', context, context_instance=RequestContext(request))
def view_registry(cat, slug): category = REGISTRY_TYPES.get(cat, None) if not category: flash('The registry category does not exist', 'error') return redirect(url_for('.index')) registry = category.get_by_slug(slug) if not registry: flash('The registry does not exist', 'error') return redirect(url_for('.index')) if cat == 'weddings': form = DonationForm(request.form) if form.validate_on_submit(): tran = Transaction() form.populate_obj(tran) tran.total_amount = form.amount.data tran.payment_status = 'unpaid' tran.type = 'donation' tran.save() tran.generate_txn_number() tran.save() donation = Donation() donation.registry_id = registry.id donation.transaction_id = tran.id donation.amount = form.amount.data donation.save() # initialize payments paystack = PaystackPay() response = paystack.fetch_authorization_url( email=tran.email, amount=tran.total_amount) if response.status_code == 200: json_response = response.json() tran.update( payment_txn_number=json_response['data']['reference']) return redirect(json_response['data']['authorization_url']) else: flash('Something went wrong. Please try again', 'error') return render_template('frontend/registry.html', registry=registry, form=form, cat=cat) return render_template('frontend/registry.html', registry=registry, cat=cat)
def new(request, year, month, day): """ Page for creating pickups """ context = {} if request.method == "POST": Print(request.POST) form = DonationForm(request.POST) object_check = Donation.objects.filter( truck=request.POST["truck"], time=datetime.datetime.strptime(request.POST["time"], "%I:%M %p"), date=datetime.datetime.strptime(request.POST["date"], "%m/%d/%Y"), ) if object_check: messages.add_message(request, messages.INFO, "Pickup not scheduled: time unavailable") else: if form.is_valid(): a = form.save() if request.POST.get("repetition"): if makeRecurringDonation(a, request): return redirect("/schedule") else: return redirect("/schedule") target_donor = Donor.objects.get(pk=request.POST["donor"]) else: user_settings = UserSettings.objects.get(pk=request.user) target_truck = user_settings.truck target_donor = user_settings.donor form = DonationForm( { "truck": target_truck.id, "donor": target_donor.id, "address1": target_donor.address1, "address2": target_donor.address2, "city": target_donor.city, "state": target_donor.state, "zip_code": target_donor.zip_code, "driver": target_truck.driver.id, "nearest_facility": target_donor.nearest_facility, "date": datetime.date(int(year), months.index(month) + 1, int(day)), } ) context.update(csrf(request)) context.update( { "form": fieldsetForm(form, request), "button_value": "Create", "page_title": "New Pickup", "target_donor": target_donor, } ) return render_to_response("truckmanagement/donation.html", context, context_instance=RequestContext(request))
def record_donation(request): main_form = DonationForm() MoneyFormSet = formset_factory(SimpleMoneyForm)(prefix="Money") transaction_type = 'donation' if request.POST: #They are issuing a POST. Let's handle it and create the record of this new donation. #(or send them back to the form to correct errors if there are any) exchange = donation_from_POST(request.POST, request.user.member) return HttpResponse('Donation recorded.') # if not invalid: # pass # # else: # form = invalid # return render(request, 'commerce/record_transaction.html', locals()) else: #They are brand new to this page. Give them the form they are looking for. #In some cases, we'll need to enter a completely new user. Let's prepare forms for that. contact_forms = UserContactForm(prefix="user"), UserContactInfoForm( prefix="contact"), UserProfileForm(prefix="profile") #We want email, first name, and last name to be required for all submissions. contact_forms[0].fields['email'].required = True contact_forms[0].fields['first_name'].required = True contact_forms[0].fields['last_name'].required = True #No phone needed. contact_forms[1].fields['phone'].required = False MoneyFormSet = formset_factory(SimpleMoneyForm)(prefix="Money") RealThingFormSet = formset_factory(DonationRealThingForm)( prefix="Real Things") item_forms = [MoneyFormSet, RealThingFormSet] return render(request, 'commerce/record_transaction.html', locals())
def donation(request, donation_id): Print(request.POST) context = {} context.update(csrf(request)) target_donation = Donation.objects.get(pk=donation_id) Print(target_donation.time) if request.POST: request.POST = request.POST.copy() if request.POST.get("time", "") == "00:00 AM": request.POST["time"] = "12:00 AM" call_redirect = False if "refresh" in request.POST: messages.add_message( request, messages.INFO, "Donation Donor Info Updated on all related, non-completed donations" ) updateDonor(target_donation) return redirect("/schedule") elif "delete" in request.POST: if request.POST.get("delete_series"): deleteDonationSeries(target_donation, request) else: object_check = Donation.objects.filter(parent_donation=target_donation) if object_check: deleteDonationSeries(target_donation, request) else: if target_donation.status != "picked_up": target_donation.delete() messages.add_message(request, messages.INFO, "Pickup Deleted") else: messages.add_message(request, messages.INFO, "Pickup Completed, Delete Failed") return redirect("/schedule") else: update_donor = False if target_donation.donor != Donor.objects.get(pk=request.POST["donor"]): update_donor = True requested_date = datetime.datetime.strptime(request.POST["date"], "%m/%d/%Y").date() requested_time = datetime.datetime.strptime(request.POST["time"], "%H:%M %p").time() object_check = Donation.objects.filter( truck=request.POST["truck"], time=requested_time, date=requested_date ).exclude(id=target_donation.id) if object_check: messages.add_message(request, messages.INFO, "Pickup not scheduled: time unavailable") elif target_donation.repetition and request.POST.get("update_series", ""): if target_donation.date != requested_date or target_donation.time != requested_time: deleteDonationSeries(target_donation, request) form = DonationForm(request.POST) target_donation = form.save() makeRecurringDonation(target_donation, request) else: target_parent_donation = target_donation if target_donation.parent_donation: target_parent_donation = target_donation.parent_donation for i in Donation.objects.filter(parent_donation=target_parent_donation): if i.status != "picked_up": i.donor = Donor.objects.get(pk=request.POST["donor"]) i.address1 = request.POST["address1"] i.address2 = request.POST["address2"] i.city = request.POST["city"] i.state = request.POST["state"] i.zip_code = request.POST["zip_code"] i.nearest_facility = request.POST["nearest_facility"] i.truck = Truck.objects.get(pk=request.POST["truck"]) i.driver = Driver.objects.get(pk=request.POST["driver"]) i.repetition = request.POST["repetition"] i.save() if target_parent_donation.status != "picked_up": target_parent_donation.donor = Donor.objects.get(pk=request.POST["donor"]) target_parent_donation.address1 = request.POST["address1"] target_parent_donation.address2 = request.POST["address2"] target_parent_donation.city = request.POST["city"] target_parent_donation.state = request.POST["state"] target_parent_donation.zip_code = request.POST["zip_code"] target_parent_donation.nearest_facility = request.POST["nearest_facility"] target_parent_donation.truck = Truck.objects.get(pk=request.POST["truck"]) target_parent_donation.driver = Driver.objects.get(pk=request.POST["driver"]) target_parent_donation.repetition = request.POST["repetition"] target_parent_donation.save() target_donation = target_parent_donation if update_donor: updateDonor(target_donation) return redirect("/schedule") elif target_donation.status != "picked_up" or request.user.has_perm( "truckmanagement.changefinished_donation" ): form = DonationForm(request.POST or None, instance=target_donation) form.save() messages.add_message(request, messages.INFO, "Pickup changed") if update_donor: updateDonor(target_donation) return redirect("/schedule") else: messages.add_message(request, messages.INFO, "Pickup complete, modification not possible") return redirect("/schedule") form = fieldsetForm(DonationForm(instance=target_donation), request) context.update( { "form": form, "button_value": "Update", "page_title": "Modify Pickup", "forward_donor": target_donation.donor.id, } ) return render_to_response("truckmanagement/donation.html", context, context_instance=RequestContext(request))
def view_campaign(id): connection = get_connection() cursor = get_cursor() form = DonationForm() campaign = None amount_donated = None percentage = None donations = None def setup(): nonlocal campaign, amount_donated, percentage, donations try: cursor.execute( """ SELECT c.id AS campaign_id, c.name, c.description, c.image, c.amount_requested, c.date_created, c.last_modified, up.id AS owner_id, up.first_name, up.last_name, up.profile_image, up.description AS owner_description, get_total_donations(c.id) AS amount_donated, ceil((get_total_donations(c.id)/c.amount_requested)*100) AS percentage FROM campaign c INNER JOIN campaign_relation cr on c.id = cr.campaign_id INNER JOIN user_account ua on cr.user_account_id = ua.id INNER JOIN user_profile up on ua.id = up.user_account_id WHERE c.id = %s AND cr.user_role='owner'; """, (id, )) campaign = cursor.fetchone() cursor.execute( """ SELECT up.first_name, up.last_name, up.profile_image, t.amount FROM campaign c INNER JOIN campaign_relation cr ON c.id = cr.campaign_id INNER JOIN transaction t ON t.id = cr.transaction_id INNER JOIN user_profile up ON cr.user_account_id = up.user_account_id WHERE user_role='pledged' AND c.id=%s ORDER BY t.date_created DESC LIMIT 10; """, (id, )) donations = cursor.fetchall() except Exception as e: current_app.logger.error(e) if form.validate_on_submit(): try: connection = get_connection() cursor = get_cursor() with connection: with cursor: cursor.execute( """ SELECT credit_card FROM user_profile WHERE user_account_id=%s """, (session['user_id'], )) cc_number = cursor.fetchone()[0] cursor.execute( """ INSERT INTO transaction(credit_card, amount) VALUES (%s, %s) RETURNING id; """, (cc_number, form.amount.data)) transaction_id = cursor.fetchone()[0] cursor.execute( """ INSERT INTO campaign_relation(user_account_id, campaign_id, transaction_id, user_role) VALUES (%s, %s, %s, %s) """, (session['user_id'], form.campaign_id.data, transaction_id, 'pledged')) flash('Successfully donated!', 'success') setup() return render_template("campaign/campaign.html", form=form, campaign=campaign, donations=donations) except Exception as e: current_app.logger.error(e) flash(e, 'error') flash(form.errors, 'error') setup() return render_template("campaign/campaign.html", form=form, campaign=campaign, donations=donations)
def donation(request, donation_id): Print(request.POST) context = {} context.update(csrf(request)) target_donation = Donation.objects.get(pk=donation_id) Print(target_donation.time) if request.POST: request.POST = request.POST.copy() if request.POST.get('time', '') == '00:00 AM': request.POST['time'] = '12:00 AM' call_redirect = False if 'refresh' in request.POST: messages.add_message( request, messages.INFO, 'Donation Donor Info Updated on all related, non-completed donations' ) updateDonor(target_donation) return redirect('/schedule') elif 'delete' in request.POST: if request.POST.get('delete_series'): deleteDonationSeries(target_donation, request) else: object_check = Donation.objects.filter( parent_donation=target_donation) if object_check: deleteDonationSeries(target_donation, request) else: if target_donation.status != 'picked_up': target_donation.delete() messages.add_message(request, messages.INFO, 'Pickup Deleted') else: messages.add_message( request, messages.INFO, 'Pickup Completed, Delete Failed') return redirect('/schedule') else: update_donor = False if target_donation.donor != Donor.objects.get( pk=request.POST['donor']): update_donor = True requested_date = datetime.datetime.strptime( request.POST['date'], '%m/%d/%Y').date() requested_time = datetime.datetime.strptime( request.POST['time'], '%H:%M %p').time() object_check = Donation.objects.filter( truck=request.POST['truck'], time=requested_time, date=requested_date).exclude(id=target_donation.id) if object_check: messages.add_message(request, messages.INFO, 'Pickup not scheduled: time unavailable') elif target_donation.repetition and request.POST.get( 'update_series', ''): if target_donation.date != requested_date or target_donation.time != requested_time: deleteDonationSeries(target_donation, request) form = DonationForm(request.POST) target_donation = form.save() makeRecurringDonation(target_donation, request) else: target_parent_donation = target_donation if target_donation.parent_donation: target_parent_donation = target_donation.parent_donation for i in Donation.objects.filter( parent_donation=target_parent_donation): if i.status != 'picked_up': i.donor = Donor.objects.get( pk=request.POST['donor']) i.address1 = request.POST['address1'] i.address2 = request.POST['address2'] i.city = request.POST['city'] i.state = request.POST['state'] i.zip_code = request.POST['zip_code'] i.nearest_facility = request.POST[ 'nearest_facility'] i.truck = Truck.objects.get( pk=request.POST['truck']) i.driver = Driver.objects.get( pk=request.POST['driver']) i.repetition = request.POST['repetition'] i.save() if target_parent_donation.status != 'picked_up': target_parent_donation.donor = Donor.objects.get( pk=request.POST['donor']) target_parent_donation.address1 = request.POST[ 'address1'] target_parent_donation.address2 = request.POST[ 'address2'] target_parent_donation.city = request.POST['city'] target_parent_donation.state = request.POST['state'] target_parent_donation.zip_code = request.POST[ 'zip_code'] target_parent_donation.nearest_facility = request.POST[ 'nearest_facility'] target_parent_donation.truck = Truck.objects.get( pk=request.POST['truck']) target_parent_donation.driver = Driver.objects.get( pk=request.POST['driver']) target_parent_donation.repetition = request.POST[ 'repetition'] target_parent_donation.save() target_donation = target_parent_donation if update_donor: updateDonor(target_donation) return redirect('/schedule') elif target_donation.status != 'picked_up' or request.user.has_perm( 'truckmanagement.changefinished_donation'): form = DonationForm(request.POST or None, instance=target_donation) form.save() messages.add_message(request, messages.INFO, 'Pickup changed') if update_donor: updateDonor(target_donation) return redirect('/schedule') else: messages.add_message( request, messages.INFO, 'Pickup complete, modification not possible') return redirect('/schedule') form = fieldsetForm(DonationForm(instance=target_donation), request) context.update({ 'form': form, 'button_value': 'Update', 'page_title': 'Modify Pickup', 'forward_donor': target_donation.donor.id, }) return render_to_response("truckmanagement/donation.html", context, context_instance=RequestContext(request))
def new(request, year, month, day): ''' Page for creating pickups ''' context = {} if request.method == 'POST': Print(request.POST) form = DonationForm(request.POST) object_check = Donation.objects.filter( truck=request.POST['truck'], time=datetime.datetime.strptime(request.POST['time'], '%I:%M %p'), date=datetime.datetime.strptime(request.POST['date'], '%m/%d/%Y')) if object_check: messages.add_message(request, messages.INFO, 'Pickup not scheduled: time unavailable') else: if form.is_valid(): a = form.save() if request.POST.get('repetition'): if makeRecurringDonation(a, request): return redirect('/schedule') else: return redirect('/schedule') target_donor = Donor.objects.get(pk=request.POST['donor']) else: user_settings = UserSettings.objects.get(pk=request.user) target_truck = user_settings.truck target_donor = user_settings.donor form = DonationForm({ 'truck': target_truck.id, 'donor': target_donor.id, 'address1': target_donor.address1, 'address2': target_donor.address2, 'city': target_donor.city, 'state': target_donor.state, 'zip_code': target_donor.zip_code, 'driver': target_truck.driver.id, 'nearest_facility': target_donor.nearest_facility, 'date': datetime.date(int(year), months.index(month) + 1, int(day)), }) context.update(csrf(request)) context.update({ 'form': fieldsetForm(form, request), 'button_value': 'Create', 'page_title': 'New Pickup', 'target_donor': target_donor }) return render_to_response("truckmanagement/donation.html", context, context_instance=RequestContext(request))
<<<<<<< HEAD if request.method == 'POST': data = request.POST new_donation = Donation(first_name=data.get('first_name'), last_name=data.get('last_name'), amount=data.get('amount'), card_number=data.get('card_number'), cvv=data.get('cvv'), message=data.get('message'), donation_made=timezone.now()) new_donation.save() return render(request, "donations/donation_form.html", {'donated': True}) else: return render(request, "donations/donation_form.html", {'donated': False}) ======= form = DonationForm(request.POST or None) context = Context({ 'donated': False, 'form': form }) if request.method == 'POST' and form.is_valid(): data = request.POST new_donation = Donation(name=data.get('name'), amount=data.get('amount'), card_number=data.get('card_number'), message=data.get('message')) new_donation.save() context['donated'] = True return render(request, "donations/donation_form.html", context) else: return render(request, "donations/donation_form.html", {'donated': False,