Beispiel #1
0
def emails_for_person(person, session=None):
    """
    Given a login, a full name, or an email, returns the corresponding email.
    This function must be imported from a running app, or a session must
    be provided.
    """
    # we import the session here, to avoid the exception when debianutils is
    # imported from elsewhere than the app
    if session is None:
        from firewoes.web.app import session
    
    if "@" in person:
        return [person]
    results = (session.query(DebianMaintainer)
               .filter(DebianMaintainer.name == person)
               .all())
    if results:
        return [res.email for res in results]
    
    result = (session.query(DebianMaintainer)
               .filter(DebianMaintainer.email == person + "@debian.org")
               .first())
    if result:
        return [result.email]
    
    return []
Beispiel #2
0
 def name_contains(self, name, limit=None):
     """ returns the packages whose name contains name """
     elems = session.query(Sut.name).filter(
         Sut.name.contains(name)).distinct()
     if limit is None:
         elems = elems.all()
     else:
         elems = elems.limit(limit)
     # we remove 'name' if it's here
     elems = [e for e in elems if e.name != name]
     return to_dict(elems)
Beispiel #3
0
 def id(self, id):
     try:
         elem = session.query(self.fh_class).filter(
             self.fh_class.id == id).first()
     except:
         raise Http500Error
     
     try:
         id = elem.id # raises error if the element doesn't exist
     except:
         raise Http404Error("This element (%s, %s) doesn't exist."
                            % (self.fh_class.__name__, id))
     return to_dict(elem)
Beispiel #4
0
 def _suggestions(self, current_args):
     """
     Returns suggestions for the current search, with probably better
     results.
     """
     if current_args.get("sut_name"):
         elems = (session.query(Sut.name,
                                func.count(Result.id).label("count"))
                  .filter(Sut.name.contains(current_args["sut_name"]))
                  .filter(Result.analysis_id == Analysis.id)
                  .filter(Analysis.metadata_id == Metadata.id)
                  .filter(Metadata.sut_id == Sut.id)
                  .group_by(Sut.name)
                  .order_by(desc("count"))
                  .all()
                  )
         suggestions = [dict(sut_name=sut.name) for sut in elems]
     elif current_args.get("maintainer"):
         elems = (session.query(DebianMaintainer.name,
                                func.count(Result.id).label("count"))
                  .filter(DebianMaintainer.name.ilike(
                     "%" + current_args["maintainer"] + "%"))
                  # maybe postgresql-specific
                  .filter(Result.analysis_id == Analysis.id)
                  .filter(Analysis.metadata_id == Metadata.id)
                  .filter(Metadata.sut_id == Sut.id)
                  .filter(Sut.name == DebianPackagePeopleMapping.package_name)
                  .filter(DebianPackagePeopleMapping.maintainer_email ==
                          DebianMaintainer.email)
                  .group_by(DebianMaintainer.name)
                  .order_by(desc("count"))
                  .all()
                  )
         suggestions = [dict(maintainer=maint.name) for maint in elems]
         
     else:
         suggestions = []
     
     return suggestions
Beispiel #5
0
    def count_per_generator(self):
        elems = (
            session.query(Generator.name,
                          func.count(Result.id).label("count"))
            .join(Sut, Sut.id == self.package_id)
            .join(Metadata, and_(Sut.id == Metadata.sut_id,
                                 Metadata.generator_id==Generator.id))
            .join(Analysis, Analysis.metadata_id == Metadata.id)
            .outerjoin(Result, Result.analysis_id == Analysis.id)
            .group_by(Generator.name)
            .order_by(desc("count"))
            .all())

        return elems
Beispiel #6
0
 def with_most_results(self, limit=5):
     """
     Returns the list of packages which have the most results.
     """
     elems = (session.query(Sut.name, func.count(Result.id).label("count"))
              .filter(Result.analysis_id == Analysis.id)
              .filter(Analysis.metadata_id == Metadata.id)
              .filter(Metadata.sut_id == Sut.id)
              .group_by(Sut.name)
              .order_by(desc("count"))
              .limit(limit)
              .all()
              )
     return to_dict(elems)
