def simple_html(status=404): if not useful.is_header_done(): print 'Content-Type: text/html\n\n' print 'Status:', status, httplib.responses.get(status, '') #print '<!--\n' + str(os.environ) + '-->' useful.header_done() useful.write_comment()
def call_main(page_id='cli', form_key='', defval='', args='', dbedit=None, switches='', options=''): pif = None try: import pifile switch, filelist = get_command_line(switches, options) for f in filelist: if f.startswith('page_id='): page_id = f[8:] if isinstance(page_id, pifile.PageInfoFile): pif = page_id else: pif = get_page_info(page_id, form_key, defval, args, dbedit) pif.switch, pif.filelist = switch, filelist ret = main_fn(pif) useful.write_comment() if ret: print ret except SystemExit: pass except useful.SimpleError as e: print '***', e.value
def call_main(switches="", options="", long_options={}, version="", short_help="", long_help="", envar=None, noerror=False, defaults={}, doglob=False): try: switch, filelist = get_command_line(switches=switches, options=options, long_options=long_options, version=version, short_help=short_help, long_help=long_help, envar=envar, noerror=noerror, defaults=defaults, doglob=doglob) ret = main_fn(switch, filelist) useful.write_comment() if ret: print ret except SystemExit: pass
def simple_html(status=404): if not useful.is_header_done(): print 'Content-Type: text/html\n\n' print 'Status:', status, httplib.responses.get(status, '') #print '<!--\n' + str(os.environ) + '-->' useful.header_done() useful.write_comment()
def do_page(pif): pif.render.print_html() pif.render.hierarchy_append('/', 'Home') pif.render.hierarchy_append('/database.php', 'Database') pif.render.hierarchy_append('packs.cgi', 'Multi-Model Packs') #pif.form.set_val('id', pif.form.get_list('id')[0]) # with no id this blows # if isinstance(pif.form.get('id'), list): # pif.form['id'] = pif.form.get_str('id')[0] if pif.form.has('id'): pif.form.set_val('id', pif.form.get_list('id')[0]) # with no id this blows pid = useful.clean_id(pif.form.get_str('id')) return do_single_pack(pif, pid) elif pif.form.has('page'): return make_pack_list( pif, verbose=pif.is_allowed('m') and pif.form.get_int('verbose'), **pif.form.get_dict(['sec', 'year', 'region', 'lid', 'material'])) elif pif.form.has('sec'): useful.write_comment(pif.form) sections = pif.dbh.fetch_sections_by_page_type('packs', pif.form.get_str('sec')) if not sections: return make_page_list(pif) pif.page_id = sections[0]['page_info.id'] return make_pack_list( pif, verbose=pif.is_allowed('m') and pif.form.get_int('verbose'), **pif.form.get_dict(['sec', 'year', 'region', 'lid', 'material'])) else: return make_page_list(pif)
def handle_exception(pif, e, header_done=False, write_traceback=True, status_code='unset'): log = pif.log if pif and pif.log else logger.Logger() log.exc.error('{} {}'.format(os.environ.get('REMOTE_ADDR', '127.0.0.1'), os.environ.get('REQUEST_URI', 'unknown'))) str_tb = write_traceback_file(pif, e) if write_traceback else '' log_page_call(pif, status_code=status_code) if not pif or not pif.render or not pif.dbh: if not header_done: simple_html() if str_tb: print('<!--\n' + str_tb + '-->') final_exit() pif.dbh.set_health(pif.page_id) if not useful.is_header_done() and not header_done: simple_html() useful.header_done() useful.write_comment() while pif.render.table_count > 0: print(pif.render.format_table_end()) if not pif.is_allowed('a'): print('<!--\n' + str_tb + '-->') final_exit()
def call_main(switches="", options="", long_options={}, version="", short_help="", long_help="", envar=None, noerror=False, defaults={}, doglob=False): try: switch, filelist = get_command_line(switches=switches, options=options, long_options=long_options, version=version, short_help=short_help, long_help=long_help, envar=envar, noerror=noerror, defaults=defaults, doglob=doglob) ret = main_fn(switch, filelist) useful.write_comment() if ret: print ret except SystemExit: pass
def __init__(self, fname): if fname.startswith('/'): fname = fname[1:] self.filename = fname useful.write_comment('trying to open', fname) self.set_globals() try: self.handle = open(fname) except IOError: raise useful.SimpleError("""I'm sorry, that page was not found. Please use your "BACK" button or try something else.""") self.srcstat = os.fstat(self.handle.fileno()) self.ignoreoverride = False self.dats = {} self.parse()
def __init__(self, fname): if fname.startswith('/'): fname = fname[1:] self.filename = fname useful.write_comment('trying to open', fname) self.set_globals() try: self.handle = open(fname) except IOError: raise useful.SimpleError( """I'm sorry, that page was not found. Please use your "BACK" button or try something else.""") self.srcstat = os.fstat(self.handle.fileno()) self.ignoreoverride = False self.dats = {} self.parse()
def code2_model(pif): mod_id = pif.form.get_str('mod_id') cat_id = pif.form.get_str('cat') pif.render.print_html() pif.render.hierarchy_append('/', 'Home') pif.render.hierarchy_append('/database.php', 'Database') pif.render.hierarchy_append('/cgi-bin/code2.cgi', 'Code 2 Models') pif.render.set_button_comment(pif) mod = pif.dbh.modify_man_item(pif.dbh.fetch_casting(mod_id)) img = pif.render.format_image_required(mod_id, largest=mbdata.IMG_SIZ_MEDIUM, pdir=config.IMG_DIR_MAN) header = '<center>%s<br><b>%s: %s</b></center><p>' % (img, mod['id'], mod['name']) lsec = pif.dbh.fetch_section(page_id=pif.page_id, category=cat_id) if not lsec: raise useful.SimpleError('No models found.') lsec = lsec.todict() pif.render.hierarchy_append('/cgi-bin/code2.cgi?section=%s' % lsec['id'], lsec['name']) pif.render.hierarchy_append( '/cgi-bin/code2.cgi?mod_id=%s&cat=%s' % (mod['id'], cat_id), mod['id']) lsec['range'] = [{'entry': []}] mvars = pif.dbh.fetch_variation_by_select(mod_id, pif.page_id, '', category=cat_id) for var in mvars: useful.write_comment(var) entry = { 'text': models.add_model_var_pic_link(pif, pif.dbh.depref('v', var)) } lsec['range'][0]['entry'].append(entry) llineup = { 'section': [lsec], 'header': header, } pif.render.format_matrix_for_template(llineup) return pif.render.format_template('simplematrix.html', llineup=llineup)
def execute(self, query, args=None, logargs=True, verbose=None, tag=''): if verbose is None: verbose = self.verbose if tag: query = query.split(None, 1) + [''] query = query[0] + ' /* ' + tag + ' */ ' + query[1] if verbose: useful.write_comment('DB.execute q : "%s"' % query) if args: if logargs: useful.write_comment(' args :', args) else: useful.write_comment(' args :', len(args), 'redacted') sys.stdout.flush() if self.logger: # log_name = os.path.join(config.LOG_ROOT, config.ENV + datetime.datetime.now().strftime('.dbq%Y%m.log')) # try: # open(log_name, 'a').write('%s %s%s %s %s\n' % # (datetime.datetime.now().strftime('%Y%m%d.%H%M%S'), '/*mock*/' if self.nowrites else '', # self.user_id, os.environ.get('REMOTE_ADDR', ''), query)) # except: # pass self.logger.info('q %s%s %s' % ('/*mock*/ ' if self.nowrites else '', os.environ.get('REMOTE_ADDR', ''), query)) if args: if logargs: self.logger.info(' args :', args) else: self.logger.info(' args :', len(args), 'redacted') cu = self.db.cursor() try: nrows = cu.execute(query, args) except: self.logger.info('x %s%s %s' % ('/*mock*/ ' if self.nowrites else '', os.environ.get('REMOTE_ADDR', ''), traceback.format_exc(0))) if verbose: traceback.print_exc() return ([], cu.description, -1) resp = cu.fetchall() if verbose: useful.write_comment("DB.execute a :", nrows, cu.lastrowid, resp) sys.stdout.flush() self.lastrowid = cu.lastrowid self.lastdescription = cu.description self.lastresp = resp cu.close() self.logger.info('a %s%s %s' % ('/*mock*/ ' if self.nowrites else '', os.environ.get('REMOTE_ADDR', ''), "%s rows %s id" % (len(resp), self.lastrowid))) return (resp, self.lastdescription, self.lastrowid)
def format_entry(pif, ent): dictFlag = { '': ('o', pif.render.format_image_art('wheel.gif', also={'class': 'dlm'})), 'Reciprocal': ('Reciprocal', '<i class="fas fa-refresh dlm"></i>'), 'PayPal': ('Accepts PayPal', '<i class="fab fa-paypal dlm"></i>'), } is_large = ent['flags'] & config.FLAG_LINK_LINE_FORMAT_LARGE url = '' if ent['flags'] & config.FLAG_LINK_LINE_DISABLED else ent['url'] tag = ent['name'] dlms = [] if ent['country']: dlms.append(ent['country']) cmt = ent['description'] if ent['flags'] & config.FLAG_LINK_LINE_RECIPROCAL: dlms.append('Reciprocal') if ent['flags'] & config.FLAG_LINK_LINE_PAYPAL: dlms.append('PayPal') ostr = pif.render.format_link(url, tag) + ' ' if not dlms and not cmt: pass elif not dlms: # add name if not is_large: ostr += format_delimiter(pif, dictFlag['']) else: # also = {'class': 'dlm'} for dlm in dlms: flag = pif.render.show_flag(dlm) if flag: ostr += useful.img_src(flag[1], also={'class': 'dlm'}) elif dlm in dictFlag: ostr += format_delimiter(pif, dictFlag[dlm]) else: useful.write_comment('tlinks.format_entry: dlm {} not found {}'.format(dlm, dictFlag)) # if cmt and is_large: # ostr += '<br>' + '<br>'.join(cmt.split('|')) # else: # ostr += cmt return ostr, cmt.split('|')
def call_main(page_id='cli', form_key='', defval='', args='', dbedit=None, switches='', options=''): pif = None try: import pifile switch, filelist = get_command_line(switches, options) for f in filelist: if f.startswith('page_id='): page_id = f[8:] if isinstance(page_id, pifile.PageInfoFile): pif = page_id else: pif = get_page_info(page_id, form_key, defval, args, dbedit) pif.switch, pif.filelist = switch, filelist ret = main_fn(pif) useful.write_comment() if ret: print ret except SystemExit: pass except useful.SimpleError as e: print '***', e.value
def handle_exception(pif, header_done=False, write_traceback=True): str_tb = '' if write_traceback: str_tb = write_traceback_file(pif) if not pif or not pif.render or not pif.dbh: if not header_done: simple_html() if str_tb: print '<!--\n' + str_tb + '-->' final_exit() pif.dbh.set_health(pif.page_id) import useful if not useful.is_header_done() and not header_done: simple_html() useful.header_done() useful.write_comment() while pif.render.table_count > 0: print pif.render.format_table_end() if not pif.is_allowed('a'): print '<!--\n' + str_tb + '-->' final_exit()
def handle_exception(pif, header_done=False, write_traceback=True): str_tb = '' if write_traceback: str_tb = write_traceback_file(pif) if not pif or not pif.render or not pif.dbh: if not header_done: simple_html() if str_tb: print '<!--\n' + str_tb + '-->' final_exit() pif.dbh.set_health(pif.page_id) import useful if not useful.is_header_done() and not header_done: simple_html() useful.header_done() useful.write_comment() while pif.render.table_count > 0: print pif.render.format_table_end() if not pif.is_allowed('a'): print '<!--\n' + str_tb + '-->' final_exit()
def execute(self, query, args=None, logargs=True, verbose=None, tag=''): if verbose is None: verbose = self.verbose if tag: query = query.split(None, 1) + [''] query = query[0] + ' /* ' + tag + ' */ ' + query[1] if verbose: useful.write_comment('DB.execute q : "{}"'.format(query)) if args: if logargs: useful.write_comment(' args :', args) else: useful.write_comment(' args :', len(args), 'redacted') sys.stdout.flush() if self.logger: self.logger.info('q {}{} {}'.format( '/*mock*/ ' if self.nowrites else '', os.environ.get('REMOTE_ADDR', ''), query)) if args: if logargs: self.logger.info(' args : %s', args) else: self.logger.info(' args : %d %s', len(args), 'redacted') cu = self.db.cursor() try: nrows = cu.execute(query, args) except Exception: self.logger.info( 'x %s%s %s' % ('/*mock*/ ' if self.nowrites else '', os.environ.get('REMOTE_ADDR', ''), traceback.format_exc(0))) if verbose: traceback.print_exc() return ([], cu.description, -1) resp = cu.fetchall() if verbose: useful.write_comment("DB.execute a :", nrows, cu.lastrowid, resp) sys.stdout.flush() self.lastrowid = cu.lastrowid self.lastdescription = cu.description self.lastresp = resp cu.close() self.logger.info('a %s%s %s' % ('/*mock*/ ' if self.nowrites else '', os.environ.get('REMOTE_ADDR', ''), "%s rows %s id" % (len(resp), self.lastrowid))) return (resp, self.lastdescription, self.lastrowid)
def reduce_variations(pif, mod_id, vars): '''Reduce all relevant vars to a list. Each entry has 0) list of var ids; 1) a picture; 2) a description.''' vard = {} for var in vars: if var['v.var']: vtd = var['v.text_description'] vard.setdefault(vtd, [list(), list()]) #eek #vard[vtd][0].append(pif.render.format_link('vars.cgi?mod=%s&var=%s' % (mod_id, var['v.var']), var['v.var'])) vard[vtd][0].append(var['v.var']) vard[vtd][1].append( var['v.picture_id'] if var['v.picture_id'] else var['v.var']) useful.write_comment('single.reduce_variations', vars, vard) return sorted([[ sorted(vard[vtd][0]), pif.render.find_alt_image_path(pif.render.find_image_path( mod_id, nobase=True, vars=vard[vtd][1], prefix=mbdata.IMG_SIZ_SMALL, pdir=config.IMG_DIR_MAN), largest=mbdata.IMG_SIZ_SMALL, required=True), vtd ] for vtd in vard])
def execute(self, query, args=None, verbose=None, tag=''): if verbose is None: verbose = self.verbose if tag: query = query.split(None, 1) + [''] query = query[0] + ' /* ' + tag + ' */ ' + query[1] if verbose: useful.write_comment('DB.execute q : "%s"' % query) if args: useful.write_comment(' args :', args) sys.stdout.flush() if self.logger: # log_name = os.path.join(config.LOG_ROOT, config.ENV + datetime.datetime.now().strftime('.dbq%Y%m.log')) # try: # open(log_name, 'a').write('%s %s%s %s %s\n' % # (datetime.datetime.now().strftime('%Y%m%d.%H%M%S'), '/*mock*/' if self.nowrites else '', # self.user_id, os.environ.get('REMOTE_ADDR', ''), query)) # except: # pass self.logger.info('q %s%s %s' % ('/*mock*/ ' if self.nowrites else '', os.environ.get('REMOTE_ADDR', ''), query)) cu = self.db.cursor() if tag == 'UpdateLineupModel': useful.write_message(query, '<br>') try: if args: nrows = cu.execute(query, args) else: nrows = cu.execute(query) except: self.logger.info('x %s%s %s' % ('/*mock*/ ' if self.nowrites else '', os.environ.get('REMOTE_ADDR', ''), traceback.format_exc(0))) if verbose: traceback.print_exc() return ([], cu.description, -1) resp = cu.fetchall() if verbose: useful.write_comment("DB.execute a :", nrows, cu.lastrowid, resp) sys.stdout.flush() self.lastrowid = cu.lastrowid self.lastdescription = cu.description self.lastresp = resp cu.close() self.logger.info('a %s%s %s' % ('/*mock*/ ' if self.nowrites else '', os.environ.get('REMOTE_ADDR', ''), "%s rows %s id" % (len(resp), self.lastrowid))) return (resp, self.lastdescription, self.lastrowid)
def call_main(page_id, form_key='', defval='', args='', dbedit=None): #useful.write_comment('PID', os.getpid()) pif = None try: import pifile if isinstance(page_id, pifile.PageInfoFile): pif = page_id else: pif = get_page_info(page_id, form_key, defval, args, dbedit) if '/etc/passwd' in os.environ.get( 'QUERY_STRING', '') or '%2fetc%2fpasswd' in os.environ.get( 'QUERY_STRING', '').lower(): raise useful.Redirect('http://www.nsa.gov/') except SystemExit: pass except useful.SimpleError as e: simple_html(status=e.status) print useful.render_template('error.html', error=[e.value], page={'tail': ''}) if pif: pif.log.debug.error('SimpleError: ' + str(e) + ' - ' + '''%s''' % os.environ.get('REQUEST_URI', '')) handle_exception(pif, True, False) return except MySQLdb.OperationalError: simple_html() print 'The database is currently down, and thus, this page is unable to be shown.<p>' str_tb = write_traceback_file(pif) handle_exception(pif, True) return except useful.Redirect as e: if not useful.is_header_done(): pif.render.print_html() print pif.render.format_template('forward.html', url=e.value, delay=e.delay) return except: handle_exception(pif) return try: ret = main_fn(pif) if not useful.is_header_done(): pif.render.print_html() useful.write_comment() if ret and not pif.unittest: print ret except SystemExit: pass except useful.SimpleError as e: if not useful.is_header_done(): pif.render.print_html(status=e.status) print pif.render.format_template('error.html', error=[e.value]) except useful.Redirect as e: if not useful.is_header_done(): pif.render.print_html() print pif.render.format_template('forward.html', url=e.value, delay=e.delay) except useful.DelayedRedirect as e: if not useful.is_header_done(): pif.render.print_html() print pif.render.format_template('forward.html', url=e.value, delay=e.delay) except MySQLdb.OperationalError: if not useful.is_header_done(): pif.render.print_html() print 'The database is currently done, and thus, this page is unable to be shown.<p>' str_tb = write_traceback_file(pif) except: handle_exception(pif) raise useful.header_done(True) useful.write_comment()
def show_single(pif): model = pif.dbh.fetch_casting(pif.form.get_id('id'), extras=True, verbose=True) if not model: raise useful.SimpleError("That ID wasn't found.") pif.render.print_html(status=404 if not model else 200) useful.write_comment('model', model) pic = pif.form.get_str('pic') pdir = pif.form.get_str('dir') if pdir.startswith('./'): pdir = pdir[2:] if not pdir.startswith('pic/') or '/' in pic: pdir = pic = '' ref = pif.form.get_id('ref') sub = pif.form.get_str('sub') reg = sub if sub else pic[4] if ( ref.startswith('year') and pic and len(pic) > 4 and pic[:4].isdigit()) else '' mod_id = model['id'] pif.render.hierarchy_append('/', 'Home') pif.render.hierarchy_append('/database.php', 'Database') pif.render.hierarchy_append('/cgi-bin/single.cgi', 'By ID') pif.render.hierarchy_append('/cgi-bin/single.cgi?id=%s' % mod_id, mod_id) pif.render.comment('id=', mod_id, 'man=', model) raw_variations = variations = [] if ref: sub = mbdata.get_region_tree(reg) + [''] raw_variations = pif.dbh.fetch_variation_by_select(mod_id, ref, sub) variations = reduce_variations(pif, mod_id, raw_variations) # years 1971 to 1981 needs to cleave W to U and R appearances = list() for appear in pif.dbh.depref('lineup_model', pif.dbh.fetch_casting_lineups(mod_id)): if (appear.get('region', '') == 'W' and int(appear.get('year', 0)) >= 1971 and int(appear.get('year', 0)) <= 1981): nappear = copy.deepcopy(appear) nappear['region'] = 'U' appear['region'] = 'R' appearances.append(nappear) appearances.append(appear) lm_pic_id = '' prod_title = '' if ref.startswith('year.'): for appear in appearances: if appear.get('page_id', '-') == ref and (appear.get( 'region', '-') in sub or sub == ['']): prod_title = appear['name'] lm_pic_id = appear['picture_id'] break if pif.form.has('useprev'): # pragma: no cover pic = use_previous_product_pic(pif, pif.form.get_int('useprev'), appear) appearances.sort(key=lambda x: x['year']) aliases = [x['alias.id'] for x in pif.dbh.fetch_aliases(mod_id, 'mack')] matrixes = pif.dbh.fetch_matrix_appearances(mod_id) matrixes.sort(key=lambda x: x['page_info.description'] + x['section.name']) for appear in matrixes: appear['title'] = [appear['section.name'], appear['page_info.description']] if appear['page_info.flags'] & 2 \ else [appear['page_info.title'], appear['page_info.description'], appear['section.name']] sections_recs = pif.dbh.fetch_sections(where="page_id like 'year.%'") sections = {} for section in sections_recs: if section['columns'] and not section['display_order']: sections.setdefault(section['page_id'][5:], []) sections[section['page_id'][5:]].append(section) boxstyles = pif.dbh.fetch_box_type_by_mod(model['id']) pif.render.title = '%(casting_type)s %(id)s: %(name)s' % model product_img = pif.render.format_image_sized(pic, pdir=pdir, largest=mbdata.IMG_SIZ_MEDIUM) product_img_credit = pif.dbh.fetch_photo_credit(pdir, pic, verbose=True) product_img_credit = product_img_credit[ 'photographer.name'] if product_img_credit else '' if product_img and pif.is_allowed('a'): # pragma: no cover img = img_re.search(product_img).group('u') url = 'imawidget.cgi?d=%s&f=%s' % tuple(img[3:].rsplit('/', 1)) product_img = pif.render.format_link(url, product_img) vscounts = pif.dbh.fetch_variation_select_counts(mod_id) model['imgid'] = [model['id']] descs = [] for s in model['descs']: if s.startswith('same as '): model['imgid'].append(s[8:]) if s in mbdata.arts: descs.append(pif.render.format_image_art(mbdata.arts[s])) elif s: descs.append("<i>%s</i>" % s) model['descs'] = descs model['img'] = pif.render.format_image_required( model['imgid'], made=model['made'], pdir=config.IMG_DIR_MAN, largest=mbdata.IMG_SIZ_MEDIUM if product_img else mbdata.IMG_SIZ_LARGE) model_img_credit = pif.dbh.fetch_photo_credit('.' + config.IMG_DIR_MAN, model['imgid'][0], verbose=True) model['credit'] = model_img_credit[ 'photographer.name'] if model_img_credit else '' if model['country']: model['country_flag'] = pif.render.format_image_flag(model['country']) model['country_name'] = mflags.FlagList()[model['country']] def make_make_link(make, name): if not make: return '' if not name: name = 'unlicensed' pic = pif.render.fmt_img(make, prefix='u', pdir=config.IMG_DIR_MAKE) if pic: name = pic + '<br>' + name return pif.render.format_link("makes.cgi?make=" + make, name) model['make_name'] = make_make_link(model.get('make', ''), model.get('vehicle_make.name', '')) def make_make(make): return { 'image': pif.render.fmt_img(make['vehicle_make.id'], prefix='u', pdir=config.IMG_DIR_MAKE), 'id': make['vehicle_make.id'], 'name': make['vehicle_make.name'], 'company_name': make['vehicle_make.company_name'], 'flags': make['vehicle_make.flags'] | make['casting_make.flags'], 'link': 'makes.cgi?make=%s' % make['vehicle_make.id'], } model['makes'] = [ make_make(x) for x in pif.dbh.fetch_casting_makes(mod_id) ] adds = [make_boxes(pif, mod_id, boxstyles, aliases)] if boxstyles else [] adds += models.make_adds(pif, mod_id) plants = make_plants(pif, mod_id, pif.dbh.fetch_variation_plant_counts(mod_id)) # ------- render ------------------------------------ pif.render.set_button_comment( pif, 'id=%s&pic=%s&dir=%s&ref=%s' % (mod_id, pic, pdir, ref)) context = { 'title': '%s %s: %s' % (mbdata.model_types[model['model_type']], mod_id, model['name']), 'note': '', 'type_id': '', 'icon_id': mod_id if os.path.exists( useful.relpath('.', config.IMG_DIR_ICON, 'i_' + mod_id.lower() + '.gif')) else '', 'vehicle_type': [mbdata.model_icons.get(x) for x in model['vehicle_type']], 'rowspan': '4', 'left_bar_content': show_left_bar_content(pif, model, ref, pic, pdir, lm_pic_id, raw_variations), 'model': model, 'variations': variations, 'prod_title': prod_title, 'product_image': product_img, 'product_img_credit': product_img_credit, 'mack_nums': get_mack_numbers(pif, mod_id, model['model_type'], aliases), 'product_pic': pic, 'appearances': show_lineup_appearances(pif, appearances), 'matrixes': show_series_appearances(pif, matrixes), 'code2s': show_code2_appearances(pif, mod_id, vscounts), 'packs': show_pack_appearances( pif, sorted(pif.dbh.fetch_pack_model_appearances(mod_id), key=lambda x: x['base_id.first_year'])), 'show_comparison_link': pif.dbh.fetch_casting_related_exists(mod_id, model['model_type'].lower()), 'external_links': show_external_links(pif, pif.dbh.fetch_links_single('single.' + mod_id)), 'relateds': make_relateds(pif, mod_id), 'compares': make_compares(pif, mod_id), 'adds_box': models.show_adds(pif, mod_id), 'adds': adds, 'plants': plants, # 'group': pif.render.find_image_path(mod_id, prefix='g', pdir=config.IMG_DIR_ADD) } return pif.render.format_template('single.html', **context)
def show_lineup_appearances(pif, appearances): if not appearances: return {} useful.write_comment(str(appearances)) # lineup appearances yd = {} rs = set() for appear in appearances: yd.setdefault(appear['year'], dict()) yd[appear['year']].setdefault(appear['region'][0], list()) yd[appear['year']][appear['region'][0]].append(appear) rs.add(appear['region'][0]) rl = filter(lambda x: x in rs, mbdata.regionlist) entries = [] if not yd: return {} def show_lineup(appear): return 'lineup.cgi?year=%(year)s®ion=%(region)s<y=all#%(number)s' % appear if 'X' in rs: # not implemented yet # pragma: no cover #return {} columns = ['', 'W'] for yr in sorted(yd.keys()): if yd[yr].get('X'): appear = yd[yr]['X'][0] entry = { '': '<b>%s</b>' % str(yr), 'W': '<a href="lineup.cgi?year=%s®ion=U<y=all#X%s">%s</a>' % (appear['year'], appear['number'], 'X') } entries.append(entry) else: columns = [''] + rl for yr in sorted(yd.keys()): entry = {'': '<b>%s</b>' % str(yr)} for reg in rl: entry[reg] = ', '.join([ pif.render.format_link(show_lineup(appear), str(appear['number'])) for appear in yd[yr][reg] ]) if yd[yr].get(reg) else ' ' entries.append(entry) llineup = { 'id': 'lappear', 'name': '', 'section': [{ 'id': 'la', 'name': '', 'columns': columns, 'headers': mbdata.regions, 'range': [{ 'entry': entries }], }], } return llineup
def debug(self, *arg, **kwargs): if self.verbose: useful.write_comment(*arg, **kwargs)
def makes_main(pif): useful.write_comment('privs', pif.privs) make_q = pif.dbh.depref( 'vehicle_make', pif.dbh.fetch_vehicle_makes( where='' if pif.is_allowed('a') else 'not (flags & %s)' % pif.dbh.FLAG_ITEM_HIDDEN)) makelist = [(x['id'], x['name']) for x in make_q] makedict = {x['id']: x for x in make_q} makedict['unl'] = { 'id': 'unl', 'name': 'Unlicensed', 'company_name': 'Unlicensed', 'flags': 0 } makedict['unk'] = { 'id': 'unk', 'name': 'unknown', 'company_name': 'Make unknown', 'flags': 0 } footer = '' make = pif.form.get_str('make', '') makes = [make] pif.render.hierarchy_append('/', 'Home') pif.render.hierarchy_append('/database.php', 'Database') pif.render.hierarchy_append('/cgi-bin/makes.cgi', 'Models by Make') if make: pif.render.title = 'Models by Make: ' + makedict.get(make, {}).get( 'name', '') if make == 'text': pif.render.hierarchy_append(pif.request_uri, 'Search') elif make: pif.render.hierarchy_append(pif.request_uri, makedict.get(make, {'name': make})['name']) pif.render.print_html() if make == 'text': makename = pif.form.get_str('text') if makename: makes = [] for m in makelist: if m[1].lower().startswith(makename.lower()): makes.append(m[0]) if not makes: makes = ['unk'] else: make = '' elif make and make in makedict: links = pif.dbh.fetch_link_lines(page_id='makes', section=make) footer = '<ul>' + '\n'.join([ '<li>' + pif.render.format_link(x['link_line.url'], x['link_line.name']) for x in links ]) + '</ul>\n' if pif.is_allowed('a'): # pragma: no cover footer += pif.render.format_button( 'ADD LINK', 'editor.cgi?table=link_line&add=1&page_id=makes§ion_id=%s&link_type=l' % make) if make: llineup = show_makes(pif, makedict, makes) else: makes = ['unk', 'unl'] + sorted([x[0] for x in makelist], key=lambda x: makedict[x]['name']) llineup = makes_form(pif, makedict, makes) pif.render.set_button_comment( pif, 'make=%s&text=%s' % (pif.form.get_str('make', ''), pif.form.get_str('text', ''))) if pif.is_allowed('a'): # pragma: no cover if make and make != 'text': footer += pif.render.format_button('edit', link=pif.dbh.get_editor_link( 'vehicle_make', {'id': make})) llineup['footer'] = footer pif.render.format_matrix_for_template(llineup) return pif.render.format_template('simplematrix.html', llineup=llineup)
def make_pack_list(pif, sec='', year='', region='', lid='', material='', verbose=False): # need to adapt this for id-var pif.render.set_button_comment(pif) years = set() regions = set() has_note = False title = pif.form.search('title') sections = pif.dbh.fetch_sections({'page_id': pif.page_id}) sec_id = sections[0]['id'] packs = pif.dbh.depref(['base_id', 'pack'], pif.dbh.fetch_packs(page_id=pif.page_id)) cols = ['pic', 'name', 'year', 'product_code'] heads = ['', 'Name', 'Year', 'Product Code'] if verbose: cols = ['edlink'] + cols + [ 'region', 'country', 'layout', 'thumb', 'material', 'stars', 'rel' ] heads = ['Pack ID'] + heads + [ 'Rg', 'Cy', 'Ly', 'Th', 'Mat', 'Models', 'Related' ] elif sections[0]['flags'] & pif.dbh.FLAG_SECTION_SHOW_IDS: cols = ['id'] + cols + ['regionname'] heads = ['ID'] + heads + ['Region'] else: cols += ['regionname'] heads += ['Region'] cols += ['note'] heads += ['Note'] heads = dict(zip(cols, heads)) pack_ids_found = [] llineup = dict(section=[]) for lsection in sections: if sec and lsection['id'] != sec: continue entries = list() for pack in packs: pack['longid'] = pack['id'] + ('-' + pack['var'] if pack['var'] else '') if pack['section_id'] == lsection['id']: if not verbose and pack['id'] in pack_ids_found: continue pack_ids_found.append(pack['id']) years.add(pack['first_year']) regions.add(pack['region']) pack['name'] = pack['rawname'].replace(';', ' ') if (year and (year < pack['first_year'] or year > pack['end_year'])) or \ (region and region != pack['region']) or (lid and not pack['id'].startswith(lid)) or \ (material and pack['material'] != material) or \ not useful.search_match(title, pack['name']): continue pack['year'] = (pack['first_year'] + '-' + pack['end_year']) if ( pack['end_year'] and pack['end_year'] != pack['first_year'] ) else pack['first_year'] pack['layout'] = pack['layout'] if pack[ 'layout'] in pack_layouts else '<font color="red">%s</font>' % pack[ 'layout'] pack['page'] = pif.form.get_str('page') pack['regionname'] = mbdata.regions[pack['region']] pack[ 'name'] = '<a href="?page=%(page)s&id=%(id)s">%(name)s</a>' % pack pack['pic'] = mbdata.comment_icon.get('c') if imgsizes( pif, config.IMG_DIR_PROD_PACK, pack['id'].lower()) else '' has_note = has_note or bool(pack['note']) if verbose: modify_pack_admin(pif, pack) entries.append(pack) if not entries: continue entries.sort(key=lambda x: (x[pif.form.get_str('order', 'name')], x[ 'name'], x['first_year'])) # if not has_note: # cols.remove('note') lrange = dict(entry=entries, note='') lsection['columns'] = cols lsection['headers'] = heads lsection['range'] = [lrange] lsection['note'] = '' llineup['section'].append(lsection) useful.write_comment(llineup) context = { 'page_id': pif.form.get_str('page'), 'years': sorted(years), 'regions': [(x, mbdata.regions[x]) for x in sorted(regions)], 'llineup': llineup, 'section_id': sec_id, 'num': 2 if sec_id == '2packs' else 10 if sec_id == '10packs' else 5, #'lid': calc_pack_select(pif, packs), } return pif.render.format_template('packlist.html', **context)
def call_main(page_id, form_key='', defval='', args='', dbedit=None): #useful.write_comment('PID', os.getpid()) pif = None try: import pifile if isinstance(page_id, pifile.PageInfoFile): pif = page_id else: pif = get_page_info(page_id, form_key, defval, args, dbedit) pif.start() if '/etc/passwd' in os.environ.get('QUERY_STRING', '') or '%2fetc%2fpasswd' in os.environ.get('QUERY_STRING', '').lower(): raise useful.Redirect('https://www.nsa.gov/') except SystemExit: pass except useful.SimpleError as e: simple_html(status=e.status) print useful.render_template('error.html', error=[e.value], page={'tail':''}) if pif: pif.log.debug.error('SimpleError: ' + str(e) + ' - ' + '''%s''' % os.environ.get('REQUEST_URI', '')) handle_exception(pif, True, False) return except MySQLdb.OperationalError: simple_html() print 'The database is currently down, and thus, this page is unable to be shown.<p>' str_tb = write_traceback_file(pif) handle_exception(pif, True) return except useful.Redirect as e: if not useful.is_header_done(): pif.render.print_html() print pif.render.format_template('forward.html', url=e.value, delay=e.delay) return except: handle_exception(pif) return try: ret = main_fn(pif) if not useful.is_header_done(): pif.render.print_html() if pif.render.is_html: useful.write_comment("Page:", pif.page_id, 'Time:', time.time() - pif.start_seconds) if ret and not pif.unittest: print ret except SystemExit: pass except useful.SimpleError as e: if not useful.is_header_done(): pif.render.print_html(status=e.status) print pif.render.format_template('error.html', error=[e.value]) except useful.Redirect as e: if not useful.is_header_done(): pif.render.print_html() print pif.render.format_template('forward.html', url=e.value, delay=e.delay) except MySQLdb.OperationalError: if not useful.is_header_done(): pif.render.print_html() print 'The database is currently done, and thus, this page is unable to be shown.<p>' str_tb = write_traceback_file(pif) except: handle_exception(pif) raise useful.header_done(True) useful.write_comment()
def comment(self, *args): if self.dump_file: # pragma: no cover self.dump_file.write(' '.join([str(x) for x in args]) + '\n') elif self.verbose: useful.write_comment(*args)
def call_main(page_id, form_key='', defval='', args='', dbedit=None): # useful.write_comment('PID', os.getpid(), 'GURU', config.GURU_ID) status_code = 'unset' pif = None try: pif = (page_id if isinstance(page_id, pifile.PageInfoFile) else get_page_info(page_id, form_key, defval, args, dbedit)) except SystemExit: pass except pymysql.OperationalError as e: status_code = 'db' simple_html() print( 'The database is currently down, and thus, this page is unable to be shown.<p>' ) write_traceback_file(pif, e) handle_exception(pif, e, True, status_code=status_code) return except Exception as e: status_code = 'exc' simple_html() handle_exception(pif, e, status_code=status_code) return pif.start() try: if ('/etc/passwd' in os.environ.get('QUERY_STRING', '') or '%2fetc%2fpasswd' in os.environ.get('QUERY_STRING', '').lower()): raise useful.Redirect('https://www.nsa.gov/') ret = main_fn(pif) if not useful.is_header_done(): pif.render.print_html() if pif.render.is_html: useful.write_comment("Page:", pif.page_id, 'Time:', time.time() - pif.start_seconds) if ret and not pif.unittest: print(ret) except SystemExit: pass # the happiest exception on earth status_code = 'exit' except useful.SimpleError as e: if not useful.is_header_done(): status_code = e.status pif.render.print_html(status=e.status) print(pif.render.format_template('error.html', error=[e.value])) except useful.Redirect as e: if not useful.is_header_done(): status_code = 302 pif.render.print_html(status=302) print( pif.render.format_template('forward.html', url=e.value, delay=e.delay)) except pymysql.OperationalError as e: if not useful.is_header_done(): status_code = 500 pif.render.print_html(status=500) print( 'The database is currently down, and thus, this page is unable to be shown.<p>' ) write_traceback_file(pif, e) except Exception as e: status_code = 'exc' handle_exception(pif, e, status_code=status_code) raise useful.header_done(True) useful.write_comment() log_page_call(pif, status_code=status_code)