def booking():
    form = BookingForm()
    if form.validate_on_submit():
        form.send()
        flash('Thanks for contacting us! We promise to get back to you soon.', 'booking')
        return redirect(url_for('booking'))
    return render_template('booking.html', title='Booking', form=form)
Beispiel #2
0
def index(request):
    template = 'booking/index.html'
    form = BookingForm(request.POST or None)
    if form.is_valid():
        booking = form.save(commit=False)
        booking.user = request.user
        booking.save()
        messages.success(request, "Your booking was completed successfully!")
        return redirect('/bookings/')
    context = {'form': form}
    return render(request, template, context)
Beispiel #3
0
def declineBooking(request, pk):
    if request.method == "POST":
        b = get_object_or_404(Booking, pk=pk)
        f = BookingForm(request.POST)
        f.id = pk
        #send email notification
        subject = "East Scarborough Storefront - Booking Approved"
        message = "Dear " + b.booker.username + ",\n\nYour booking request '" + b.name + "' at East Scarborough Storefront has been declined.\nPlease call the Storefront for more details.\n\nThank you"
        b.booker.email_user(subject, message, settings.DEFAULT_FROM_EMAIL)
        b.delete()
        return render(request, 'juakstore/admin_declineBooking.html')
    else:
        form = BookingForm()
        return HttpResponseRedirect(reverse('juakstore/admin_declineBooking.html', args=(pk,)))
Beispiel #4
0
def addbooking(id):
    #TODO: check that gite id belongs to user

    form = BookingForm ()
    uid = g.user.id
    form.customer.choices = [(c.id, c.name)for c in Customer.query.filter_by(user = uid).all()]
    if form.validate_on_submit():
        booking = Booking(start = form.start.data,
                          end = form.end.data,
                          gite_id = id,
                          customer_id = form.customer.data)
        db.session.add(booking)
        db.session.commit()
        flash("booking  rajoute!")
        return redirect(url_for('index'))

    return render_template('editbooking.html', form = form)
Beispiel #5
0
def booking(uid, date_str, time_str):
    resource = User.getByUrlsafeId(uid)
    tz = resource.getTimezoneObject()
    duration = resource.prefs.duration
    dt_str = '%s %s' % (date_str, time_str.replace('-', ':', 1))
    dt_start = tz.localize(date_parser.parse(dt_str))
    dt_end = dt_start + timedelta(minutes=duration)
    form = BookingForm(start_time=dt_start, end_time=dt_end, timezone=tz.zone)
    if request.method == 'POST':
        if form.validate_on_submit():
            booking = Booking.createFromPost(resource, form.data)
            flash('Your booking succeeded.', 'info')
            return redirect(url_for('calendar', uid=uid, date_str=date_str))
        else:
            flash_form_errors('Your booking failed.', form)
    return render_template('booking-new.html', 
        uid=uid, date_str=date_str, time_str=time_str, 
        form=form, resource=resource, 
        dt_start=dt_start, tz=tz, duration=duration)
Beispiel #6
0
def acceptBooking(request, pk):
    if request.method == "POST":
        b = get_object_or_404(Booking, pk=pk)
        f = BookingForm(request.POST)
        f.id = pk
        
        b.approved = True
        b.save()
        #send email notification
        subject = "East Scarborough Storefront - Booking Approved"
        current_site = Site.objects.get_current()        
        text_content = "Dear " + b.booker.username + ",\n\nYour booking request '" + b.name + "' at East Scarborough Storefront has been approved.\nYou can login at " + current_site.name + ".\n\nThank you"
        html_content = 'Dear ' + b.booker.username + ',<br><br>Your booking request ' + b.name + ' at East Scarborough Storefront has been approved.<br>You can login at <a href="http://' + current_site.domain + '">' + current_site.name + '</a>.<br><br>Thank you'
        msg = EmailMultiAlternatives(subject, text_content, settings.DEFAULT_FROM_EMAIL, [b.booker.email])
        msg.attach_alternative(html_content, "text/html")
        msg.send()          
        return render(request, 'juakstore/admin_acceptBooking.html', {'booking': b})
    else:
        form = BookingForm()
        return HttpResponseRedirect(reverse('juakstore/admin_acceptBooking.html', args=(pk,)))
Beispiel #7
0
def booking_calendar(request):
    """
    Calendar widget requires booking objects as json. We get all bookings from DB, then convert them to json.
    This conversion removes several fields from the Booking object, since they cannot be converted to json/aren't required.
    """
    bookings = Booking.objects.all()  # Todo: should load only this month's bookings
    bookings_as_dicts = []
    for booking in bookings:
        bookings_as_dicts.append(booking.to_json())
    bookings_json_string = dumps(bookings_as_dicts)
    form = BookingForm()
    return render(request, "calendar.html", {'bookings': bookings_json_string, 'form': form})
Beispiel #8
0
def new_booking():
    if not current_user.is_authenticated:
        return redirect(url_for('core.home'))

    form = BookingForm()
    if form.validate_on_submit():
        print(form.car_id.data)
        booking = Booking(user_id=current_user.id,
                          car_id=form.car_id.data,
                          start_time=form.start_date_time.data,
                          end_time=form.end_date_time.data)
        booking_validation = booking.is_car_avaiable()
        if booking_validation['result'] == False:
            flash(booking_validation['reason'])
            return render_template('booking/new.html',
                                   title='New Booking',
                                   form=form)
        db.session.add(booking)
        db.session.commit()
        flash('Thanks for making a booking')
        return redirect(url_for('booking_blueprint.bookings'))
    return render_template('booking/new.html', title='New Booking', form=form)
Beispiel #9
0
def booking_done_view():
    form = BookingForm()
    day = form.clientWeekday.data
    time = form.clientTime.data
    teacher_id = form.clientTeacher.data
    if request.method == 'POST' and form.validate():
        name = form.name.data
        phone = form.phone.data
        # Create and update booking.json
        db_manager('booking', [{
            'name': name,
            'phone': phone,
            'teacher_id': teacher_id,
            'weekDay': day,
            'time': time
        }])
        # Timetable updating
        [
            teacher['free'][day].update({str(time) + ':00': False})
            for teacher in TEACHERS_DATA
            if int(teacher['id']) == int(teacher_id)
        ]
        return render_template('booking_done.html',
                               day=day,
                               hour=time,
                               name=name,
                               phone=phone,
                               day_of_week=day_of_week)
    else:
        return redirect(
            url_for('booking_view',
                    data=TEACHERS_DATA,
                    day=day,
                    hour=time,
                    day_of_week=day_of_week,
                    teacher_id=teacher_id,
                    form=form))
Beispiel #10
0
def render_booking(teacher_id, day, time):
    current_teacher = s.get_teacher(teacher_id)
    try:
        s.get_schedule(teacher_id)[day][f"{time}:00"]
    except KeyError:
        abort(404)
    form = BookingForm()
    return render_template("booking.html",
                           teacher_name=current_teacher.name,
                           teacher_id=teacher_id,
                           teacher_picture=current_teacher.picture,
                           day_key=day,
                           day_value=days[day],
                           time=f"{time}:00",
                           form=form,
                           title="Запись на пробный урок",
                           url=request.url)
Beispiel #11
0
def render_booking_done(id_teacher, day, time):
    form = BookingForm()
    if request.method == "POST":
        name = form.name.data
        phone = form.phone.data
        with open("booking.json", "r") as r:
            booking = json.load(r)
        booking.append({
            "day": day,
            "time": time,
            "name": name,
            "phone": phone
        })
        with open("booking.json", "w") as w:
            json.dump(booking, w)
        with open("booking.json", "r") as f:
            bookings_json = json.loads(f.read())
        index = 0
        bookings = Booking(name=bookings_json[index]["name"],
                           phone=bookings_json[index]["phone"],
                           time=bookings_json[index]["time"],
                           day=bookings_json[index]["day"])
        db.session.add(bookings)
        index += 1
        db.session.commit()
        return render_template("booking_done.html",
                               day=day,
                               time=time,
                               name=name,
                               phone=phone)
    else:
        teachers = db.session.query(Teacher)
        id_t = int(id_teacher)
        return render_template("booking.html",
                               teachers=teachers,
                               id=id_t,
                               day=day,
                               time=time,
                               form=form)
Beispiel #12
0
def render_booking_done():
    form = BookingForm()

    if not form.phone.data:
        form = RequestForm()
        return render_template(
            'request.html',
            form=form,
        )

    name = form.name.data
    phone = form.phone.data
    day = form.day.data
    time = form.time.data
    teacher_id = form.teacher_id.data
    timeslot_id = form.timeslot_id.data

    if Student.query.filter(Student.phone == phone).scalar():
        student = Student.query.filter(Student.phone == phone).scalar()
    else:
        student = Student(name=name, phone=phone)
        db.session.add(student)
        db.session.commit()

    booking = Booking(student_id=student.id,
                      teacher_id=teacher_id,
                      timeslot_id=timeslot_id)
    db.session.add(booking)
    db.session.commit()

    return render_template(
        'booking_done.html',
        form=form,
        name=name,
        phone=phone,
        day=day,
        time=time,
    )
 def get(self, request, *args, **kwargs):
     try:
         email_form = EmailForm(instance=request.user)
         phone_form = PhoneForm(instance=request.user.profile)
         booking_form = BookingForm({
             'name':
             request.user.first_name + ' ' + request.user.last_name
         })
         tz = pytz.timezone(settings.TIME_ZONE)
         my_bookings = Booking.objects.filter(madeBy=request.user,
                                              start__gt=datetime.now(tz),
                                              app_config=self.config)
         delete_booking = DeleteBooking()
     except AttributeError:
         return render(request, 'bookings.html')
     return render(
         request, 'bookings.html', {
             'email_form': email_form,
             'phone_form': phone_form,
             'booking_form': booking_form,
             'my_bookings': my_bookings,
             'delete_booking': delete_booking,
         })
Beispiel #14
0
def addBooking(request):
    if request.method == "POST":
        f = BookingForm(request.POST, initial={'booker':request.user, 'room':0})
        if f.is_valid():
            first_booking = 0
            #relatedBookings = []
            submittedBookings = []
            for room in f.cleaned_data['room']:
                newBooking = Booking(name=f.cleaned_data['name'],
                              notes=f.cleaned_data['notes'],
                              date=f.cleaned_data['date'],
                              start=f.cleaned_data['start'],
                              end=f.cleaned_data['end'],
                              booker=get_object_or_404(User, pk=request.user.id),
                              approved=False,
                              category=get_object_or_404(BookingCategory, pk=request.POST['category']),
                              room=get_object_or_404(Room, pk=room.pk))
                #auto-approve if admin
                if request.user.is_staff:
                    newBooking.approved = True
                newBooking.save()
                if first_booking == 0:
                    first_booking = newBooking
                submittedBookings.append(newBooking)
                #relatedBookings.append([newBooking])


                if (f.cleaned_data['repeat']): # repeat is specified
                    curr_date = f.cleaned_data['date']

                    ''' Timestep will be the increment for the repeat '''
                    if f.cleaned_data['repeat_frequency_unit'] == 'day':
                        timestep =  datetime.timedelta(days=f.cleaned_data['repeat_frequency'])
                    elif f.cleaned_data['repeat_frequency_unit'] == 'week':
                        timestep = datetime.timedelta(weeks=f.cleaned_data['repeat_frequency'])
                    elif f.cleaned_data['repeat_frequency_unit'] == 'month':
                        timestep = datetime.timedelta(weeks=4 * f.cleaned_data['repeat_frequency'])

                    while curr_date + timestep <= f.cleaned_data['repeat_end'] and timestep:
                        curr_date += timestep
                        '''
                            TODO: check if this booking conflicts with any others
                        '''
                        repeatBooking = Booking(name=f.cleaned_data['name'],
                              notes=f.cleaned_data['notes'],
                              date=curr_date,
                              start=f.cleaned_data['start'],
                              end=f.cleaned_data['end'],
                              booker=get_object_or_404(User, pk=request.user.id),
                              approved=False,
                              category=get_object_or_404(BookingCategory, pk=request.POST['category']),
                              room=get_object_or_404(Room, pk=room.pk))
                        if request.user.is_staff:
                            repeatBooking.approved = True
                        repeatBooking.save()
                        submittedBookings.append(repeatBooking)
                        RepeatBooking(source=newBooking, target=repeatBooking).save()
            #            relatedBookings[-1].append(repeatBooking)
            #
            #if len(relatedBookings[0]) > 1:
            #    for i in range(0, len(relatedBookings)):
            #        for j in range(1, len(relatedBookings[i])):
            #            if not relatedBookings[i][j] is None:
            #                RepeatBooking(source=relatedBookings[i][0], target=relatedBookings[i][j]).save()

            #if len(relatedBookings) > 1:
            #    for j in range(0, len(relatedBookings[0])):
            #        for i in range(1, len(relatedBookings)):
            #            if not relatedBookings[i][j] is None:
            #                MultiRoomBooking(source=relatedBookings[0][j], target=relatedBookings[i][j]).save()

            # notify all admins of booking request, dont send if admin makes a booking
            admins = User.objects.filter(is_staff=True) 
            subject = "East Scarborough Storefront - Booking Request"
            message = request.user.username + " has requested a booking.\n\nBooking info:\nName: " + newBooking.name + "\nNotes: " + newBooking.notes + "\nDate: " + str(newBooking.date) +"\nTime: " + str(newBooking.start) + " - " + str(newBooking.end) + "\nCategory: " + newBooking.category.name + "\nRoom: " + newBooking.room.name
            if newBooking.booker.is_staff == False:
                for a in admins:
                    a.email_user(subject, message, settings.DEFAULT_FROM_EMAIL)                        
            return render(request, 'juakstore/booking_add.html', {'form':BookingForm(),
                                                                  'all_bookings':Booking.objects.all().filter(approved=True),
                                                                  'year': datetime.datetime.now().year,
                                                                  'month': datetime.datetime.now().month,
                                                                  'day': datetime.datetime.now().day,
                                                                  'submittedBookings': submittedBookings,
                                                                  })
        else:
            return render(request, 'juakstore/booking_add.html', {'form': f,
                                                                  'all_bookings':Booking.objects.all().filter(approved=True),
                                                                  'year': datetime.datetime.now().year,
                                                                  'month': datetime.datetime.now().month,
                                                                  'day': datetime.datetime.now().day,
                                                                  })
    else:
        return HttpResponseRedirect(reverse('juakstore:bookingCreate'))
Beispiel #15
0
    def post(self, obj_id=None):
        """
       Create a new booking/ Update a booking
       ---
       tags:
         - bookings
       parameters:
         - in: path
           name: obj_id
         - in: body
           name: body
           schema:
             id: Booking
             required:
               - room_number
               - customerID
             optional:
               - is_active
             properties:
               customerID:
                 type: string
                 description: customer id
               room_number:
                 type: integer
                 description: room number
               is_active:
                 type: boolean
                 description: room booked
       responses:
         201:
           description: Booking created
       """
        data = self.prepare_data()
        if obj_id:
            form = UpdateBookingForm(data, csrf_enabled=False)
            if form.validate():
                booking = Booking.get_by_id(int(obj_id))
                if not booking:
                    abort(404, message="Booking with key ({}) not found".format(obj_id))

                booking.is_active = form.is_active.data
                booking.put()

                room = Room.query(Room.number == booking.room_number).get()
                room.is_booked = True if booking.is_active is True else False
                room.put()

                output = self.output_fields
                output.update(self.resource_fields)
                return marshal(booking, output), 200

            error_data = self.prepare_errors(form.errors)
            raise CustomException(code=400, name='Validation Failed', data=error_data)

        else:
            form = BookingForm(data, csrf_enabled=False)
            if form.validate():
                booking = Booking(customerID=form.customerID.data, room_number=int(form.room_number.data),
                                  is_active=True)
                booking.put()
                booking.id = str(booking.key.id())
                booking.put()

                room = Room.query(Room.number == booking.room_number).get()
                room.is_booked = True if booking.is_active is True else False
                room.put()

                output = self.output_fields
                output.update(self.resource_fields)
                return marshal(booking, output), 201

            error_data = self.prepare_errors(form.errors)
            raise CustomException(code=400, name='Validation Failed', data=error_data)
Beispiel #16
0
def show_bookings(form=None):
    if form is None:
        form = BookingForm()
    bookings = read_csv_file(bookings_file_path)
    return render_template('bookings.html', form=form, bookings=bookings)