Beispiel #7
0
 def random_results(self, limit=5):
     """
     Returns the last results.
     """
     elems = (session.query(Result.id, Message.text, Sut.name)
              .filter(Result.message_id == Message.id)
              .filter(Result.analysis_id == Analysis.id)
              .filter(Analysis.metadata_id == Metadata.id)
              .filter(Metadata.sut_id == Sut.id)
              .order_by(func.random())
              # PostgreSQL-specific, maybe func.rand() for other back-ends
              .limit(limit)
              .all()
              )
     return to_dict(elems)
Beispiel #8
0
 def versions(self, name):
     elems = session.query(Generator.version).filter(
         Generator.name == name).all()
     return to_dict(elems)
Beispiel #9
0
 def unique_by_name(self):
     elems = session.query(Generator.name).distinct().all()
     return to_dict(elems)
Beispiel #10
0
 def all(self):
     elems = session.query(self.fh_class).order_by(Generator.name).all()
     return to_dict(elems)
Beispiel #11
0
 def all(self):
     elem = session.query(self.fh_class).all()
     return to_dict(elem)
Beispiel #12
0
 def filter(self, request_args, offset=None):
     """
     returns the results corresponding to the args in request_args,
     along with a drill down menu
     
     request_args: GET variables dict (?generator_name=foo&bar=foobar...)
     offset: number of results (if not set, this value is read in config)
     """
     
     import filters
     
     # we need the arguments without "page" for the menu
     # ("page" would add page=foo on the menu links, which we don't want)
     args_without_page = request_args.copy()
     try:
         del(args_without_page["page"])
     except:
         pass
     
     query = (session.query(
             Result.id,
             Result.type.label("result_type"),
             File.givenpath.label("location_file"),
             Function.name.label("location_function"),
             Message.text.label("message_text"),
             Message.id.label("message_id"),
             Point, Range,
             Sut.name.label("sut_name"),
             Sut.version.label("sut_version"),
             Sut.type.label("sut_type"),
             Sut.release.label("sut_release"),
             Sut.buildarch.label("sut_buildarch"),
             Generator.name.label("generator_name"),
             Generator.version.label("generator_version"),
             Result.testid.label("testid"))
         .outerjoin(Location, File, Function, Point, Analysis,
                    Metadata, Generator, Sut, Message)
         .outerjoin(Range, Location.range_id==Range.id)
              )
     menu = filters.Menu(args_without_page)
     query = menu.filter_sqla_query(query)
     
     # we get the page number and the offset
     try:  page = int(request_args["page"])
     except: page = 1
     
     try: offset = int(request_args["offset"])
     except: offset = offset or app.config["SEARCH_RESULTS_OFFSET"]
     
     # we calculate the range of results
     start = (page - 1) * offset
     end = start + offset
     
     menu=menu.get(session,
                   max_items=app.config["SEARCH_MENU_MAX_NUMBER_OF_ELEMENTS"])
     results_all_count = query.count()
     results=to_dict(query.slice(start, end).all())
     
     # do we need to suggest things?
     if len(results) == 0:
         suggestions = self._suggestions(request_args)
     else:
         suggestions = []
                 
     
     return dict(results=results,
                 menu=menu,
                 page=page,
                 offset=offset,
                 results_all_count=results_all_count,
                 results_range = (start+1, start+len(results)),
                 # to avoid 1-10 of 5 results
                 suggestions=suggestions,
                 )
Beispiel #13
0
 def all(self):
     elems = session.query(Result.id).all()
     return to_dict(elems)
Beispiel #14
0
 def versions(self, name):
     elems = session.query(Sut).filter(
         Sut.name==name).order_by(Sut.version).all()
     return to_dict(elems)
Beispiel #15
0
 def id(self, id):
     elem = session.query(Analysis).filter(Analysis.id == id).first()
     elem = to_dict(elem)
     elem['results'] = to_dict(session.query(
             Result.id).filter(Result.analysis_id == id).all())
     return elem
Beispiel #16
0
 def all(self):
     elems = session.query(Analysis.id,
                          Generator.name.label("generator_name")).filter(
         and_(Analysis.metadata_id == Metadata.id,
              Metadata.generator_id == Generator.id)).all()
     return to_dict(elems)