def __call__(self, img_id=None): # turn our class into HTML return XML( MainAppComponent.MAINAPP.format( get_posts_url=URL(self.get_posts_url, signer=self.signer), create_post_url=URL(self.create_post_url, signer=self.signer), get_about_url=URL(self.get_about_url, signer=self.signer), delete_all=URL(self.delete_all_posts_url, signer=self.signer)))
def __call__(self, id=None): """This method returns the element that can be included in the page. @param id: id of the file uploaded. This can be useful if there are multiple instances of this form on the page.""" return XML(ThumbRater.THUMBRATER.format( url=URL(self.url, id, signer=self.signer), callback_url=URL(self.callback_url, id, signer=self.signer)))
def test_url(self): request.app_name = '_default' self.assertEqual(URL('index'), '/index') request.app_name = 'app' self.assertEqual(URL('index'), '/app/index') self.assertEqual(URL('a', 'b', vars=dict(x=1), hash='y'), '/app/a/b?x=1#y')
def new_project(pid=None): # default for this in models doesn't seem to work db.project.startdate.default = ( datetime.datetime.utcnow()).strftime("%Y-%m-%d") pid = int(pid) if pid and pid.isnumeric() else None if pid: # locked is mainly to protect the unspecified project from changes islocked = db(db.project.id == pid).select().first() if islocked.locked or (islocked.proj_owner != auth.user_id and not islocked.proj_shared): flash.set("You can't edit this record", sanitize=True) redirect(URL('projectgrid')) db.project.proj_owner.default = auth.user_id form = Form(db.project, record=pid, formstyle=FormStyleBulma) if pid: proj = db(db.project.id == pid).select().first() if (not proj.proj_shared) and proj.proj_owner != auth.user_id: flash.set("Not Editable by You", sanitize=True) form.deletable = False form.readonly = True if form.accepted: session['projid'] = form.vars['id'] redirect(URL('projectgrid')) return dict(form=form)
def __call__(self, redirect_url=None): """This method returns the element that can be included in the page. The *args and **kwargs are used when subclassing, to allow for forms that are 'custom built' for some need.""" return XML(VueForm.FORM.format(url=URL(self.url, signer=self.signer), check_url=URL(self.url_check, signer=self.signer), redirect_url=redirect_url))
def edit_phone(phone_id=None): # """Note that in the above declaration, the contact_id argument must match # the <contact_id> argument of the @action.""" # # We read the contact. p = db.phone[phone_id] if p is None: redirect(URL('index')) c = db.contact[p.contact_id] user_email = auth.current_user.get('email') if c.user_email != user_email: redirect(URL('index')) # Only display form if phone exists and contact belongs to logged in user form = Form([Field('number'), Field('name')], record=dict(name=p.name, number=p.number), csrf_session=session, formstyle=FormStyleBulma) if form.accepted: # Create phone and insert into table db.phone[phone_id] = dict(number=form.vars['number'], name=form.vars['name']) db.commit() redirect(URL('phone_numbers', c.id)) return dict(contact_id=c.id, form=form)
def __call__(self): return XML( NotesHandler.NOTESHANDLER.format( get_notes=URL(self.get_notes_url, signer=self.signer), add_notes=URL(self.add_notes_url, signer=self.signer), save_notes=URL(self.save_notes_url, signer=self.signer), delete_notes=URL(self.delete_notes_url, signer=self.signer)))
def delete(contact_id=None): assert contact_id is not None contact = db.contact[contact_id] if contact is None: redirect(URL('index')) db(db.contact.id == contact_id).delete() redirect(URL('index'))
def edit_phone(contact_id=None, phone_id=None): assert contact_id is not None assert phone_id is not None # match the phone owner's id as well as the number's id row = db( (db.phone.contact_id == contact_id) & (db.phone.id == phone_id) ).select().first() if row is None: redirect(URL('edit_phones', contact_id)) contact_inf = db(db.contact.id == contact_id).select().first() if contact_inf.user_email != get_user_email(): abort(403) form = Form([Field('Phone', 'string'), Field('Kind', 'string')], record=dict(Phone=row.number, Kind=row.phone_type), deletable=False, csrf_session=session, formstyle=FormStyleBulma) form.param.sidecar.append(SPAN(" ", A("Back", _class="button", _href=URL('edit_phones', contact_id)))) if form.accepted: # insert the number for that contact number = form.vars['Phone'] phone_type = form.vars['Kind'] db((db.phone.contact_id == contact_id) & (db.phone.id == phone_id)).update( number=number, phone_type=phone_type ) redirect(URL('edit_phones', contact_id)) contact_inf = db.contact[contact_id] first_name = contact_inf.first_name last_name = contact_inf.last_name name = f"{first_name} {last_name}" return dict(form=form, name=name)
def edit_contact(person_id=None, phone_id=None): # read person p = db.person[person_id] if p is None: # nothing to edit redirect(URL('index')) # contact wasn't the created by the user if (p.user_email != auth.current_user.get('email')): redirect(URL('index')) else: row = db(db.phone.id == phone_id).select().as_list() form = Form([Field('number'), Field('kind')], record=dict(number=row[0].get('number'), kind=row[0].get('kind')), deletable=False, csrf_session=session, formstyle=FormStyleBulma) if form.accepted: db.phone.update_or_insert(((db.phone.id == phone_id)), number=form.vars.get('number'), kind=form.vars.get('kind')) redirect(URL('edit_phones', person_id)) # return variables person_row = db(db.person.id == person_id).select().as_list() first_name = person_row[0].get('first_name') last_name = person_row[0].get('last_name') return dict(form=form, first_name=first_name, last_name=last_name)
def test_url(self): request.app_name = "_default" self.assertEqual(URL("index"), "/index") request.app_name = "app" self.assertEqual(URL("index"), "/app/index") self.assertEqual(URL("a", "b", vars=dict(x=1), hash="y"), "/app/a/b?x=1#y")
def profile(): print("profile page reached") user_info = db(db.auth_user.email == get_user_email()).select().first() if not db(db.profiles.user == user_info.id).select().first(): db.profiles.insert( user=user_info.id, region=db.profiles.region.default, bio=db.profiles.bio.default, mic=db.profiles.mic.default, tiltproof=db.profiles.tiltproof.default, leader=db.profiles.leader.default, fun=db.profiles.fun.default, communicative=db.profiles.communicative.default, ) profile_info = db(db.profiles.user == user_info.id).select().first() return dict( user_info=user_info, profile_info=profile_info, change_profile_url=URL('change_profile', signer=url_signer), add_game_url=URL('add_game', signer=url_signer), load_games_url=URL('load_games', signer=url_signer), )
def render_table_pager(self): pager = DIV(**self.param.grid_class_style.get("grid-pagination")) previous_page_number = None for page_number in self.iter_pages(self.current_page_number, self.number_of_pages): pager_query_parms = dict(self.query_parms) pager_query_parms["page"] = page_number # if there is a gat add a spacer if previous_page_number and page_number - previous_page_number > 1: pager.append(SPAN("...", _style="margin:0 10px;")) is_current = self.current_page_number == page_number page_name = ("grid-pagination-button-current" if is_current else "grid-pagination-button") attrs = ({ "_hx-get": URL(self.endpoint, vars=pager_query_parms), "_hx-target": self.param.htmx_target, "_hx-swap": "innertHTML", } if self.param.htmx_target else { "_href": URL(self.endpoint, vars=pager_query_parms) }) pager.append( A( page_number, **self.param.grid_class_style.get(page_name), _role="button", **attrs, )) previous_page_number = page_number return pager
def pay(): # See https://stripe.com/docs/payments/checkout/migration#api-products # Insert non-paid order (the customer has not checked out yet). rows = db(db.shopping_cart.user_id == get_user()).select().as_list() line_items = [] for row in rows: prices = db(db.prices.ebook_id == row['ebook_id']).select().as_list() # print(prices) result = sys.maxsize for p in prices: if p['price'] < result: result = p['price'] # and we can simply assign the nice string to a field of the row! # No matter that the field did not originally exist in the database. row["price"] = result line_item = { 'quantity': int(1), 'price_data': { 'currency': 'usd', 'unit_amount': int(row["price"] * 100), # Stripe wants int. 'product_data': { 'name': row['title'], } } } line_items.append(line_item) stripe_session = stripe.checkout.Session.create( payment_method_types=['card'], line_items=line_items, mode='payment', success_url=full_url(URL('successful_payment', signer=url_signer)), cancel_url=full_url(URL('cancelled_payment', signer=url_signer))) return dict(session_id=stripe_session.id)
def add_phone(contact_id=None): # Retrieving First/Last name for header contact = db.contacts[contact_id] first_name = contact.first_name last_name = contact.last_name user_email = auth.current_user.get('email') # Contact DNE or belongs to different user if contact is None or contact.user_email != user_email: redirect(URL('index')) # Creating form to add phone number form = Form([Field('phone_number'), (Field('kind'))], csrf_session=session, formstyle=FormStyleBulma, validation=validate_phone) # Inserting phone number into phone_numbers table # Redirecting to edit_phones page if form.accepted: phone_number = form.vars['phone_number'] kind = form.vars['kind'] db.phone_numbers.insert(phone_number=phone_number, kind=kind, contact_id=contact_id) redirect(URL('edit_phones', contact_id)) return dict(form=form, first_name=first_name, last_name=last_name)
def edit_phone(contact_id=None, phone_id=None): assert contact_id is not None assert phone_id is not None c = db.contact[contact_id] p = db.phone[phone_id] print('hi edit singular phone') if p is None: # nothing found to be edited redirect(URL('edit_phones', contact_id)) form = Form( [Field('phone_numb'), Field('phone_name')], record=dict(phone_numb=p.phone_numb, phone_name=p.phone_name), deletable=False, csrf_session=session, formstyle=FormStyleBulma ) if form.accepted: db(db.phone.id == phone_id).update( phone_numb=form.vars['phone_numb'], phone_name=form.vars['phone_name'], contact_id=contact_id, ) redirect(URL('edit_phones', contact_id)) return dict( form = form, contact = c )
def song(song_id=None): email = get_user_email() name = auth.current_user.get('first_name') + " " + auth.current_user.get( "last_name") user_id = get_user_id() assert song_id is not None #assert song_name is not None #n = song_name.replace('_',' ') #song = db(db.song.name.like(n, case_sensitive = False)).select().first() #if song is None: #redirect(URL('index')) song = db(db.song.id == song_id).select().first() band = db(db.band.id == song.band_id).select().first() album = db(db.album.id == song.album_id).select().first() return dict( song=song, album=album, band=band, load_posts_url=URL('load_posts', signer=url_signer), add_post_url=URL('add_post', signer=url_signer), delete_post_url=URL('delete_post', signer=url_signer), vote_post_url=URL('vote_post', signer=url_signer), user_email=email, username=name, user_id=user_id, )
def projectgrid(path=None): GRID_DEFAULTS = dict(rows_per_page=15, include_action_button_text=True, search_button_text='Filter', formstyle=FormStyleBulma, grid_class_style=GridClassStyleBulma) fields = [ db.project.proj_name, db.project.proj_status, db.project.description, db.project.proj_shared, db.project.priority ] orderby = [~db.project.priority] search_queries = [[ 'Search by Name', lambda value: db.project.name == value ]] # search = GridSearch(search_queries, queries) grid = Grid( path, db.project, fields=fields, headings=['Name', 'Status', 'Description', 'Shared', 'Priority'], orderby=orderby, search_queries=search_queries, create=URL('new_project/'), details=URL('view_project/'), editable=URL('new_project/'), deletable=True, **GRID_DEFAULTS) return dict(grid=grid)
def locationgrid(path=None): GRID_DEFAULTS = dict(rows_per_page=15, include_action_button_text=True, search_button_text='Filter', formstyle=FormStyleBulma, grid_class_style=GridClassStyleBulma) fields = [ db.locn.location_name, db.locn.address1, db.locn.address2, db.locn.addrcode, db.locn.addrurl, db.locn.country, db.locn.description ] orderby = [db.locn.location_name] search_queries = [[ 'Search by Name', lambda value: db.locn.location_name == value ]] # search = GridSearch(search_queries, queries) grid = Grid(path, db.locn, fields=fields, headings=[ 'Name', 'Address1', 'Address2', 'Addrcode', 'Addrurl', 'Country', 'Description' ], orderby=orderby, search_queries=search_queries, create=URL('new_location/0'), details=True, editable=URL('new_location/'), deletable=URL('new_project/delete/'), **GRID_DEFAULTS) return dict(grid=grid)
def index(): return dict( # This is an example of a signed URL for the callback. # See the index.html template for how this is passed to the javascript. callback_url=URL('callback', signer=url_signer), add_note_url=URL('add_note', signer=url_signer), get_notes_url=URL('get_notes', signer=url_signer))
def search_form(): user = auth.get_user() form = Form([Field('search_string')], formstyle=FormStyleBulma) if form.accepted: print(URL('search', vars=form.vars)) redirect(URL('search', vars=form.vars)) return dict(form=form)
def edit_contact(person_id=None): # read person p = db.person[person_id] if p is None: # nothing to edit redirect(URL('index')) # contact wasn't the created by the user if (p.user_email != auth.current_user.get('email')): redirect(URL('index')) else: row = db(db.person.id == person_id).select().as_list() form = Form([ Field('first_name', requires=IS_NOT_EMPTY()), Field('last_name', requires=IS_NOT_EMPTY()) ], record=dict(first_name=row[0].get('first_name'), last_name=row[0].get('last_name')), deletable=False, csrf_session=session, formstyle=FormStyleBulma) if form.accepted: db.person.update_or_insert(((db.person.id == person_id)), first_name=form.vars.get('first_name'), last_name=form.vars.get('last_name')) redirect(URL('index')) return dict(form=form)
def stop_smpp_connector(cid): if not cid: flash.set('You need to select a connector to stop') redirect(URL('manage_smpp_connectors')) con = cid t = jasmin.connector(['stop', con]) flash.set('Stopped connector %s' % cid) redirect(URL('manage_smpp_connectors'))
def start_smpp_connector(cid): if not cid: flash.set('You need to select a connector to start') redirect(URL('manage_smpp_connectors')) con = cid t = jasmin.connector(['start', con]) flash.set("Started Connector %s" % con) redirect(URL('manage_smpp_connectors'))
def lobbies(): print("lobby page reached") return dict( # URLS used for callbacks and HTTP requests add_lobby_url=URL('add_lobby', signer=url_signer), load_lobbies_url=URL('load_lobbies', signer=url_signer), )
def delete_contact(person_id=None, phone_id=None): p = db.person[person_id] if p is None: redirect(URL('index')) # else delete person if has access if (p.user_email == auth.current_user.get('email')): db(db.phone.id == phone_id).delete() redirect(URL('edit_phones', person_id))
def __call__(self, id=None, redirect_url=None): """This method returns the element that can be included in the page. @param id: if an id is specified, the form is an update form for the specified record id. @param redirect_url: URL to which to redirect after success.""" return XML(VueForm.FORM.format(url=URL(self.url, id, signer=self.signer), check_url=URL(self.url_check, id, signer=self.signer), redirect_url=redirect_url))
def __call__(self, id=None): return XML( ThumbRater.THUMBRATER.format(url=URL(self.url, id, signer=self.signer), callback_url=URL(self.callback_url, id, signer=self.signer)))
def index(): return dict( # COMPLETE: return here any signed URLs you need. # test pycharm connection load_posts_url=URL('load_posts', signer=url_signer), add_post_url=URL('add_post', signer=url_signer), delete_post_url=URL('delete_post', signer=url_signer), )
def render_table_header(self): up = I(**self.param.grid_class_style.get("grid-sorter-icon-up")) dw = I(**self.param.grid_class_style.get("grid-sorter-icon-down")) columns = [] sort_order = request.query.get("orderby", "") for index, field in enumerate(self.param.fields): if field.readable and (field.type != "id" or self.param.show_id): key = "%s.%s" % (field.tablename, field.name) heading = ( self.param.headings[index] if index < len(self.param.headings) else field.label ) heading = title(heading) # add the sort order query parm sort_query_parms = dict(self.query_parms) attrs = {} if isinstance(field, FieldVirtual): col = DIV(heading) elif key == sort_order: sort_query_parms["orderby"] = "~" + key url = URL(self.endpoint, vars=sort_query_parms) attrs = self.attributes_plugin.link(url=url) col = A(heading, up, **attrs) else: sort_query_parms["orderby"] = key url = URL(self.endpoint, vars=sort_query_parms) attrs = self.attributes_plugin.link(url=url) col = A(heading, dw if "~" + key == sort_order else "", **attrs) columns.append((key, col)) thead = THEAD(_class=self.param.grid_class_style.classes.get("grid-thead", "")) for key, col in columns: col_class = " grid-col-%s" % key thead.append( TH( col, _class=self.param.grid_class_style.classes.get("grid-th", "") + col_class, _style=self.param.grid_class_style.styles.get("grid-th"), ) ) if ( self.param.details or self.param.editable or self.param.deletable or self.param.pre_action_buttons or self.param.post_action_buttons ): thead.append( TH("", **self.param.grid_class_style.get("grid-th-action-button")) ) return thead