Skip to content

Instantly share code, notes, and snippets.

@binary1230
Created June 9, 2016 02:00
Show Gist options
  • Save binary1230/500bf727858b585e09a94e37e8f6ea30 to your computer and use it in GitHub Desktop.
Save binary1230/500bf727858b585e09a94e37e8f6ea30 to your computer and use it in GitHub Desktop.
ubersystem menu rework idea
import json
# Dom's idea to try and rework the ubersystem menu system so we can append things to it from plugins
# right now, we can't really inject things into the menu from plugins, and I thought this should be an easier thing.
# (mostly, was trying to get some links for the band plugin on the main menu)
#
# TLDR ended up coming up with this idea for how to represent the menu in python, and then passing that as JSON to
# our javascript code which actually creates the HTML elements.
#
# Lemme know what you guys think and if it's worth pursuing I'll continue on with this when I have a few spare minutes
# pretend this is uber's Config() class
class DomsJunkConfig(object):
# fake config data to make this example work
ACCOUNTS = 0
PEOPLE = 1
REG_AT_CON = 2
PEOPLE = 3
WATCHLIST = 4
STUFF = 5
STATS = 6
AT_THE_CON = False
# end fake config data
class MenuItemEncoder(json.JSONEncoder):
def default(self, o):
out = {}
# skip anything that is None
for key,value in o.__dict__.items():
if value:
out[key] = value
return out
@property
def MENU_JSON(self):
return json.dumps(c.MENU, cls=self.MenuItemEncoder, sort_keys=True, indent=2)
c = DomsJunkConfig()
class MenuItem:
access = None # list of permission levels allowed to display this menu
href = None # link to show
submenu = None # submenu to show
def __init__(self, href=None, access=None, submenu=None):
if submenu:
self.submenu = submenu
else:
self.href = href
self.access = access
# in base ubersystem, we represent the menu like this
c.MENU = {
'Accounts': MenuItem(href='../accounts/', access=c.ACCOUNTS),
'People': MenuItem(access=[c.PEOPLE, c.REG_AT_CON], submenu=[
{'Attendees': MenuItem(href='../registration/{}'.format('?invalid=True' if c.AT_THE_CON else ''))},
{'Groups': MenuItem(href='../groups/')},
{'All Untaken Shifts': MenuItem(access=c.PEOPLE, href='../jobs/everywhere')},
{'Jobs': MenuItem(access=c.PEOPLE, href='../jobs/')},
{'Watchlist': MenuItem(access=c.WATCHLIST, href='../registration/watchlist_entries')},
{'Feed of Database Changes': MenuItem(href='../registration/feed')}
]),
'Schedule': MenuItem(access=c.STUFF, submenu=[
{'View Schedule': MenuItem(href='../schedule/')},
{'Edit Schedule': MenuItem(href='../schedule/edit')}
]),
'Statistics': MenuItem(href='../summary/'),
}
# in plugins, we can add stuff to the menus like this:
c.MENU['People'].submenu.append(
{'Bands': MenuItem(href='../registration/feed')}
)
# convert to JSON like this and pass to the templates (should already be passed to templates because it's in 'c')
print(c.MENU_JSON)
@binary1230
Copy link
Author

ignore all of this, I now have a better version here:
magfest/ubersystem#1849

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment