def prepare_wiki_nav(): # get all visible wiki entries for current user entries = WikiEntry.get_query_for_visible_items(include_hidden_for_user=True) \ .with_entities(WikiEntry.category, WikiEntry.id, WikiEntry.title, WikiEntry.is_visible) \ .order_by(WikiEntry.title.asc()).all() cat_dict = {} # nested touples for categories for entry in entries: # don't include main page, it is added statically if entry[1] == 1: continue if entry[0] is None: cat = "" else: cat = entry[0] if cat not in cat_dict: cat_dict[cat] = [] cat_dict[cat].append(entry[1:4]) return OrderedDict(sorted(cat_dict.items(), key=lambda t: t[0]))
def get_recently_edited(): entries = WikiEntry.get_query_for_visible_items(include_hidden_for_user=True) \ .join(User, WikiEntry.edited_by_id == User.id) \ .with_entities(WikiEntry.id, WikiEntry.title, WikiEntry.edited, User.username) \ .order_by(WikiEntry.edited.desc()).limit(5).all() return entries
def create(): form = WikiEntryForm() form.submit.label.text = "Create Article" cats = gen_category_strings() if form.validate_on_submit(): entry = WikiEntry(title=form.title.data, content=form.content.data, category=form.category.data, tags=form.tags.data, is_visible=form.is_visible.data) db.session.add(entry) db.session.commit() flash("Wiki entry was added.", "success") return redirect(entry.view_url()) elif request.method == "GET": form.is_visible.data = True return render_template("wiki/create.html", form=form, nav=(prepare_wiki_nav(), WikiSearchForm()), cats=cats, title=page_title("Add Wiki Article"))
def gen_wiki_category_choices(): choices = [("", "choose a category")] entries = WikiEntry.get_query_for_visible_items( include_hidden_for_user=True) categories = entries.with_entities(WikiEntry.category).distinct() for cat in categories: if cat[0] != "": choices.append((cat[0], cat[0])) return choices
def gen_category_strings(): entries = WikiEntry.get_query_for_visible_items( include_hidden_for_user=True) entries = entries.with_entities(WikiEntry.category).distinct().all() cats = [] for cat in entries: if cat[0] != '': cats.append(cat[0]) return cats
def set_up_wiki(self): self.wiki_entry1 = WikiEntry(title="Wiki 1", created_by_id=self.admin.id, category="Cat 1") self.wiki_entry2 = WikiEntry(title="Wiki 2", created_by_id=self.admin.id, category="Cat 1", is_visible=False) self.wiki_entry3 = WikiEntry(title="Wiki 3", created_by_id=self.moderator.id, category="Cat 1") self.wiki_entry4 = WikiEntry(title="Wiki 4", created_by_id=self.moderator.id, category="Cat 2", is_visible=False) self.wiki_entry5 = WikiEntry(title="Wiki 5", created_by_id=self.user.id, category="") self.wiki_entry6 = WikiEntry(title="Wiki 6", created_by_id=self.user.id, category="", is_visible=False) self.add_all([self.wiki_entry1, self.wiki_entry2, self.wiki_entry3, self.wiki_entry4, self.wiki_entry5, self.wiki_entry6]) self.commit()
def install(): if not GeneralSetting.query.get(1): form = InstallForm() if form.validate_on_submit(): welcome_msg = "" with open(current_app.config["WELCOME_MD"], "r") as markdown_file: for line in markdown_file: welcome_msg += line settings = GeneralSetting(welcome_page=welcome_msg) db.session.add(settings) calendar_setting = CalendarSetting(finalized=False) map_setting = MapSetting(icon_anchor=0) wiki_setting = WikiSetting() event_setting = EventSetting() media_setting = MediaSetting() db.session.add(calendar_setting) db.session.add(map_setting) db.session.add(wiki_setting) db.session.add(event_setting) db.session.add(media_setting) event_cat = EventCategory(name="Default", color="#000000") media_cat = MediaCategory(name="Default") db.session.add(event_cat) db.session.add(media_cat) if form.default_mapnodes.data: village = MapNodeType(name="Village", description="A small village with not more than 1000 inhabitants", icon_file="village.png", icon_height=35, icon_width=35) town = MapNodeType(name="Town", description="Towns usually have up to 5000 people living in them", icon_file="town.png", icon_height=35, icon_width=35) city = MapNodeType(name="City", description="Cities can have up to 10000 residents", icon_file="city.png", icon_height=35, icon_width=35) capital = MapNodeType(name="Capital", description="Capital city of a country or region", icon_file="capital.png", icon_height=35, icon_width=35) poi = MapNodeType(name="PoI", description="A particular point of interest", icon_file="poi.png", icon_height=35, icon_width=35) quest = MapNodeType(name="Quest", description="An old school quest marker", icon_file="quest.png", icon_height=35, icon_width=35) ruins = MapNodeType(name="Ruins", description="Forgotten and abandoned ruins", icon_file="ruins.png", icon_height=35, icon_width=35) note = MapNodeType(name="Note", description="For additional information", icon_file="note.png", icon_height=35, icon_width=35) # copy files from install dir # TODO: catch exceptions copy_tree(path.join(current_app.config["ROOT_DIR"], "install", "mapnodes"), current_app.config["MAPNODES_DIR"]) db.session.add(village) db.session.add(town) db.session.add(city) db.session.add(capital) db.session.add(poi) db.session.add(quest) db.session.add(ruins) db.session.add(note) flash("8 default location types were added.", "info") db.session.commit() event_setting.default_category = 1 admin = User(username=form.admin_name.data) admin.set_password(form.admin_password.data) admin.role = Role.Admin.value admin.must_change_password = False db.session.add(admin) db.session.commit() wiki_home = WikiEntry(title="Wiki index page", content="Feel free to edit this...", is_visible=True) db.session.add(wiki_home) db.session.commit() flash("Install successful. You can now log in and check the settings.", "success") return redirect(url_for("main.index")) return render_template("install.html", form=form, title="Installation") else: flash("Setup was already executed.", "danger") return redirect(url_for("main.index"))
def sidebar(): entries = WikiEntry.get_query_for_visible_items(include_hidden_for_user=True) \ .with_entities(WikiEntry.id, WikiEntry.title, WikiEntry.is_visible) \ .order_by(WikiEntry.title.asc()).all() return jsonify(entries)