def get_objects(self, **kwargs): """ Returns the files whose checksum corresponds to the one given. """ try: page = int(request.args.get("page")) except: page = 1 checksum = request.args.get("checksum") package = request.args.get("package") or None # we count the number of results: count = qry.count_files_checksum(session, checksum, package) count = count.first()[0] # pagination: if self.d.get('pagination'): offset = int(current_app.config.get("LIST_OFFSET") or 60) start = (page - 1) * offset end = start + offset slice_ = (start, end) pagination = Pagination(page, offset, count) else: pagination = None slice_ = None # finally we get the files list results = self._files_with_sum( checksum, slice_=slice_, package=package) return dict(results=results, sha256=checksum, count=count, page=page, pagination=pagination)
def get_objects(self, **kwargs): """ Returns the files whose checksum corresponds to the one given. """ page = request.args.get("page", 1, type=int) checksum = request.args.get("checksum") package = request.args.get("package") or None # we count the number of results: count = qry.count_files_checksum(session, checksum, package) count = count.first()[0] # pagination: if self.d.get('pagination'): offset = int(current_app.config.get("LIST_OFFSET") or 60) start = (page - 1) * offset end = start + offset slice_ = (start, end) pagination = Pagination(page, offset, count) else: pagination = None slice_ = None # finally we get the files list results = self._files_with_sum( checksum, slice_=slice_, package=package) return dict(results=results, sha256=checksum, count=count, page=page, pagination=pagination)
def _render_file(self, location): """ renders a file """ file_ = SourceFile(location) checksum = file_.get_sha256sum(session) number_of_duplicates = (qry.count_files_checksum(session, checksum) .first()[0] ) pkg_infos = Infobox(session, location.get_package(), location.get_version()).get_infos() text_file = file_.istextfile() raw_url = file_.get_raw_url() path = location.get_path_to() if self.d.get('api'): self.render_func = jsonify return dict(type="file", file=location.get_deepest_element(), package=location.get_package(), version=location.get_version(), mime=file_.get_mime(), raw_url=raw_url, path=path, text_file=text_file, stat=qry.location_get_stat(location.sources_path), checksum=checksum, number_of_duplicates=number_of_duplicates, pkg_infos=pkg_infos ) # prepare the non-api render func self.render_func = None # more work to do with files # as long as 'lang' is in keys, then it's a text_file lang = None if 'lang' in request.args: lang = request.args['lang'] # if the file is not a text file, we redirect to it elif not text_file: self.render_func = bind_redirect(raw_url) # set render func (non-api form) if not self.render_func: sources_path = raw_url.replace( current_app.config['SOURCES_STATIC'], current_app.config['SOURCES_DIR'], 1) # ugly, but better than global variable, # and better than re-requesting the db # TODO: find proper solution for retrieving souces_path # (without putting it in kwargs, we don't want it in # json rendering eg) # we get the variables for highlighting and message (if they exist) try: highlight = request.args.get('hl') except (KeyError, ValueError, TypeError): highlight = None try: msg = request.args.getlist('msg') if msg == "": msg = None # we don't want empty messages except (KeyError, ValueError, TypeError): msg = None # we preprocess the file with SourceCodeIterator sourcefile = SourceCodeIterator( sources_path, hl=highlight, msg=msg, lang=lang) self.render_func = bind_render( self.d['templatename'], nlines=sourcefile.get_number_of_lines(), pathl=qry.location_get_path_links(".source", path), file_language=sourcefile.get_file_language(), msgs=sourcefile.get_msgdict(), code=sourcefile) return dict(type="file", file=location.get_deepest_element(), package=location.get_package(), version=location.get_version(), mime=file_.get_mime(), raw_url=raw_url, path=path, text_file=text_file, stat=qry.location_get_stat(location.sources_path), checksum=checksum, number_of_duplicates=number_of_duplicates, pkg_infos=pkg_infos )