def post(self): item = None vals = {} try: # get all the incoming values section = Section.get( self.request.get('section') ) name = self.request.get('name').strip() title = self.request.get('title').strip() content = self.request.get('content') type = self.request.get('type') label_raw = self.request.get('label_raw').strip() attribute_raw = util.make_attr_raw_string( { 'index-entry' : self.request.get('index_entry'), 'has-comments' : self.request.get('has_comments'), 'comments-open' : self.request.get('comments_open'), } ).strip() # some pre-processing of the input params if name == '': name = util.urlify(self.request.get('title')) if self.request.get('key'): item = Page.get( self.request.get('key') ) item.section = section item.name = name item.title = title item.content = content item.type = type item.label_raw = label_raw item.attribute_raw = attribute_raw else: item = Page( section = section, name = name, title = title, content = content, type = type, label_raw = label_raw, attribute_raw = attribute_raw, ) # update and save this page item.set_derivatives() item.put() # once saved, regenerate certain section properties section.regenerate() # also, check that this section doesn't have duplicate content Task( params={ 'section_key': str(section.key()), 'name': item.name }, countdown=30, ).add( queue_name='section-check-duplicate-nodes' ) self.redirect('.') except Exception, err: vals['item'] = self.request.POST vals['err'] = err vals['sections'] = Section.all() vals['types'] = models.type_choices self.template( 'page-form.html', vals, 'admin' );
def post(self): msgs = [] section = None try: section = Section.get( self.request.get('section_key') ) except BadKeyError: # invalid key, try again self.redirect('.') node_type = self.request.get('node_type') data_input = self.request.POST.get('data_input').file.read() data_type = self.request.get('data_type') data = None if data_type == 'json': data = json.loads( data_input ) elif data_type == 'yaml': data = yaml.load( data_input ) else: # someone is messing with the input params self.redirect('.') # logging.info( 'data=' + json.dumps( data ) ) # figure out what this node_type is item = None if node_type == 'page': item = Page( section = section, name = data['name'], title = data['title'], content = data['content'], type = data['type'], label_raw = ' '.join( data['label'] ), attribute_raw = ' '.join( data['attribute'] ), inserted = util.str_to_datetime( data['inserted'] ), updated = util.str_to_datetime( data['updated'] ), ) elif node_type == 'recipe': item = Recipe( section = section, name = data['name'], title = data['title'], intro = data['intro'], serves = data['serves'], ingredients = data['ingredients'], method = data['method'], type = data['type'], label_raw = ' '.join( data['label'] ), attribute_raw = ' '.join( data['attribute'] ), inserted = util.str_to_datetime( data['inserted'] ), updated = util.str_to_datetime( data['updated'] ), ) else: # again, someone is messing with the input params self.redirect('.') # regenerate this item item.set_derivatives() item.put() item.section.regenerate() # output messages msgs.append( 'Added node [%s, %s]' % (item.name, item.title) ) if 'comments' in data: for comment in data['comments']: # load each comment in against this node comment = Comment( node = item, name = comment['name'], email = comment['email'], website = comment['website'], comment = comment['comment'], comment_html = util.render(comment['comment'], 'text'), status = comment['status'], inserted = util.str_to_datetime( comment['inserted'] ), updated = util.str_to_datetime( comment['updated'] ), ) comment.put() msgs.append( 'Added comment [%s, %s]' % (comment.name, comment.email) ) # now that we've added a node, regenerate it item.regenerate() # also, check that this section doesn't have duplicate content Task( params={ 'section_key': str(section.key()), 'name': item.name }, countdown=30, ).add( queue_name='section-check-duplicate-nodes' ) vals = { 'msgs' : msgs, 'node_type' : node_type, 'section_key' : section.key(), 'data_type' : data_type, } self.template( 'load-import.html', vals, 'admin' );