def __init__(self, **kw): """ **kw arguments used to override defaults additional **kw are passed to the model """ # set instance parameters from kw and defaults for key in self.defaults: setattr(self, key, kw.pop(key, self.defaults[key])) if self.item_plural is None: self.item_plural = self.item_name + 's' # should templates be reloaded? if isinstance(self.reload, basestring): self.reload = self.reload.lower() == 'true' # model: backend storage and associated methods if 'fields' in kw and isinstance(kw['fields'], basestring): # split fields if given as a string kw['fields'] = strsplit(kw['fields']) if hasattr(self.model_type, '__call__'): model = self.model_type elif self.model_type in models: model = models[self.model_type] else: try: import pyloader model = pyloader.load(self.model_type) except: raise AssertionError("model_type '%s' not found in %s" % (self.model_type, models.keys())) self.model = model(**kw) # add an about view if file specified if self.about: about = file(self.about).read() import docutils.core about = docutils.core.publish_parts(about, writer_name='html')['body'] self.about = about # request handlers in order they will be tried if self.handlers is None: self.handlers = [ TagsView, CreateProjectView, FieldView, QueryView, DeleteProjectHandler, ProjectView] if self.about: self.handlers.append(AboutView) # extend reserved URLS from handlers if self.reserved is None: self.reserved = set(['css', 'js', 'img']) for handler in self.handlers: if handler.handler_path: self.reserved.add(handler.handler_path[0])
def __init__(self, **kw): """ **kw arguments used to override defaults additional **kw are passed to the model """ # set instance parameters from kw and defaults for key in self.defaults: setattr(self, key, kw.pop(key, self.defaults[key])) if self.item_plural is None: self.item_plural = self.item_name + 's' # should templates be reloaded? if isinstance(self.reload, basestring): self.reload = self.reload.lower() == 'true' # model: backend storage and associated methods if 'fields' in kw and isinstance(kw['fields'], basestring): # split fields if given as a string kw['fields'] = strsplit(kw['fields']) if hasattr(self.model_type, '__call__'): model = self.model_type elif self.model_type in models: model = models[self.model_type] else: try: import pyloader model = pyloader.load(self.model_type) except: raise AssertionError("model_type '%s' not found in %s" % (self.model_type, models.keys())) self.model = model(**kw) # add an about view if file specified if self.about: about = file(self.about).read() import docutils.core about = docutils.core.publish_parts(about, writer_name='html')['body'] self.about = about # request handlers in order they will be tried if self.handlers is None: self.handlers = [ TagsView, CreateProjectView, FieldView, QueryView, DeleteProjectHandler, ProjectView ] if self.about: self.handlers.append(AboutView) # extend reserved URLS from handlers if self.reserved is None: self.reserved = set(['css', 'js', 'img']) for handler in self.handlers: if handler.handler_path: self.reserved.add(handler.handler_path[0])
def Post(self): # get some data required = self.app.model.required post_data = self.post_data() # ensure the form isn't over 24 hours old day = 24*3600 form_date = post_data.pop('form-render-date', -day) try: form_date = float(form_date) except ValueError: form_date = -day if abs(form_date - time()) > day: # if more than a day old, don't honor the request return Response(content_type='text/plain', status=400, body="Your form is over a day old or you don't have Javascript enabled") # build up a project dict project = dict([(i, post_data.get(i, '').strip()) for i in required]) # check for errors errors = {} missing = set([i for i in required if not project[i]]) if missing: # missing required fields errors['missing'] = missing # TODO check for duplicate project name # and other url namespace collisions name_conflict = self.check_name(project['name']) if name_conflict: errors[name_conflict] = [project['name']] if errors: error_list = [] for key in errors: # flatten the error dict into a list error_list.extend([(key, i) for i in errors[key]]) return self.redirect(self.request.path_info.strip('/'), error_list) # add fields to the project for field in self.app.model.fields(): value = post_data.get(field, '').strip() values = strsplit(value) if not value: continue project[field] = values or value self.app.model.update(project) return self.redirect(quote(project['name']))
def Post(self): # data post_data = self.post_data() project = self.data['projects'][0] # insist that you have a name if 'name' in post_data and not post_data['name'].strip(): self.data['title'] = 'Rename error' self.data['error'] = 'Cannot give a project an empty name' self.data['content'] = self.render(self.template, **self.data) return Response(content_type='text/html', status=403, body=self.render('main.html', **self.data)) # don't allow overiding other projects with your fancy rename if 'name' in post_data and post_data['name'] != project['name']: if self.app.model.project(post_data['name']): self.data['title'] = '%s -> %s: Rename error' % (project['name'], post_data['name']) self.data['error'] = 'Cannot rename over existing project: <a href="%s">%s</a>' % (post_data['name'], post_data['name'] ) self.data['content'] = self.render(self.template, **self.data) return Response(content_type='text/html', status=403, body=self.render('main.html', **self.data)) # XXX for compatability with jeditable: id = post_data.pop('id', None) action = post_data.pop('action', None) old_name = project['name'] if action == 'delete': for field in self.app.model.fields(): if field in post_data and field in project: values = post_data.pop(field) if isinstance(values, basestring): values = [values] for value in values: project[field].remove(value) if not project[field]: project.pop(field) else: for field in self.app.model.required: if field in post_data: project[field] = post_data[field] for field in self.app.model.fields(): if field in post_data: value = post_data[field] if isinstance(value, basestring): value = strsplit(value) if action == 'replace': # replace the field from the POST request project[field] = value else: # append the items....the default action project.setdefault(field, []).extend(value) # rename handling if 'name' in post_data and post_data['name'] != old_name: self.app.model.delete(old_name) self.app.model.update(project) return self.redirect(quote(project['name'])) self.app.model.update(project) # XXX for compatability with jeditable: if id is not None: return Response(content_type='text/plain', body=cgi.escape(project['description'])) # XXX should redirect instead return self.Get()