Beispiel #17
0
def app(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/html; charset=utf-8')])

    this_now = datetime.datetime.now()

    #today = date.today() #today.year, today.month, today.day

    pages = [
        "crafts-gallery", "private-events", "about-us", "media", "reviews",
        "custom-built", "after-school", "summer-camp", "art-camp",
        "3-wednesdays-workshop", "listening-room", "wheel-wars"
    ]

    #galleries_dict = {"acrylic-painting": 1, "watercolor-painting": 2, "paint-your-pet": 3, "fused-glass": 4, "resin-crafts": 5, "fluid-art": 6, "commissioned-art": 8, "alcohol-ink": 9, "artist-guided-family-painting": 10, "handbuilt-pottery": 11, "leathercraft": 12, "water-marbling": 13, "pottery-painting": 18, "string-art": 19, "pottery-lessons": 20}

    #mysql -u catalystcreative_cca catalystcreative_66
    #SELECT lower(name), id FROM piwigz_categories order by id asc;
    galleries_dict = {
        "acrylic-painting": 1,
        "watercolor-painting": 2,
        "paint-your-pet": 3,
        "fused-glass": 4,
        "resin-crafts": 5,  # was "resin-art"
        "fluid-art": 6,
        "summer-art-camp": 7,
        "commissioned-art": 8,
        #"alcohol-ink": 9,
        "artist-guided-family-painting": 10,
        "handbuilt-pottery": 11,
        "leathercraft": 12,
        "water-marbling": 13,
        #"private-events": 14,
        #"custom-counter-tops-tables": 15,
        #"about-us": 16,
        "pottery-painting": 18,
        "string-art": 19,
        "pottery-lessons": 20
    }

    galleries_list = list(galleries_dict.keys())
    galleries_dict_vals = list(galleries_dict.values())

    db = MySQLdb.connect(host="localhost",
                         user=dbuser,
                         passwd=passwd,
                         db="catalystcreative_arts")

    ####
    ####
    if environ['REQUEST_METHOD'] == "GET":

        if environ['PATH_INFO'] == '/admin/events/list':
            c = db.cursor()
            c.execute(
                f"SELECT * FROM events WHERE edatetime >= CURDATE() ORDER BY edatetime"
            )
            allrows = c.fetchall()
            c.close()
            template = env.get_template("admin-events-list.html")
            response = template.render(allrows=allrows)

        elif environ['PATH_INFO'] == '/admin/orders/list':
            db.query(
                f"select *, ceiling(ship_date / 10000000000000) as ship_group from cart_order where status = 'complete' order by ship_group, ship_date desc, checkout_date desc"
            )
            #db.query(f"select * from cart_order where status = 'complete' order by ship_date desc, checkout_date desc") # and ship_date is NULL
            r = db.store_result()
            allrows = r.fetch_row(maxrows=100, how=1)

            new_allrows = []

            overall_total = 0
            per_month_totals = {}

            for row in allrows:
                cart_order_id = row["cart_order_id"]
                #db.query(f"select product_id, quantity from cart_order_product where cart_order_id = {cart_order_id}")

                try:
                    content = read_file(
                        f"../cart-api/details/{cart_order_id}.json")
                    details = json.loads(content)
                    email = details["payer"]["email_address"]
                    address = details["purchase_units"][0]["shipping"][
                        "address"]
                    given_name = details["payer"]["name"]["given_name"]
                    surname = details["payer"]["name"]["surname"]
                    name = f"{given_name} {surname}"
                    try:
                        phone = details["payer"]["phone"]["phone_number"][
                            "national_number"]
                    except:
                        phone = ""
                except:
                    pass

                db.query(
                    f"select a.product_id, a.quantity, b.name, b.inventory \
                    from cart_order_product a, products b \
                    where a.product_id = b.pid and cart_order_id = {cart_order_id}"
                )
                r = db.store_result()
                sub_allrows = r.fetch_row(maxrows=100, how=1)
                row["products"] = sub_allrows

                try:
                    row["email"] = email
                    row["address"] = address
                    row["name"] = name
                    row["phone"] = phone
                except:
                    pass

                new_allrows.append(row)

                overall_total += int(row["total"])

                checkout_month = str(row["checkout_date"]).split("-")[1]

                try:
                    per_month_totals[checkout_month] += int(row["total"])
                except:
                    per_month_totals[checkout_month] = int(row["total"])

            template = env.get_template("admin-orders-list.html")
            response = template.render(allrows=new_allrows,
                                       overall_total=overall_total,
                                       per_month_totals=per_month_totals)

        elif environ['PATH_INFO'] == '/admin/events/add-edit':
            if len(environ['QUERY_STRING']) > 1:
                eid = environ['QUERY_STRING'].split("=")[1]
                db.query(f"SELECT * FROM events WHERE eid = {eid}")
                r = db.store_result()
                row = r.fetch_row(maxrows=1, how=1)[0]
                form = EventsForm(**row)

                c = db.cursor()
                c.execute(
                    f"SELECT * FROM events WHERE tags like '%series={eid}%'")
                children = c.fetchall()
                c.close()
            else:
                form = EventsForm()
                children = None
            template = env.get_template("admin-events-add-edit.html")
            response = template.render(form=form, children=children)

        elif environ['PATH_INFO'] == "/admin/events/delete":
            eid = int(environ['QUERY_STRING'].split("=")[1])
            if type(eid) == int:

                # FIRST UPDATE THE HTML PAGE !!!!
                db.query(f"SELECT * FROM events WHERE eid = {eid}")
                e = db.store_result()
                event = e.fetch_row(maxrows=1, how=1)[0]
                event["quantity_sum"] = 0
                event["remaining_spots"] = 0
                template = env.get_template("event.html")
                content = template.render(event=event, deleted=True)
                write_file(f"../www/event/{eid}.html", content)

                sql = f"DELETE FROM events WHERE eid = {eid}"
                c = db.cursor()
                c.execute(sql)
                c.close()

                response = '<meta http-equiv="refresh" content="0; url=/app/admin/events/list" />'
            else:
                response = ""

        #### ####
        #### ####
        elif environ['PATH_INFO'] == '/build-individual-event':
            if environ['QUERY_STRING']:
                eid = int(environ['QUERY_STRING'].split("=")[1])
                db.query(f"SELECT * FROM events WHERE eid = {eid}")
            else:
                db.query("SELECT * FROM events WHERE edatetime >= CURTIME() \
                    and (tags <> 'invisible' or tags is null) ORDER BY edatetime"
                         )
            e = db.store_result()
            allrows = e.fetch_row(maxrows=100, how=1)

            template = env.get_template("event.html")
            upcoming_event_ids = []
            for event in allrows:
                eid = event["eid"]
                elimit = event["elimit"]

                db.query(
                    f"select sum(quantity) as quantity_sum from orders where eid = {eid}"
                )
                q = db.store_result()
                quantity_sum = q.fetch_row(maxrows=1, how=1)[0]["quantity_sum"]

                try:
                    event["quantity_sum"] = int(quantity_sum)
                    event["remaining_spots"] = int(elimit) - int(quantity_sum)
                except:
                    event["quantity_sum"] = 0
                    event["remaining_spots"] = int(elimit)

                upcoming_event_ids.append(eid)
                content = template.render(event=event)
                write_file(f"../www/event/{eid}.html", content)
            write_file(f"data/upcoming_event_ids.json",
                       json.dumps(upcoming_event_ids, indent=4))
            response = "build-individual-event"
        #### ####
        #### ####

        elif environ['PATH_INFO'] == '/list/events' or environ[
                'PATH_INFO'] == '/calendar':

            db.query(
                "SELECT * FROM events WHERE edatetime >= CURTIME() and (tags <> 'invisible' or tags is null) ORDER BY edatetime"
            )
            r = db.store_result()
            allrows = r.fetch_row(maxrows=100, how=1)

            db.query(
                "SELECT eid, SUM(quantity) as sum_quantity FROM orders GROUP BY eid"
            )
            # TODO: May need to add join to events table above
            # so as to only pull future event dates
            r = db.store_result()
            orders_count = r.fetch_row(maxrows=100, how=1)

            orders_count_object = {}
            for item in orders_count:
                key = int(item['eid'])
                val = int(item['sum_quantity'])
                orders_count_object[key] = val

            events_object = {}
            parent = {}
            for row in allrows:
                eid = row["eid"]
                events_object[eid] = {}
                events_object[eid]["date"] = int(row["edatetime"].timestamp())
                events_object[eid]["title"] = row["title"]
                events_object[eid]["price"] = row["price"]
                events_object[eid]["price_text"] = row["price_text"]

                if "series" in row["tags"]:
                    parent[eid] = re.sub('^.*series=(\d+).*$', r'\1',
                                         row["tags"])
                else:
                    parent[eid] = ""

            events_object = json.dumps(events_object)

            try:
                test = environ['QUERY_STRING'].split("=")[1]
            except:
                test = ""

            template = env.get_template("list-events.html")
            response = template.render(events=allrows,
                                       orders_count=orders_count_object,
                                       events_object=events_object,
                                       parent=parent,
                                       test=test)

        elif environ['PATH_INFO'] == '/cart':
            template = env.get_template("cart-list.html")
            response = template.render()

        elif environ['PATH_INFO'] == '/products':
            db.query("SELECT * FROM products WHERE active = 1")
            r = db.store_result()
            allrows = r.fetch_row(maxrows=100, how=1)
            template = env.get_template("list-products.html")
            response = template.render(products=allrows)

        elif re.match('/products/[a-z-]+/[0-9]+', environ['PATH_INFO']):
            path_parts = environ['PATH_INFO'].split('/')
            product_name = path_parts[2]
            product_id = path_parts[3]
            pid = int(product_id)
            db.query(f"SELECT * FROM products WHERE pid = {pid}")
            r = db.store_result()
            row = r.fetch_row(maxrows=1, how=1)[0]
            template = env.get_template("product-detail.html")
            response = template.render(row=row)

        elif environ['PATH_INFO'] == '/book/event':
            eid = environ['QUERY_STRING'].split("=")[1]
            db.query(f"SELECT * FROM events WHERE eid = {eid}")
            r = db.store_result()
            row = r.fetch_row(maxrows=1, how=1)[0]

            db.query(f"SELECT count(id) as cnt FROM orders WHERE eid = {eid}")
            o = db.store_result()
            order_count = o.fetch_row(maxrows=1, how=1)[0]["cnt"]

            template = env.get_template("book-event.html")
            response = template.render(event_data=row, order_count=order_count)

        elif environ['PATH_INFO'] == '/gallery/slideshow' or environ[
                'PATH_INFO'].lstrip('/') in galleries_list:

            if environ['PATH_INFO'] == '/gallery/slideshow':
                gid = int(environ['QUERY_STRING'].split("=")[1])
            else:
                path_info = environ['PATH_INFO'].lstrip('/')
                gid = int(galleries_dict[path_info])

            try:
                g = Gallery(gid)
                gallery = g.get_gallery()
                images = g.get_images()

                template = env.get_template("gallery-slideshow.html")
                response = template.render(gallery=gallery, images=images)
            except:
                template = env.get_template("main.html")
                response = template.render(
                    path_info=f"{path_info} gallery does not yet exist")

        ####
        elif environ['PATH_INFO'] == '/admin/booking/list':

            view = ""
            gtlt = ">="  # default
            ascdesc = "asc"

            if environ['QUERY_STRING'] and "view" in environ['QUERY_STRING']:
                view = environ['QUERY_STRING'].split("=")[1]

                if view == "past-events":
                    gtlt = "<"
                    ascdesc = "desc"

            # TODO: This first part should be call-able separately
            # And should be called about once per minute

            # OR EVEN BETTER:
            # Maybe this chunk should be moved to the
            # /paypal-transaction-complete section

            # List, sort, then read all files in the orders/ folder
            files = [
                f for f in listdir("orders/") if isfile(join("orders/", f))
            ]

            if len(files) > 0 and view != "past-events":
                # PART-1: Load new orders into database:
                # Reminder: Orders data files are saved as event_eid_value.json
                for f in files:
                    eid = f.replace(".json", "").strip()
                    event_orders_data = json.loads(read_file(f"orders/{f}"))

                    for cca_order_id, cca_order in event_orders_data.items():

                        try:
                            order = cca_order["paypal"]

                            #print("____cca_order_id", cca_order_id)
                            #print("____order", order)
                            data_array = []
                            # We don't necessarily want all data from orders/event_eid_value.json
                            # So let's pick and choose what data we want to keep:

                            data_array.append(order['orderID'])

                            data_array.append(eid)
                            data_array.append(order['details']['create_time'])
                            data_array.append(
                                order['details']['payer']['email_address'])
                            data_array.append(order['details']['payer']['name']
                                              ['given_name'])
                            data_array.append(
                                order['details']['payer']['name']['surname'])
                            data_array.append(order['quantity'])
                            # Notice zero after purchase_units:
                            data_array.append(
                                order['details']['purchase_units'][0]['amount']
                                ['value'])
                            data_array.append(
                                order['details']['purchase_units'][0]
                                ['payments']['captures'][0]['amount']['value'])

                            # FOR VARIABLE_TIME FIELD
                            try:
                                data_array.append(order['variable_time_slot'])
                            except:
                                data_array.append('no variable time slot')

                            # FOR EXTRA_DATA FIELD
                            try:
                                total_number_scarf = json.dumps({
                                    "total_number_scarf":
                                    int(order['total_number_scarf'])
                                })
                                data_array.append(total_number_scarf)
                            except:
                                data_array.append('not an event with scarf')

                            try:
                                data_array.append(
                                    order["details"]["purchase_units"][0]
                                    ["payments"]["captures"][0]["id"])
                            except:
                                data_array.append("transaction id")

                            data_array.append(cca_order["cca_buyer_name"])
                            data_array.append(cca_order["cca_buyer_phone"])

                            # Load database:
                            fields = "order_id, eid, create_time, email, first_name, last_name, quantity, cost, paid, variable_time, extra_data, transaction_id, buyer_name, buyer_phone"
                            #vals = str(data_array).lstrip('[').rstrip(']')
                            vals = data_array
                            #sql = f"INSERT INTO orders ({fields}) VALUES ({vals})"
                            sql = f"INSERT INTO orders ({fields}) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"

                            c = db.cursor()
                            #c.execute(sql)
                            c.execute(sql, vals)
                            c.close()

                        except:
                            pass

                    # So that this event json doesn't get processed again:
                    try:
                        # If existing json for this event exists, add new json to it:
                        event_dict = json.loads(
                            read_file(f"orders/loaded/{f}"))
                        event_dict.update(event_orders_data)
                        write_file(f"orders/loaded/{f}",
                                   json.dumps(event_dict, indent=4))
                        # REMOVE the file
                        os.remove(f"orders/{f}")
                    except:
                        # MOVE the file to /orders/loaded/event_eid_value.json
                        os.rename(f"orders/{f}", f"orders/loaded/{f}")

            # PART-2: Select future-event-date orders from database for admin view
            #c = db.cursor()
            query = f"SELECT e.title, e.edatetime, e.elimit, o.* \
                FROM events e, orders o WHERE e.eid = o.eid AND e.edatetime {gtlt} CURDATE() ORDER BY e.edatetime {ascdesc}"

            #c.execute(query)
            #allrows = c.fetchall()
            #c.close()

            # NOTE: changing from db.cursor / c.execute to db.query / db.store_result for use of dict in template

            db.query(query)
            r = db.store_result()
            allrows = r.fetch_row(maxrows=1000, how=1)

            template = env.get_template("admin-booking-list.html")
            response = template.render(orders=allrows)

        ####
        elif environ['PATH_INFO'] == '/admin/booking/add-edit':

            c = db.cursor()
            c.execute(
                f"SELECT * FROM events WHERE edatetime > CURTIME() order by edatetime asc"
            )
            allevents = c.fetchall()
            c.close()

            if len(environ['QUERY_STRING']) > 1:
                order_id = environ['QUERY_STRING'].split("=")[1]
                db.query(f"SELECT * FROM orders WHERE id = {order_id}")
                r = db.store_result()
                row = r.fetch_row(maxrows=1, how=1)[0]
                form = BookingForm(**row)
            else:
                form = BookingForm()

            template = env.get_template("admin-booking-add-edit.html")
            response = template.render(form=form,
                                       allevents=allevents,
                                       this_now=this_now)

        elif environ['PATH_INFO'] == '/home':
            # UP-NEXT EVENT
            month = int(this_now.strftime("%m"))
            year = int(this_now.strftime("%Y"))
            html_cal = make_cal(db, month, year)

            #print(html_cal)

            db.query(f"SELECT * FROM events WHERE edatetime > CURTIME() \
                and (tags <> 'invisible' or tags is null) \
                and title != 'Private Event' \
                and description not like '%Private Event%' \
                and title not like '%Studio Closed%' \
                and title != 'Studio Closed' \
                ORDER BY edatetime limit 1")
            r = db.store_result()
            next_event = r.fetch_row(maxrows=1, how=1)[0]
            #html_cal = ""
            #next_event = ""

            # FEATURED / PINNED EVENTS
            db.query(
                f"SELECT * FROM events WHERE edatetime > CURTIME() and tags = 'home' ORDER BY edatetime limit 10"
            )
            r = db.store_result()
            events_tagged_home = r.fetch_row(maxrows=10, how=1)

            # RANDOM PRODUCT
            db.query(
                f"select pid, name, image_path_array, price from products where inventory > 0 and active = 1 ORDER BY RAND() LIMIT 1"
            )
            r = db.store_result()
            random_product = r.fetch_row(maxrows=10, how=1)[0]

            # GALLERY / SLIDESHOW
            #random_number = random.randint(1,len(galleries_dict))
            random_number = random.choice(galleries_dict_vals)
            g = Gallery(random_number)
            gallery = g.get_gallery()
            images = g.get_images()

            # Event List:
            event_list_html = make_list(db)

            template = env.get_template("home.html")
            response = template.render(next_event=next_event,
                                       calendar={"html": html_cal},
                                       events_tagged_home=events_tagged_home,
                                       gallery=gallery,
                                       images=images,
                                       event_list_html=event_list_html,
                                       random_product=random_product)

        elif environ['PATH_INFO'] == '/admin/pages':
            template = env.get_template("admin-pages.html")
            if environ['QUERY_STRING']:
                page_name = environ['QUERY_STRING'].split("=")[1]
                try:
                    page_content = read_file(f"data/{page_name}.html")
                except:
                    page_content = None
                response = template.render(page_name=page_name,
                                           page_content=page_content)
            else:
                response = template.render(pages=pages)

        elif environ['PATH_INFO'] == '/admin/registration':
            #c = db.cursor()
            #c.execute("SELECT * FROM registration ORDER BY session_detail")
            #allrows = c.fetchall()
            #c.close()

            view = ""
            if environ['QUERY_STRING'] and "view" in environ['QUERY_STRING']:
                view = environ['QUERY_STRING'].split("=")[1]

            if view == "all":
                special = ""
                orderby = "order_id, session_detail"
            else:
                special = "where order_id is not NULL"
                orderby = "session_detail"

            db.query(
                f"SELECT * FROM registration {special} ORDER BY {orderby}")
            r = db.store_result()
            allrows = r.fetch_row(maxrows=100, how=1)

            data = json.loads(
                read_file("../registration/data/wheel-wars.json"))

            template = env.get_template("admin-registration.html")
            response = template.render(allrows=allrows, data=data)

        #elif environ['PATH_INFO'] == '/admin/registration2':
        #    data = json.loads(read_file("../registration/data/wheel-wars.json"))
        #    template = env.get_template("admin-registration2.html")
        #    response = template.render(data=data)

        elif environ['PATH_INFO'] == '/summer-camp-registration':
            template = env.get_template("summer-camp-registration.html")
            form = RegistrationForm()
            response = template.render(form=form)

        elif environ['PATH_INFO'] == '/art-camp-registration':
            template = env.get_template("art-camp-registration.html")
            form = RegistrationForm()
            response = template.render(form=form)

        elif environ['PATH_INFO'].lstrip('/') in pages:
            page_name = environ['PATH_INFO'].lstrip('/')
            page_content = str(read_file(f"data/{page_name}.html"))
            template = env.get_template("pages.html")
            response = template.render(page_name=page_name,
                                       page_content=page_content)

        elif environ['PATH_INFO'] == '/admin/products/list':
            c = db.cursor()
            c.execute("SELECT * FROM products order by pid desc")
            allrows = c.fetchall()
            c.close()
            template = env.get_template("admin-products-list.html")
            response = template.render(allrows=allrows)

        elif environ['PATH_INFO'] == '/admin/products/add-edit':
            if len(environ['QUERY_STRING']) > 1:
                pid = environ['QUERY_STRING'].split("=")[1]
                db.query(f"SELECT * FROM products WHERE pid = {pid}")
                r = db.store_result()
                row = r.fetch_row(maxrows=1, how=1)[0]
                form = ProductsForm(**row)
            else:
                form = ProductsForm()
            template = env.get_template("admin-products-add-edit.html")
            response = template.render(form=form)

        elif environ['PATH_INFO'] == '/admin/products/delete':
            if len(environ['QUERY_STRING']) > 1:
                pid = int(environ['QUERY_STRING'].split("=")[1])
                sql = f"DELETE FROM products WHERE pid = {pid}"
                c = db.cursor()
                c.execute(sql)
                c.close()
                sql = f"DELETE FROM cart_order_product WHERE product_id = {pid}"
                c = db.cursor()
                c.execute(sql)
                c.close()
                response = '<meta http-equiv="refresh" content="0; url=/app/admin/products/list" />'
            else:
                response = ""

        else:
            path_info = environ['PATH_INFO'].lstrip('/')
            template = env.get_template("main.html")
            response = template.render(path_info=path_info)

    ####
    ####
    elif environ['REQUEST_METHOD'] == "POST" and environ[
            'PATH_INFO'] == "/paypal-transaction-complete":

        length = int(environ.get('CONTENT_LENGTH', '0'))
        post_input = environ['wsgi.input'].read(length).decode('UTF-8')
        form_orders = json.loads(post_input)
        event_id = str(form_orders['event_id'])

        try:
            orders = json.loads(read_file(f"orders/{event_id}.json"))
        except:
            orders = []

        orders.append(form_orders)
        write_file(f"orders/{event_id}.json", json.dumps(orders, indent=4))
        response = "200"

        #scrape_and_write("calendar")

    ####
    ####
    elif environ['REQUEST_METHOD'] == "POST" and environ[
            'PATH_INFO'] == "/product-image/upload":
        length = int(environ.get('CONTENT_LENGTH', '0'))
        # NOTICE: NOT DECODING post_input below FOR IMAGES
        post_input = environ['wsgi.input'].read(length)
        # NOTICE: BYTES STRING below FOR IMAGES
        image_pid = post_input.split(b'Content-Disposition: form-data')[1]
        pid = re.sub(b'^.*name="pid"(.*?)------.*$',
                     r"\1",
                     image_pid,
                     flags=re.DOTALL).strip()
        pid = int(pid.decode('UTF-8'))
        image_data = post_input.split(b'Content-Disposition: form-data')[2]
        image_filename = re.sub(b'^.*filename="(.*?)".*$',
                                r"\1",
                                image_data,
                                flags=re.DOTALL).strip()
        image_contents = re.sub(b'^.*Content-Type: image/jpeg(.*)$',
                                r"\1",
                                image_data,
                                flags=re.DOTALL).strip()
        img_name = image_filename.decode('UTF-8')
        open(f"../www/img/orig/{img_name}", 'wb').write(image_contents)
        size = 350, 350
        image = Image.open(f"../www/img/orig/{img_name}")
        image.thumbnail(size)
        image.save(f"../www/img/small/{img_name}", 'JPEG')
        sql = f"UPDATE products SET image_path_array = concat(ifnull(image_path_array,''), ',{img_name}') WHERE pid = {pid}"
        c = db.cursor()
        c.execute(sql)
        c.close()
        response = f'<meta http-equiv="refresh" content="0; url=/app/admin/products/list" />'

    ####
    ####
    elif environ['REQUEST_METHOD'] == "POST" and environ[
            'PATH_INFO'] == "/image/upload":
        length = int(environ.get('CONTENT_LENGTH', '0'))
        # NOTICE: NOT DECODING post_input below FOR IMAGES
        post_input = environ['wsgi.input'].read(length)

        #print(post_input)

        # NOTICE BYTES STRING below FOR IMAGES
        image_eid = post_input.split(b'Content-Disposition: form-data')[1]
        eid = re.sub(b'^.*name="eid"(.*?)------.*$',
                     r"\1",
                     image_eid,
                     flags=re.DOTALL).strip()
        eid = int(eid.decode('UTF-8'))
        image_data = post_input.split(b'Content-Disposition: form-data')[2]
        image_filename = re.sub(b'^.*filename="(.*?)".*$',
                                r"\1",
                                image_data,
                                flags=re.DOTALL).strip()
        image_contents = re.sub(b'^.*Content-Type: image/jpeg(.*)$',
                                r"\1",
                                image_data,
                                flags=re.DOTALL).strip()
        img_name = image_filename.decode('UTF-8')
        if img_name and image_contents:
            open(f"../www/img/orig/{img_name}", 'wb').write(image_contents)
            size = 350, 350
            image = Image.open(f"../www/img/orig/{img_name}")
            image.thumbnail(size)
            image.save(f"../www/img/small/{img_name}", 'JPEG')
            sql = f"UPDATE events SET image = '{img_name}' WHERE eid = {eid}"
            c = db.cursor()
            c.execute(sql)
            c.close()
        response = f'<meta http-equiv="refresh" content="0; url=/app/admin/events/list" />'

    ####
    ####
    elif environ['REQUEST_METHOD'] == "POST" and environ[
            'PATH_INFO'] == "/contact":
        length = int(environ.get('CONTENT_LENGTH', '0'))
        post_input = environ['wsgi.input'].read(length).decode('UTF-8')

        data_object = json.loads(read_file("data/contactus.json"))

        post_input_array = post_input.split('------')

        message_object = {}
        for d in post_input_array:
            post_data_key = re.sub(r'^.*name="(.*?)".*$',
                                   r"\1",
                                   d,
                                   flags=re.DOTALL).strip()
            post_data_val = re.sub(r'^.*name=".*?"(.*)$',
                                   r"\1",
                                   d,
                                   flags=re.DOTALL).strip()
            if len(post_data_key) > 1 and not post_data_key.startswith(
                    'WebKitForm') and post_data_key != "submit":
                message_object[post_data_key] = post_data_val

            data_object[str(this_now)] = message_object

        email = data_object[str(this_now)]["email"]

        write_file(f"data/contactus.json", json.dumps(data_object, indent=4))

        #template = env.get_template("about-us.html")
        #response = template.render(thanks=data_object[str(this_now)])

        page_content = str(read_file(f"data/about-us.html"))
        template = env.get_template("pages.html")
        page_name = "about-us"
        response = template.render(page_name=page_name,
                                   page_content=page_content,
                                   email=email)

    ####
    ####
    elif environ['REQUEST_METHOD'] == "POST" and environ[
            'PATH_INFO'] == "/admin/pages":
        length = int(environ.get('CONTENT_LENGTH', '0'))
        post_input = environ['wsgi.input'].read(length).decode('UTF-8')

        post_input_array = post_input.split('------')

        data_object = {}
        for d in post_input_array:
            post_data_key = re.sub(r'^.*name="(.*?)".*$',
                                   r"\1",
                                   d,
                                   flags=re.DOTALL).strip()
            post_data_val = re.sub(r'^.*name=".*?"(.*)$',
                                   r"\1",
                                   d,
                                   flags=re.DOTALL).strip()
            if len(post_data_key) > 1 and not post_data_key.startswith(
                    'WebKitForm') and post_data_key != "submit":
                data_object[post_data_key] = post_data_val

        page_name = data_object['page_name']
        page_content = data_object['page_content']

        # Backup current version just in case cuz why not
        os.rename(f"data/{page_name}.html", f"data/{page_name}.html.bak")

        write_file(f"data/{page_name}.html", page_content)
        response = '<meta http-equiv="refresh" content="0; url=/app/admin/pages"/>'

        scrape_and_write(page_name)

    ####
    ####
    elif environ['REQUEST_METHOD'] == "POST" and environ[
            'PATH_INFO'] == "/admin/products/add-edit":

        length = int(environ.get('CONTENT_LENGTH', '0'))
        post_input = environ['wsgi.input'].read(length).decode('UTF-8')

        data_object = {}
        data_array = []

        post_input_array = post_input.split('------')

        for d in post_input_array:
            post_data_key = re.sub(r'^.*name="(.*?)".*$',
                                   r"\1",
                                   d,
                                   flags=re.DOTALL).strip()
            post_data_val = re.sub(r'^.*name=".*?"(.*)$',
                                   r"\1",
                                   d,
                                   flags=re.DOTALL).strip()
            if len(post_data_key) > 1 and not post_data_key.startswith(
                    'WebKitForm'
            ) and post_data_key != "submit" and not post_data_val.startswith(
                    '-----'):
                data_object[post_data_key] = post_data_val
                data_array.append(post_data_val)

        try:
            if int(data_object['pid']) > 0:
                action = "Update"
                pid = data_object['pid']
            else:
                action = "Insert"
        except:
            action = "Insert"

        # Cleanup: Remove "pid"
        del data_object['pid']
        del data_array[0]

        # Todo: More validation
        products_form = ProductsForm(**data_object)

        # Set query based on update vs insert
        if action == "Update":
            keys_vals = ""
            for k, v in data_object.items():
                v = v.replace("'", "''")
                keys_vals += str(f"{k}='{v}', ")
            keys_vals = keys_vals.rstrip(', ')
            sql = f"UPDATE products SET {keys_vals} WHERE pid = {pid}"
            #print(sql)
            c = db.cursor()
            c.execute(sql)
        else:
            fields = "name, description, image_path_array, inventory, price, keywords_array, active"
            vals = data_array
            sql = f"INSERT INTO products ({fields}) VALUES (%s, %s, %s, %s, %s, %s, %s)"
            c = db.cursor()
            c.execute(sql, vals)
        c.close()

        # Next template needs to know the pid
        if action == "Insert":
            # Now retrieve the pid from the item we just added
            n = data_object['name']
            p = data_object['price']
            sql2 = f"SELECT pid FROM products WHERE name = '{n}' AND price = '{p}'"
            #print(sql2)
            d = db.cursor()
            d.execute(sql2)
            results = d.fetchone()
            pid = int(results[0])
            d.close()
        else:
            sql2 = "just an update"

        image_form = ImageForm()

        template = env.get_template("admin-products-image.html")
        response = template.render(product_data=data_object,
                                   image_form=image_form,
                                   sql={"sql": sql},
                                   pid={"pid": pid},
                                   sql2={"sql2": sql2})

    ####
    ####
    elif environ['REQUEST_METHOD'] == "POST":

        #print("HERE AAA")

        length = int(environ.get('CONTENT_LENGTH', '0'))
        post_input = environ['wsgi.input'].read(length).decode('UTF-8')

        #print("post_input", post_input)

        data_object = {}
        data_array = []

        post_input_array = post_input.split('------')

        #with open("stderr.log", "a") as logfile:
        #    logfile.write(str(f"++++{post_input}++++"))

        for d in post_input_array:
            post_data_key = re.sub(r'^.*name="(.*?)".*$',
                                   r"\1",
                                   d,
                                   flags=re.DOTALL).strip()
            post_data_val = re.sub(r'^.*name=".*?"(.*)$',
                                   r"\1",
                                   d,
                                   flags=re.DOTALL).strip()
            if len(post_data_key) > 1 and not post_data_key.startswith(
                    'WebKitForm'
            ) and "submit" not in post_data_key and not post_data_val.startswith(
                    '-----'):
                data_object[post_data_key] = post_data_val
                data_array.append(post_data_val)

        #print("data_object", data_object)
        #print("data_array", data_array)

        # TEMPORARILY removing series input data
        #data_object_temp = data_object
        #for k, v in data_object_temp.items():
        #    if "series" in k:
        #        del data_object[k]

        # If form passes an eid value then query
        # is an update as opposed to an insert

        try:
            if int(data_object['eid']) > 0:
                action = "Update"
                eid = data_object['eid']
            else:
                action = "Insert"
        except:
            action = "Insert"

        # Cleanup: Remove "eid"
        del data_object['eid']
        del data_array[0]

        # Cleanup: Remove "append_time"
        del data_object['append_time']
        del data_array[1]

        # For the variable-field stuff:
        price_text = data_object["price_text"]
        elimit = data_object["elimit"]

        # Todo: More validation
        events_form = EventsForm(**data_object)

        # Set query based on update vs insert
        if action == "Update":
            keys_vals = ""
            for k, v in data_object.items():
                v = v.replace("'", "''")
                keys_vals += str(f"{k}='{v}', ")
            keys_vals = keys_vals.rstrip(', ')
            sql = f"UPDATE events SET {keys_vals} WHERE eid = {eid}"

            c = db.cursor()
            c.execute(sql)
            #c.execute(sql, vals)

        else:
            # fields MUST match keys coming in via "data_array":
            fields = "edatetime, title, duration, price, elimit, location, image, description, price_text, tags, extra_data"
            #vals = str(data_array).lstrip('[').rstrip(']')
            vals = data_array
            #sql = f"INSERT INTO events ({fields}) VALUES ({vals})"
            # Number of items (%s) MUST match num of vals coming in via "data_array":
            sql = f"INSERT INTO events ({fields}) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"

            c = db.cursor()
            #c.execute(sql)
            c.execute(sql, vals)

        c.close()

        # Next template needs to know the eid
        if action == "Insert":
            # Now retrieve the eid from the item we just added
            e = data_object['edatetime']
            t = data_object['title'].replace("'", "''")
            sql2 = f"SELECT eid, price_text, elimit FROM events WHERE edatetime = '{e}' AND title = '{t}'"
            #print("____", sql2)
            d = db.cursor()
            d.execute(sql2)
            results = d.fetchone()
            #print("____results", results)
            #print("____type of results", type(results))
            eid = int(results[0])
            try:
                price_text = results[1]
            except:
                price_text = ""
            try:
                elimit = int(results[2])
            except:
                elimit = ""
            d.close()

            #
            # TESTING THE NEW VARIABLE-TIME STUFF:
            if "am" in price_text or "pm" in price_text:
                u = UpdateExtra(eid, price_text, elimit)
                u.set_via_admin()
                u.update_extra()
            #

        else:
            sql2 = "just an update"

        image_form = ImageForm()

        template = env.get_template("admin-events-image.html")
        response = template.render(event_data=data_object,
                                   image_form=image_form,
                                   sql={"sql": sql},
                                   eid={"eid": eid},
                                   sql2={"sql2": sql2})

        #scrape_and_write("calendar")
        #time.sleep(2)
        #scrape_and_write("home")

    ####
    ####
    else:
        response = "error"

    #response += f"<hr>{str(environ)}"

    db.close()

    return [response.encode()]