def edit_squad(request, squad_id=None): """Edit an existing squad or create a new one.""" squad = None if squad_id is not None: squad = Squad.query.get(squad_id) if squad is None: raise NotFound() form = EditSquadForm(squad) if request.method == 'POST': if 'cancel' in request.form: return form.redirect('admin/squads') elif request.form.get('delete') and squad: return redirect_to('admin/squads/delete', squad_id=squad.id) elif form.validate(request.form): if squad is None: squad = form.make_squad() msg = _('The squad %s was created successfully.') icon = 'add' else: form.save_changes() msg = _('The squad %s was updated successfully.') icon = 'info' admin_flash(msg % (escape(squad.name)), icon) db.commit() if 'save_and_continue' in request.form: return redirect_to('admin/squads/edit', squad_id=squad.id) return redirect_to('admin/squads') return render_admin_response('admin/squad_edit.html', 'gamesquad.squads', form=form.as_widget())
def forum_edit(request, forum_id=None): """Edit an existing forum or create a new one.""" forum = None if forum_id is not None: forum = Forum.query.get(forum_id) if forum is None: raise NotFound() form = ForumForm(forum) if request.method == 'POST': if 'cancel' in request.form: return form.redirect(forum_endpoints['list']) elif 'delete' in request.form and forum: return redirect_to(forum_endpoints['delete'], forum_id=forum_id) elif form.validate(request.form): if forum is None: forum = form.create_forum() msg = _('The forum %s was created successfully.') icon = 'add' else: form.save_changes(forum) msg = _('The forum %s was updated successfully.') icon = 'info' admin_flash(msg % ('<a href="%s">%s</a>' % (url_for(forum_endpoints['edit'], forum_id=forum.id), escape(forum.name))), icon) if 'save_and_continue' in request.form: return redirect_to(forum_endpoints['edit'], forum_id=forum_id) elif 'save_and_new' in request.form: return redirect_to(forum_endpoints['edit']) return redirect_to(forum_endpoints['list']) return render_admin_response('admin/board_base_edit.html', 'board.forums', form=form.as_widget(), itemname=_('Forum'))
def imaccount_edit(request, account_id=None): """Edit an existing game account or create a new one.""" imaccount = None if account_id is not None: imaccount = IMAccount.query.get(account_id) if imaccount is None: raise NotFound() elif imaccount.user != request.user: raise Forbidden() form = EditIMAccountForm(request.user, imaccount) if request.method == 'POST': if 'cancel' in request.form: return form.redirect('account/imaccount_list') elif request.form.get('delete') and imaccount: return redirect_to('account/imaccount_delete', account_id=account_id) elif form.validate(request.form): if imaccount is None: imaccount = form.make_imaccount() msg = _('IM account %s was added successfully.') icon = 'add' else: form.save_changes() msg = _('IM account %s was updated successfully.') icon = 'info' flash(msg % (escape(imaccount.account)), icon) db.commit() if 'save_and_continue' in request.form: return redirect_to('account/imaccount_edit', account_id=imaccount.id) return form.redirect('account/imaccount_list') return render_account_response('account/imaccount_edit.html', 'imaccounts', form=form.as_widget())
def edit_level(request, level_id=None): """Edit an existing level or create a new one.""" level = None if level_id is not None: level = Level.query.get(level_id) if level is None: raise NotFound() form = EditLevelForm(level) if request.method == 'POST': if 'cancel' in request.form: return form.redirect('admin/levels') elif request.form.get('delete') and level: return redirect_to('admin/levels/delete', level_id=level_id) elif form.validate(request.form): if level is None: level = form.make_level() msg = _('The level %s was created successfully.') icon = 'add' else: form.save_changes() msg = _('The level %s was updated successfully.') icon = 'info' admin_flash(msg % (escape(level.name)), icon) db.commit() if 'save_and_continue' in request.form: return redirect_to('admin/levels/edit', level_id=level.id) return redirect_to('admin/levels') return render_admin_response('admin/level_edit.html', 'levelsquad.levels', form=form.as_widget())
def post_delete(request, post_id): """Delete a post""" post = Post.query.get(post_id) if post is None: raise NotFound() (postnr, page, topic_id) = locate_post(post) user = request.user topic = post.topic if not post.can_delete(user): raise Forbidden() form = DeletePostForm(post) if request.method == 'POST': if form.validate(request.form): if 'confirm' in request.form: forum = topic.forum try: form.delete_post() return redirect_to('board/topic_detail', topic_id=topic.id) except TopicEmpty: return redirect_to('board/topics', forum_id=forum.id) return render_response('board_delete_post.html', topic=topic, post=post, form=form.as_widget())
def edit_game(request, game_id=None): """Edit an existing game or create a new one.""" game = None if game_id is not None: game = Game.query.get(game_id) if game is None: raise NotFound() form = EditGameForm(game) if request.method == 'POST': if 'cancel' in request.form: return form.redirect('admin/games') elif request.form.get('delete') and game: return redirect_to('admin/games/delete', game_id=game.id) elif form.validate(request.form): if game is None: game = form.make_game() msg = _('The game %s was created successfully.') icon = 'add' else: form.save_changes() msg = _('The game %s was updated successfully.') icon = 'info' admin_flash(msg % (escape(game.name)), icon) db.commit() if 'save_and_continue' in request.form: return redirect_to('admin/game_edit', game_id=game.id) return redirect_to('admin/games') return render_admin_response('admin/game_edit.html', 'gamesquad.games', form=form.as_widget())
def edit_group(request, group_id=None): """Edit a Group. This is used to create a group as well.""" group = None if group_id is not None: group = Group.query.get(group_id) if group is None: raise NotFound() form = EditGroupForm(group) if request.method == 'POST': if request.form.get('cancel'): return form.redirect('admin/manage_groups') elif request.form.get('delete') and group: return redirect_to('admin/delete_group', group_id=group.id) elif form.validate(request.form): if group is None: group = form.make_group() msg = _(u'Group %s created successfully.') icon = 'add' else: form.save_changes() msg = _(u'Group %s edited successfully.') icon = 'info' db.commit() html_group_detail = u'<a href="%s">%s</a>' % ( escape(url_for(group)), escape(group.name)) flash(msg % html_group_detail, icon) if request.form.get('save'): return form.redirect('admin/manage_groups') return redirect_to('admin/edit_group', group_id=group.id) return render_admin_response('admin/edit_group.html', 'users_groups.groups', form=form.as_widget())
def warmode_edit(request, warmode_id=None): """Edit an existing warmode or create a new one.""" warmode = None if warmode_id is not None: warmode = WarMode.query.get(warmode_id) if warmode is None: raise NotFound() form = forms.EditWarModeForm(warmode) if request.method == 'POST': if 'cancel' in request.form: return form.redirect('admin/warmodes') elif request.form.get('delete') and warmode: return redirect_to('admin/warmodes/delete', warmode_id=warmode_id) elif form.validate(request.form): if warmode is None: warmode = form.make_warmode() msg = _('Warmode %s was created successfully.') icon = 'add' else: form.save_changes() msg = _('Warmode %s was updated successfully.') icon = 'info' admin_flash(msg % (warmode.name), icon) db.commit() if 'save_and_continue' in request.form: return redirect_to('admin/warmodes/edit', warmode_id=warmode.id) return redirect_to('admin/warmodes') return render_admin_response('admin/warmode_edit.html', 'war.modes', form=form.as_widget())
def war_edit(request, war_id=None): """Edit an existing war or create a new one.""" war = None if war_id is not None: war = War.query.get(war_id) if war is None: raise NotFound() form = forms.EditWarForm(war) if request.method == 'POST': if 'cancel' in request.form: return form.redirect('admin/wars') elif 'result' in request.form: return form.redirect('admin/warresult_edit', war_id=war_id) elif request.form.get('delete') and war: return redirect_to('admin/wars/delete', war_id=war_id) elif form.validate(request.form): if war is None: war = form.make_war() msg = _('The war %s was added successfully.') icon = 'add' else: form.save_changes() msg = _('The war %s was updated successfully.') icon = 'info' admin_flash(msg % (war.clanname), icon) db.commit() if 'save_and_continue' in request.form: return redirect_to('admin/wars/edit', war_id=war.id) return redirect_to('admin/wars') return render_admin_response('admin/war_edit.html', 'war.wars', form=form.as_widget(), memberstates=memberstates, warstates=warstates)
def gameaccount_edit(request, account_id=None): """Edit an existing game account or create a new one.""" gameaccount = None if account_id is not None: gameaccount = GameAccount.query.get(account_id) if gameaccount is None: raise NotFound() form = EditGameAccountForm(request.user, gameaccount) if request.method == 'POST': if 'cancel' in request.form: return form.redirect('account/gameaccounts') elif request.form.get('delete') and gameaccount: return redirect_to('account/gameaccounts/delete', account_id=account_id) elif form.validate(request.form): if gameaccount is None: gameaccount = form.make_gameaccount() msg = _('The game account %s was registered successfully.') icon = 'add' else: form.save_changes() msg = _('The game account %s was updated successfully.') icon = 'info' account_flash(msg % (escape(gameaccount.account)), icon) db.commit() if 'save_and_continue' in request.form: return redirect_to('account/gameaccounts/edit', account_id=gameaccount.id) return redirect_to('account/gameaccounts') return render_account_response('account/gameaccount_edit.html', 'gameaccounts', form=form.as_widget())
def category_edit(request, category_id=None): """Edit an existing category or create a new one.""" category = None if category_id is not None: category = Category.query.get(category_id) if category is None: raise NotFound() form = CategoryForm(category) if request.method == 'POST': if 'cancel' in request.form: return form.redirect(cat_endpoints['list']) elif 'delete' in request.form and category: return redirect_to(cat_endpoints['delete'], category_id=category_id) elif form.validate(request.form): if category is None: category = form.create_category() msg = _('Category %s was created successfully.') icon = 'add' else: form.save_changes(category) msg = _('Category %s was updated successfully.') icon = 'info' admin_flash(msg % ('<a href="%s">%s</a>' % (url_for(cat_endpoints['edit'], category_id=category.id), escape(category.name))), icon) if 'save_and_continue' in request.form: return redirect_to(cat_endpoints['edit'], category_id=category_id) elif 'save_and_new' in request.form: return redirect_to(cat_endpoints['edit']) return redirect_to(cat_endpoints['list']) return render_admin_response('admin/board_base_edit.html', 'board.categories', form=form.as_widget(), itemname=_('Category'))
def edit_user(request, user_id=None): """Edit a user. This can also create a user. If a new user is created the dialog is simplified, some unimportant details are left out. """ user = None if user_id is not None: user = User.query.get(user_id) if user is None: raise NotFound() form = EditUserForm(user) if request.method == 'POST': if request.form.get('cancel'): return form.redirect('admin/manage_users') elif request.form.get('delete') and user: return redirect_to('admin/delete_user', user_id=user.id) elif form.validate(request.form): picfile = request.files.get('picfile') if user is None: user = form.make_user() if picfile and form['userpictype'] == 'Upload': picture.place_file(picfile) msg = _(u'User %s created successfully.') icon = 'add' else: picture = UserPicture(request.user) if picfile: form.save_changes() if form['userpictype'] == 'Upload': picture.place_file(picfile) else: pictype = user.userpictype if not form['userpictype']: form.data['userpictype'] = pictype if form['userpictype'] != pictype: picture.remove() form.save_changes() msg = _(u'User %s edited successfully.') icon = 'info' db.commit() html_user_detail = u'<a href="%s">%s</a>' % ( escape(url_for(user)), escape(user.username) ) flash(msg % html_user_detail, icon) if request.form.get('save'): return form.redirect('admin/manage_users') return redirect_to('admin/edit_user', user_id=user.id) return render_admin_response('admin/edit_user.html', 'users_groups.users', form=form.as_widget())
def configuration(request): """Advanced configuration editor. This is useful for development or if a plugin doesn't ship an editor for the configuration values. Because all the values are not further checked it could easily be that pyClanSphere is left in an unusable state if a variable is set to something bad. Because of this the editor shows a warning and must be enabled by hand. """ form = make_config_form() if request.method == 'POST': if request.form.get('enable_editor'): request.session['ace_on'] = True elif request.form.get('disable_editor'): request.session['ace_on'] = False elif form.validate(request.form): form.apply() flash(_(u'Configuration updated successfully.'), 'configure') return redirect_to('admin/configuration') else: flash(_(u'Could not save the configuration because the ' u'configuration is invalid.'), 'error') return render_admin_response('admin/configuration.html', 'options.configuration', form=form.as_widget(), editor_enabled= request.session.get('ace_on', False))
def profile(request): form = EditProfileForm(request.user) if request.method == 'POST': if 'cancel' in request.form: return form.redirect('account/index') elif 'delete' in request.form: return redirect_to('account/delete') elif form.validate(request.form): picfile = request.files.get('picfile') picture = UserPicture(request.user) if picfile: form.save_changes() picture.place_file(picfile) else: pictype = request.user.userpictype if not form['userpictype']: form.data['userpictype'] = pictype if form['userpictype'] != pictype: picture.remove() form.save_changes() db.commit() flash(_(u'Your profile was updated successfully.'), 'info') return form.redirect('account/index') return render_account_response('account/edit_profile.html', 'profile.profile', form=form.as_widget())
def cache(request): """Configure the cache.""" form = CacheOptionsForm() if request.method == 'POST': if 'clear_cache' in request.form: request.app.cache.clear() flash(_(u'The cache was cleared successfully.'), 'configure') return redirect_to('admin/cache') elif form.validate(request.form): form.apply() flash(_(u'Cache settings were changed successfully.'), 'configure') return redirect_to('admin/cache') return render_admin_response('admin/cache.html', 'options.cache', form=form.as_widget())
def log(request, page): page = request.app.log.view().get_page(page) form = LogOptionsForm() if request.method == 'POST' and form.validate(request.form): form.apply() flash(_('Log changes saved.'), 'configure') return redirect_to('admin/log', page=page.number) return render_admin_response('admin/log.html', 'system.log', page=page, form=form.as_widget())
def category_delete(request, category_id=None): category = Category.query.get(category_id) if category is None: raise NotFound() form = DeleteCategoryForm(category) if request.method == 'POST': if 'cancel' in request.form: return redirect_to(cat_endpoints['edit'], category_id=category_id) if form.validate(request.form) and 'confirm' in request.form: msg = _('The category %s was deleted successfully.') % \ escape(category.name) icon = 'remove' form.delete_category() admin_flash(msg, icon) return redirect_to(cat_endpoints['list']) return render_admin_response('admin/board_base_delete.html', 'board.categories', form=form.as_widget(), itemname=_('Category'), childitemnames=_('Forums'), entryname=category.name)
def topic_by_post(request, post_id): """This function acts as a proxy to find posts by id redirects to proper page if post is found, raise 404 otherwise :URL endpoint: ``board/post_find`` """ post = Post.query.get(post_id) if post is None: raise NotFound() postnr, page, topic_id = locate_post(post) return redirect_to('board/topic_detail', topic_id=topic_id, page=page, _anchor="post-%i" % (post_id,))
def forum_delete(request, forum_id=None): forum = Forum.query.get(forum_id) if forum is None: raise NotFound() form = DeleteForumForm(forum) if request.method == 'POST': if 'cancel' in request.form: return redirect_to(forum_endpoints['edit'], forum_id=forum_id) if form.validate(request.form) and 'confirm' in request.form: msg = _('The forum %s was deleted successfully.') % \ escape(forum.name) icon = 'remove' form.delete_forum() admin_flash(msg, icon) return redirect_to(forum_endpoints['list']) return render_admin_response('admin/board_base_delete.html', 'board.forums', form=form.as_widget(), itemname=_('Forum'), childitemnames=_('Topics'), entryname=forum.name)
def recaptcha(request): """Configure reCAPTCHA form protection.""" form = RecaptchaOptionsForm() if request.method == 'POST': if form.validate(request.form): form.apply() flash(_(u'reCAPTCHA settings were changed successfully.'), 'configure') return redirect_to('admin/recaptcha') return render_admin_response('admin/recaptcha.html', 'options.recaptcha', form=form.as_widget())
def warmap_edit(request, warmap_id=None): """Edit an existing warmap or create a new one.""" warmap = None if warmap_id is not None: warmap = WarMap.query.get(warmap_id) if warmap is None: raise NotFound() form = forms.EditWarMapForm(warmap) if request.method == 'POST': if 'cancel' in request.form: return form.redirect('admin/warmaps') elif request.form.get('delete') and warmap: return redirect_to('admin/warmaps/delete', warmap_id=warmap_id) elif form.validate(request.form): if warmap is None: warmap = form.make_warmap() msg = _('Warmap %s was created successfully.') icon = 'add' else: form.save_changes() msg = _('Warmap %s was updated successfully.') icon = 'info' db.commit() mapfile = request.files.get('mapfile') if mapfile: warmap.place_file(mapfile) if form.overwrite_mapname and hasattr(warmap.metadata, 'name'): warmap.name = unicode(warmap.metadata.name) db.commit() admin_flash(msg % (warmap.name), icon) if 'save_and_continue' in request.form: return redirect_to('admin/warmaps/edit', warmap_id=warmap.id) return redirect_to('admin/warmaps') return render_admin_response('admin/warmap_edit.html', 'war.maps', form=form.as_widget())
def basic_options(request): """The dialog for basic options such as the site title etc.""" # flash an altered message if the url is ?altered=true. For more information # see the comment that redirects to the url below. if request.args.get('altered') == 'true': flash(_(u'Configuration altered successfully.'), 'configure') return redirect_to('admin/basic_options') form = BasicOptionsForm() if request.method == 'POST' and form.validate(request.form): form.apply() # because the configuration page could change the language and # we want to flash the message "configuration changed" in the # new language rather than the old. As a matter of fact we have # to wait for pyClanSphere to reload first which is why we do the # actual flashing after one reload. return redirect_to('admin/basic_options', altered='true') return render_admin_response('admin/basic_options.html', 'options.basic', form=form.as_widget())
def theme(request): """Allow the user to select one of the themes that are available.""" form = ThemeOptionsForm() if request.method == 'GET': if 'configure' in request.args: return redirect_to('admin/configure_theme') elif form.validate(request.args): new_theme = request.args.get('select') if new_theme in request.app.themes: request.app.cfg.change_single('theme', new_theme) flash(_(u'Theme changed successfully.'), 'configure') return redirect_to('admin/theme') return render_admin_response('admin/theme.html', 'options.theme', themes=sorted( request.app.themes.values(), key=lambda x: x.name == 'default' or x.display_name.lower() ), current_theme=request.app.theme, form=form.as_widget() )
def warresult_edit(request, war_id=None): war = None if war_id is not None: war = War.query.get(war_id) if war is None: raise NotFound() else: raise NotFound() warresult = WarResult.query.get(war_id) form = forms.EditWarResultForm(war, warresult) if request.method == 'POST': if request.form.get('cancel'): return form.redirect('admin/wars/edit', war_id=war_id) elif request.form.get('delete'): if warresult is not None: db.delete(warresult) db.commit() admin_flash(_('The result for %s was deleted successfully') % war.clanname, 'remove') return form.redirect('admin/wars/edit', war_id=war_id) elif form.validate(request.form): if warresult is None: warresult = form.make_warresult() msg = _('The result for war %s was created successfully.') icon = 'add' else: form.save_changes() msg = _('The result for war %s was updated successfully.') icon = 'info' admin_flash(msg % (war.clanname), icon) db.commit() if 'save_and_continue' in request.form: return redirect_to('admin/warresult_edit', war_id=war_id) return redirect_to('admin/wars') return render_admin_response('admin/warresult_edit.html', 'war.wars', form=form.as_widget())
def edit_squadmember(request, squad_id=None, user_id=None): """Edit Squadmemberships""" if squad_id is None: raise NotFound() squad = Squad.query.get(squad_id) if squad is None: raise NotFound() squadmember = None if user_id is not None: squadmember = SquadMember.query.get((user_id,squad_id)) if squadmember is None: raise NotFound() form = EditSquadMemberForm(squad, squadmember) if request.method == 'POST': if 'cancel' in request.form: return form.redirect('admin/squadmembers', squad_id=squad_id) elif request.form.get('delete') and user_id is not None: return redirect_to('admin/squadmembers/delete', squad_id=squad_id, user_id=user_id) elif form.validate(request.form): if squadmember is None: squadmember = form.make_squadmember() msg = _('The squadmember %s was created successfully.') icon = 'add' else: form.save_changes() msg = _('The squadmember %s was updated successfully.') icon = 'info' admin_flash(msg % (escape(squadmember.user.display_name)),icon) db.commit() if 'save_and_continue' in request.form: return redirect_to('admin/squadmembers/edit', squad_id=squad_id, user_id=squadmember.user.id) return redirect_to('admin/squadmembers', squad_id=squad_id) return render_admin_response('admin/squad_editmember.html', 'gamesquad.squads', form=form.as_widget(), squad=squad, squadmember=squadmember)
def login(request): """Show a login page.""" if request.user.is_somebody: return redirect_to('account/index') form = LoginForm() if request.method == 'POST' and form.validate(request.form): request.login(form['user'], form['permanent']) if request.user.is_admin: return form.redirect('admin/index') return form.redirect('account/index') return render_response('account/login.html', form=form.as_widget())
def edit_news(request, news_id=None): """Edit an existing entry or create a new one.""" newsitem = None if news_id is not None: newsitem = News.query.get(news_id) if newsitem is None: raise NotFound() form = NewsForm(newsitem) if newsitem is None: assert_privilege(privileges.NEWS_CREATE) else: if not newsitem.can_edit(request.user): raise Forbidden() if request.method == 'POST': if 'cancel' in request.form: return form.redirect('admin/news') elif 'delete' in request.form: return redirect_to('admin/news/delete', news_id=news_id) elif form.validate(request.form): if newsitem is None: newsitem = form.make_news(request.user) msg = _('The entry %s was created successfully.') else: form.save_changes() msg = _('The entry %s was updated successfully.') admin_flash(msg % (escape(newsitem.title))) db.commit() if 'save_and_continue' in request.form: return redirect_to('admin/news_edit', news_id=newsitem.id) return redirect_to('admin/news') return render_admin_response('admin/news_edit.html', 'news.edit', form=form.as_widget())
def maintenance(request): """Enable / Disable maintenance mode.""" cfg = request.app.cfg form = MaintenanceModeForm() if request.method == 'POST' and form.validate(request.form): cfg.change_single('maintenance_mode', not cfg['maintenance_mode']) if not cfg['maintenance_mode']: flash(_(u'Maintenance mode disabled. The site is now ' u'publicly available.'), 'configure') return redirect_to('admin/maintenance') return render_admin_response('admin/maintenance.html', 'system.maintenance', maintenance_mode=cfg['maintenance_mode'], form=form.as_widget() )
def topic_list(request, forum_id, page=1): """Render topics for a given forum Available template variables: `forum`: Forum instance we give details about `pagination`: a pagination object to render a pagination `stickies`: Sorted list of sticky topics for given `forum_id` `topics`: Sorted list of non-sticky topics for given `forum_id` `form`: Form for topic creation or None if user is not allowed to create topics :Template name: ``board_topic_index.html`` :URL endpoint: ``board/topics`` """ forum = Forum.query.get(forum_id) if forum is None: raise NotFound() if not forum.can_see(request.user): raise Forbidden() form = None if forum.can_create_topic(request.user): form = PostForm(forum) if request.method == 'POST': if form.validate(request.form): newpost = form.create_post() return redirect_to('board/topic_detail', topic_id=newpost.topic.id) data = Topic.query.filter(Topic.forum==forum) \ .get_list('board/topics', page, request.per_page, {'forum_id': forum_id}) data['forum'] = forum data['form'] = form.as_widget() if form else None return render_response('board_topic_list.html', **data)
def post_edit(request, post_id): """Render post for editing, re-uses topic detail template Available template variables: `topic`: Topic instance we give details about `pagination`: a pagination object to render a pagination `posts`: Sorted list of posts for given topic `form`: Form for post editing :Template name: ``board_topic_detail.html`` :URL endpoint: ``board/post_edit`` """ post = Post.query.get(post_id) if post is None: raise NotFound() (postnr, page, topic_id) = locate_post(post) user = request.user topic = post.topic if not topic.can_see(user) or not post.can_edit(user): raise Forbidden() form = PostForm(topic,post) del form.title if request.method == 'POST': if form.validate(request.form): form.save_changes(post) return redirect_to('board/post_find', post_id=post.id) data = Post.query.filter(Post.topic==topic)\ .get_list('board/topic_detail', page, request.per_page, {'topic_id': topic_id}) return render_response('board_topic_detail.html', topic=topic, posts=data['posts'], pagination=data['pagination'], form=form.as_widget() if form else None)