def create_section(handler, profile, section, original_template=""): if section == 'main': return section title = section section = url_factory.clean_name(section) if section in handler.constants['reserved_names']: handler.set_status(400) handler.write("dup-section") raise tornado.web.HTTPError(400) section_item = handler.models.content.get(username=profile, section='main', name=section)[0] if not section_item: new_section = handler.models.content() new_section.username = profile new_section.section = 'main' section = get_unique_name(handler, content=new_section, title=title) new_section.name = section new_section.title = title new_section.template = original_template new_section.date_created = datetime.datetime.utcnow() new_section.date_updated = datetime.datetime.utcnow() new_section.save() handler.set_header('X-Helloworld-Section', section) return section
def get_unique_name(handler, content, name="_", title=None): if not title: title = handler.locale.translate('untitled') # check to see if name is unique if name and name != '_': name = url_factory.clean_name(name) existing_content = handler.models.content.get( username=content.username, name=name)[0] if ((existing_content and existing_content.id != content.id) or name in handler.constants['reserved_names']): handler.set_status(400) handler.write("dup-name") raise tornado.web.HTTPError(400) else: name = url_factory.clean_name(title) new_name = name counter = 1 options = {'username': content.username, 'name like': new_name + '-%'} highest_existing_name = handler.models.content.get(**options).order_by( 'id', 'DESC')[0] if highest_existing_name: existing_counter = highest_existing_name.name.split('-')[1] try: counter = int(existing_counter) + 1 new_name = name + '-' + str(counter) except: pass # if existing_counter is not a possible number while True: existing_content = handler.models.content.get( username=content.username, name=new_name)[0] if existing_content or new_name in handler.constants[ 'reserved_names']: new_name = name + '-' + str(counter) counter += 1 else: name = new_name break return name
def get_unique_name(handler, content, name="_", title=None): if not title: title = handler.locale.translate('untitled') # check to see if name is unique if name and name != '_': name = url_factory.clean_name(name) existing_content = handler.models.content.get(username=content.username, name=name)[0] if ((existing_content and existing_content.id != content.id) or name in handler.constants['reserved_names']): handler.set_status(400) handler.write("dup-name") raise tornado.web.HTTPError(400) else: name = url_factory.clean_name(title) new_name = name counter = 1 options = { 'username': content.username, 'name like': new_name + '-%' } highest_existing_name = handler.models.content.get(**options).order_by( 'id', 'DESC')[0] if highest_existing_name: existing_counter = highest_existing_name.name.split('-')[1] try: counter = int(existing_counter) + 1 new_name = name + '-' + str(counter) except: pass # if existing_counter is not a possible number while True: existing_content = handler.models.content.get(username=content.username, name=new_name)[0] if existing_content or new_name in handler.constants['reserved_names']: new_name = name + '-' + str(counter) counter += 1 else: name = new_name break return name
def rename_section(handler, old_name, new_name, new_title): new_name = url_factory.clean_name(new_name) profile = handler.get_author_username() section_item = handler.models.content.get(username=profile, section='main', name=old_name)[0] section_item.name = new_name section_item.title = new_title section_item.save() collection = handler.models.content.get(username=profile, section=old_name)[:] for item in collection: item.section = new_name item.save() create_redirect(handler, section_item, 'main', old_name) return new_name
def create_album(handler, profile, section, album, original_template=""): if not album: return "" if album == 'main': return album title = album album = url_factory.clean_name(album) if album in handler.constants['reserved_names']: handler.set_status(400) handler.write("dup-album") raise tornado.web.HTTPError(400) album_item = handler.models.content.get(username=profile, section=section, album='main', name=album)[0] parent_section = handler.models.content.get(username=profile, section='main', name=section)[0] if not album_item: new_album = handler.models.content() new_album.username = profile new_album.section = section new_album.album = 'main' album = get_unique_name(handler, content=new_album, title=title) new_album.name = album new_album.title = title new_album.hidden = parent_section.hidden new_album.template = original_template new_album.date_created = datetime.datetime.utcnow() new_album.date_updated = datetime.datetime.utcnow() new_album.save() handler.set_header('X-Helloworld-Album', album) return album