Skip to content

Instantly share code, notes, and snippets.

@hcwiley
Last active December 10, 2015 15:39
Show Gist options
  • Save hcwiley/4455877 to your computer and use it in GitHub Desktop.
Save hcwiley/4455877 to your computer and use it in GitHub Desktop.
makes routes for ui and api for models based off a baseModel.

makes views for models based off baseModel. useful when using http://github.com/jwietelmann/express3_boilerplate

if you have

  • models/department.js
  • routes/ui/departments.js
  • routes/api/departments.js
  • views/departments/index.jade

and

  • models/area.js
  • models/assistant.js
  • models/..

and you want them to have the same contents as departments routes and views, but with the var names changed correctly.

run from parent dir.

$ python routeMaker.py
$ python viewMaker.py
import os, sys
models = ['area', 'assistant', 'award', 'center', 'grant', 'patent', 'publication', 'research']
baseModel = 'department'
os.chdir('routes/ui')
for model in models:
f = open("%ss.js" % baseModel)
newLines = []
for line in f.readlines():
line = line.replace("%s" % baseModel, "%s" % model)
line = line.replace("%s" % baseModel.title(), "%s" % model.title())
newLines.append(line.replace("%s" % baseModel,"%s" % model))
f.close()
f = open("%ss.js" % model, 'w')
for line in newLines:
f.write(line)
f.flush()
f.close()
os.chdir('../api')
for model in models:
f = open("%ss.js" % baseModel)
newLines = []
for line in f.readlines():
line = line.replace("%s" % baseModel, "%s" % model)
line = line.replace("%s" % baseModel.title(), "%s" % model.title())
newLines.append(line.replace("%s" % baseModel,"%s" % model))
f.close()
f = open("%ss.js" % model, 'w')
for line in newLines:
f.write(line)
f.flush()
f.close()
import os, sys
models = ['area', 'assistant', 'award', 'center', 'grant', 'patent', 'publication', 'research']
baseModel = "department"
for model in models:
f = open('views/%ss/index.jade' % baseModel)
newLines = []
for line in f.readlines():
newLines.append(line.replace("department","%s" % model))
f.close()
try:
os.mkdir("views/%ss" % model)
except:
print("oh well, already there")
f = open("views/%ss/index.jade" % model, 'w')
for line in newLines:
f.write(line)
f.flush()
f.close()
f = open('views/%ss/show.jade' % baseModel)
newLines = []
for line in f.readlines():
newLines.append(line.replace("department","%s" % model))
f.close()
f = open("views/%ss/show.jade" % model, 'w')
for line in newLines:
f.write(line)
f.flush()
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment