def get(cls, key): """Return the instance of this class whose key is supplied.""" instance = IStore(cls).get(cls, key) if instance is None: raise SQLObjectNotFound( 'No occurrence of %s has key %s' % (cls.__name__, key)) return instance
def assignProfileCategory(categoryName, profileRecs=None, screenNames=None): """ Assign Categories to Profiles. Fetch Category or create it if it does not exist. Put Profiles in the Category but ignore if link exists already. An error is raised if a Profile does not exist, but previous Profiles in the list still have been allocated already before the error occurred. :param categoryName: String. Get a category by name and create it if it does not exist yet. If Profile records or Profile screen names are provided, then assign all of those Profiles to the category. Both Profile inputs can be left as not set to just create the Category. :param profileRecs: Default None. List of db Profile records to be assigned to the category. Cannot be empty if screenNames is also empty. :param screenNames: Default None. List of Profile screen names to be assigned to the category. The screen names should exist as Profiles in the db already (matching on exact case), otherwise an error will be raised. The screenNames argument cannot be empty if profileRecs is also empty. :return tuple of new and existing counts. - newCnt: Count of new Profile Category links created. - existingCnt: Count of Profile Category links not created because they already exist. """ newCnt = 0 existingCnt = 0 try: categoryRec = db.Category.byName(categoryName) except SQLObjectNotFound: categoryRec = db.Category(name=categoryName) print("Created category: {0}".format(categoryName)) if profileRecs or screenNames: if profileRecs is None: # Use screen names to populate an empty profileRecs list. profileRecs = [] for screenName in screenNames: profile = db.Profile.select( LIKE(db.Profile.q.screenName, screenName)).getOne(None) if not profile: raise SQLObjectNotFound( "Cannot assign Category since Profile screen name" " is not in db: {0}".format(screenName)) profileRecs.append(profile) for profileRec in profileRecs: try: categoryRec.addProfile(profileRec) newCnt += 1 except DuplicateEntryError: existingCnt += 1 return newCnt, existingCnt
def get(cls, key): """Return the instance of this class whose key is supplied. :raises: SQLObjectNotFound """ instance = IStore(BranchJob).get(BranchJob, key) if instance is None or instance.job_type != cls.class_job_type: raise SQLObjectNotFound( 'No occurrence of %s has key %s' % (cls.__name__, key)) return cls(instance)
def byMsgid(cls, key): """Return a POMsgID object for the given msgid.""" # We can't search directly on msgid, because this database column # contains values too large to index. Instead we search on its # hash, which *is* indexed r = POMsgID.selectOne('sha1(msgid) = sha1(%s)' % quote(key)) if r is None: # To be 100% compatible with the alternateID behaviour, we should # raise SQLObjectNotFound instead of KeyError raise SQLObjectNotFound(key) return r
def get(cls, job_id): """Get a job by id. :return: the BranchMergeProposalJob with the specified id, as the current BranchMergeProposalJobDereived subclass. :raises: SQLObjectNotFound if there is no job with the specified id, or its job_type does not match the desired subclass. """ job = BranchMergeProposalJob.get(job_id) if job.job_type != cls.class_job_type: raise SQLObjectNotFound('No object found with id %d and type %s' % (job_id, cls.class_job_type.title)) return cls(job)
def assignTweetCampaign(campaignRec, tweetRecs=None, tweetGuids=None): """ Assign Campaigns to Tweets using the ORM. Fetch a Campaign and assign it to Tweets, ignoring existing links and raising an error on a Campaign which does not exist. For large batches of inserts, rather use bulkAssignTweetCampaign. Search query is not considered here and should be set using the campaign manager utility or the ORM directly. :param campaignRec: Campaign record to assign to all Tweet records indicated with tweetRecs or tweetGuids inputs. Both Tweet inputs can be left as not set to just create the Campaign. Note that the assignProfileCategory function expects a Category name because it can be created there, but here the actual Campaign record is expected because creation must be handled with the Campaign manager utility instead because of the search query field. :param tweetRecs: Default None. List of db Tweet records to be assigned to the campaign. Cannot be empty if tweetGuids is also empty. :param tweetGuids: Default None. List of Tweet GUIDs to be assigned to the campaign. The GUIDs should exist as Tweets in the db already, otherwise an error will be printed and ignored. The tweetGuids argument cannot be empty if tweetRecs is also empty. :return newCnt: Count of new Tweet Campaign links created. :return existingCnt: Count of Tweet Campaign links not created because they already exist. """ newCnt = 0 existingCnt = 0 if not tweetRecs: # Use GUIDs to populate tweetRecs list. tweetRecs = [] for guid in tweetGuids: try: tweet = db.Tweet.byGuid(guid) except SQLObjectNotFound: raise SQLObjectNotFound("Cannot assign Campaign as Tweet" " GUID is not in db: {0}".format(guid)) tweetRecs.append(tweet) for tweet in tweetRecs: try: campaignRec.addTweet(tweet) newCnt += 1 except DuplicateEntryError: existingCnt += 1 return newCnt, existingCnt
def getByBlobUUID(cls, uuid): """See `IApportJobSource`.""" store = IStore(ApportJob) jobs_for_blob = store.find( ApportJob, ApportJob.job == Job.id, ApportJob.job_type == cls.class_job_type, ApportJob.blob_id == TemporaryBlobStorage.id, TemporaryBlobStorage.uuid == uuid) job_for_blob = jobs_for_blob.one() if job_for_blob is None: raise SQLObjectNotFound( "No ProcessApportBlobJob found for UUID %s" % uuid) return cls(job_for_blob)