def snapshot_sql_result(value): # SQLMultipleJoin and SQLRelatedJoin return # SelectResults, which doesn't really help the Snapshot # object. We therefore list()ify the values; this isn't # perfect but allows deltas to be generated reliably. return shortlist( value, longest_expected=100, hardlimit=HARD_LIMIT_FOR_SNAPSHOT)
def get_bugmail_from_address(person, bug): """Returns the right From: address to use for a bug notification.""" if person == getUtility(ILaunchpadCelebrities).janitor: return format_address( 'Launchpad Bug Tracker', "%s@%s" % (bug.id, config.launchpad.bugs_domain)) if person.hide_email_addresses: return format_address( person.displayname, "%s@%s" % (bug.id, config.launchpad.bugs_domain)) if person.preferredemail is not None: return format_address(person.displayname, person.preferredemail.email) # XXX: Bjorn Tillenius 2006-04-05: # The person doesn't have a preferred email set, but he # added a comment (either via the email UI, or because he was # imported as a deaf reporter). It shouldn't be possible to use the # email UI if you don't have a preferred email set, but work around # it for now by trying hard to find the right email address to use. email_addresses = shortlist( getUtility(IEmailAddressSet).getByPerson(person)) if not email_addresses: # XXX: Bjorn Tillenius 2006-05-21 bug=33427: # A user should always have at least one email address, # but due to bug #33427, this isn't always the case. return format_address(person.displayname, "%s@%s" % (bug.id, config.launchpad.bugs_domain)) # At this point we have no validated emails to use: if any of the # person's emails had been validated the preferredemail would be # set. Since we have no idea of which email address is best to use, # we choose the first one. return format_address(person.displayname, email_addresses[0].email)
def related_projects(self): """Return all project groups and projects. This property was created for the Related projects portlet in the bug tracker's page. """ pillars = chain(*self.context.getRelatedPillars(self.user)) return shortlist([p for p in pillars if p.active], 100)
def getBestMirrorsForCountry(self, country, mirror_type): """See IDistributionMirrorSet""" # As per mvo's request we only return mirrors which have an # http_base_url. country_id = None if country is not None: country_id = country.id base_query = And( DistributionMirror.content == mirror_type, DistributionMirror.enabled == True, DistributionMirror.http_base_url != None, DistributionMirror.official_candidate == True, DistributionMirror.status == MirrorStatus.OFFICIAL) query = And(DistributionMirror.countryID == country_id, base_query) # The list of mirrors returned by this method is fed to apt through # launchpad.net, so we order the results randomly in a lame attempt to # balance the load on the mirrors. order_by = [Func('random')] mirrors = shortlist( DistributionMirror.select(query, orderBy=order_by), longest_expected=200) if not mirrors and country is not None: continent = country.continent query = And( Country.q.continentID == continent.id, DistributionMirror.countryID == Country.q.id, base_query) mirrors.extend(shortlist( DistributionMirror.select(query, orderBy=order_by), longest_expected=300)) if mirror_type == MirrorContent.ARCHIVE: main_mirror = getUtility( ILaunchpadCelebrities).ubuntu_archive_mirror elif mirror_type == MirrorContent.RELEASE: main_mirror = getUtility( ILaunchpadCelebrities).ubuntu_cdimage_mirror else: raise AssertionError("Unknown mirror type: %s" % mirror_type) assert main_mirror is not None, 'Main mirror was not found' if main_mirror not in mirrors: mirrors.append(main_mirror) return mirrors
def published_by_version(self, sourcepackage): """Return a dict of publications keyed by version. :param sourcepackage: ISourcePackage """ publications = sourcepackage.distroseries.getPublishedSources( sourcepackage.sourcepackagename) pocket_dict = {} for pub in shortlist(publications): version = pub.source_package_version pocket_dict.setdefault(version, []).append(pub) return pocket_dict
def _getMilestones(self, user, only_active): """Return a list of milestones for this project group. If only_active is True, only active milestones are returned, else all milestones. A project group has a milestone named 'A', if at least one of its products has a milestone named 'A'. """ store = Store.of(self) columns = ( Milestone.name, SQL('MIN(Milestone.dateexpected)'), SQL('BOOL_OR(Milestone.active)'), ) privacy_filter = ProductSet.getProductPrivacyFilter(user) conditions = And(Milestone.product == Product.id, Product.project == self, Product.active == True, privacy_filter) result = store.find(columns, conditions) result.group_by(Milestone.name) if only_active: result.having('BOOL_OR(Milestone.active) = TRUE') # MIN(Milestone.dateexpected) has to be used to match the # aggregate function in the `columns` variable. result.order_by( 'milestone_sort_key(MIN(Milestone.dateexpected), Milestone.name) ' 'DESC') # An extra query is required here in order to get the correct # products without affecting the group/order of the query above. products_by_name = {} if result.any() is not None: milestone_names = [data[0] for data in result] product_conditions = And( Product.project == self, Milestone.product == Product.id, Product.active == True, privacy_filter, In(Milestone.name, milestone_names)) for product, name in ( store.find((Product, Milestone.name), product_conditions)): if name not in products_by_name.keys(): products_by_name[name] = product return shortlist( [ProjectMilestone( self, name, dateexpected, active, products_by_name.get(name, None)) for name, dateexpected, active in result])
def getMilestoneWidgetValues(self): """See `BugTaskSearchListingView`. We return only the active milestones on the current distribution since any others are irrelevant. """ current_distro = self.current_package.distribution vocabulary_registry = getVocabularyRegistry() vocabulary = vocabulary_registry.get(current_distro, 'Milestone') return shortlist([ dict(title=milestone.title, value=milestone.token, checked=False) for milestone in vocabulary], longest_expected=10)
def processCondorcetVotingForm(self): """Process the condorcet-voting form to change a user's vote or register a new one. This method must not be called if the poll is not open. """ assert self.context.isOpen() form = self.request.form activeoptions = shortlist(self.context.getActiveOptions()) newvotes = {} for option in activeoptions: try: preference = int(form.get('option_%d' % option.id)) except ValueError: # XXX: Guilherme Salgado 2005-09-14: # User tried to specify a value which we can't convert to # an integer. Better thing to do would be to notify the user # and ask him to fix it. preference = None newvotes[option] = preference if self.userVoted(): # This is a vote change. # For now it's not possible to have votes in an inactive option, # but it'll be in the future as we'll allow people to make options # inactive after a poll opens. assert len(activeoptions) == len(self.currentVotes) for vote in self.currentVotes: vote.preference = newvotes.get(vote.option) self.currentVotes.sort(key=lambda v: v.preference) self.feedback = "Your vote was changed successfully." else: # This is a new vote. votes = self.context.storeCondorcetVote(self.user, newvotes) self.token = votes[0].token self.currentVotes = sorted(votes, key=lambda v: v.preference) if self.isSecret(): self.feedback = ( "Your vote has been recorded. If you want to view or " "change it later you must write down this key: %s" % self.token) else: self.feedback = ( "Your vote was stored successfully. You can come back to " "this page at any time before this poll closes to view " "or change your vote, if you want.")
def getReleasedPackages(self, binary_name, pocket=None, include_pending=False, archive=None): """See IDistroArchSeries.""" from lp.soyuz.model.publishing import BinaryPackagePublishingHistory queries = [] if not IBinaryPackageName.providedBy(binary_name): binary_name = BinaryPackageName.byName(binary_name) queries.append(""" binarypackagerelease=binarypackagerelease.id AND binarypackagerelease.binarypackagename=%s AND distroarchseries = %s """ % sqlvalues(binary_name, self)) if pocket is not None: queries.append("pocket=%s" % sqlvalues(pocket.value)) if include_pending: queries.append("status in (%s, %s)" % sqlvalues( PackagePublishingStatus.PUBLISHED, PackagePublishingStatus.PENDING)) else: queries.append("status=%s" % sqlvalues( PackagePublishingStatus.PUBLISHED)) archives = self.distroseries.distribution.getArchiveIDList(archive) queries.append("archive IN %s" % sqlvalues(archives)) published = BinaryPackagePublishingHistory.select( " AND ".join(queries), clauseTables=['BinaryPackageRelease'], orderBy=['-id']) return shortlist(published)
def _exportToBranches(self, productseries_iter): """Loop over `productseries_iter` and export their translations.""" items_done = 0 items_failed = 0 unpushed_branches = 0 productseries = shortlist(productseries_iter, longest_expected=2000) for source in productseries: try: self._exportToBranch(source) if self.txn: self.txn.commit() except (KeyboardInterrupt, SystemExit): raise except NotBranchError: unpushed_branches += 1 if self.txn: self.txn.abort() self._handleUnpushedBranch(source) if self.txn: self.txn.commit() except Exception as e: items_failed += 1 self.logger.error( "Failure in %s/%s: %s", source.product.name, source.name, repr(e)) if self.txn: self.txn.abort() items_done += 1 self.logger.info( "Processed %d item(s); %d failure(s), %d unpushed branch(es)." % ( items_done, items_failed, unpushed_branches))
def documentation(self): filter = [SpecificationFilter.COMPLETE, SpecificationFilter.INFORMATIONAL] return shortlist(self.context.specifications(self.user, filter=filter))
def all_specifications(self): return shortlist(self.context.all_specifications(self.user))
def spec_links(self): """List all of the SprintSpecifications appropriate for this view.""" filter = self.spec_filter return shortlist(self.context.specificationLinks(filter=filter))
def getTranslationTemplateFormats(self): """See `IHasTranslationTemplates`.""" formats_query = self.getCurrentTranslationTemplates().order_by( 'source_file_format').config(distinct=True) return helpers.shortlist( formats_query.values(POTemplate.source_file_format), 10)
def initialize(self): """See `LaunchpadView`.""" super(CVEReportView, self).initialize() search_params = BugTaskSearchParams( self.user, has_cve=True) bugtasks = shortlist( self.context.searchTasks(search_params), longest_expected=600) if not bugtasks: self.open_cve_bugtasks = [] self.resolved_cve_bugtasks = [] return bugtask_set = getUtility(IBugTaskSet) badge_properties = bugtask_set.getBugTaskBadgeProperties(bugtasks) people = bugtask_set.getBugTaskPeople(bugtasks) open_bugtaskcves = {} resolved_bugtaskcves = {} for bugtask in bugtasks: badges = badge_properties[bugtask] # Wrap the bugtask in a BugTaskListingItem, to avoid db # queries being issues when trying to render the badges. bugtask = BugTaskListingItem( bugtask, has_bug_branch=badges['has_branch'], has_specification=badges['has_specification'], has_patch=badges['has_patch'], tags=(), people=people) if bugtask.status in RESOLVED_BUGTASK_STATUSES: current_bugtaskcves = resolved_bugtaskcves else: current_bugtaskcves = open_bugtaskcves if bugtask.bug.id not in current_bugtaskcves: current_bugtaskcves[bugtask.bug.id] = BugTaskCve() current_bugtaskcves[bugtask.bug.id].bugtasks.append(bugtask) bugcves = getUtility(ICveSet).getBugCvesForBugTasks( bugtasks, get_cve_display_data) for bug, cve in bugcves: if bug.id in open_bugtaskcves: open_bugtaskcves[bug.id].cves.append(cve) if bug.id in resolved_bugtaskcves: resolved_bugtaskcves[bug.id].cves.append(cve) # Order the dictionary items by bug ID and then store only the # bugtaskcve objects. self.open_cve_bugtasks = [ bugtaskcve for bug, bugtaskcve in sorted(open_bugtaskcves.items())] self.resolved_cve_bugtasks = [ bugtaskcve for bug, bugtaskcve in sorted(resolved_bugtaskcves.items())] # The page contains links to the bug task assignees: # Pre-load the related Person and ValidPersonCache records assignee_ids = [task.assigneeID for task in bugtasks] list(getUtility(IPersonSet).getPrecachedPersonsFromIDs( assignee_ids, need_validity=True))
def documentation(self): filter = [ SpecificationFilter.COMPLETE, SpecificationFilter.INFORMATIONAL ] return shortlist(self.context.specifications(self.user, filter=filter))
def _getNominatableObjects(self): """Return all non-obsolete distribution series""" return [ series for series in shortlist(self.distribution.series) if series.status != SeriesStatus.OBSOLETE]
def bugtasks(self): tasks = Store.of(self).find(BugTask, BugTask.bugwatch == self.id) tasks = tasks.order_by(Desc(BugTask.datecreated)) return shortlist(tasks, 10, 100)
def parents(self): """See IRevision.parents""" return shortlist(RevisionParent.selectBy( revision=self, orderBy='sequence'))
def _getNominatableObjects(self): """See BugNominatableSeriesVocabularyBase.""" return shortlist(self.product.series)
def parents(self): """See IRevision.parents""" return shortlist( RevisionParent.selectBy(revision=self, orderBy='sequence'))