def install(self, django_user): """ Install queries. Raise InstallException on failure. """ LOG.info('Installing sample query: %s' % (self.name, )) try: # Don't overwrite model = models.SavedQuery.objects.get(owner=django_user, name=self.name, type=self.type) msg = _('Sample design %(name)s already exists.') % { 'name': self.name } LOG.error(msg) raise InstallException(msg) except models.SavedQuery.DoesNotExist: model = models.SavedQuery(owner=django_user, name=self.name) model.type = self.type # The data field needs to be a string. The sample file writes it # as json (without encoding into a string) for readability. model.data = simplejson.dumps(self.data) model.desc = self.desc model.save() LOG.info('Successfully installed sample design: %s' % (self.name, ))
def safe_get_design(request, design_type, design_id=None): """ Return a new design, if design_id is None, Return the design with the given id and type. If the design is not found, display a notification and return a new design. """ design = None if design_id is not None: try: design = models.SavedQuery.get(design_id, request.user, design_type) except models.SavedQuery.DoesNotExist: messages.error(request, _('Design does not exist.')) if design is None: design = models.SavedQuery(owner=request.user, type=design_type) return design