Advertisement
Guest User

Untitled

a guest
Jan 12th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.41 KB | None | 0 0
  1. import os
  2. from selectors import _PollLikeSelector
  3. os.environ.setdefault('DJANGO_SETTINGS_MODULE',
  4.                       'tango_with_django_project.settings')
  5. # need to set path and import settings (via setup() below)
  6. import django
  7. django.setup()
  8. from rango.models import Category, Page
  9. # must import specifics (cat, page) after import gen settings (preceding line)
  10.  
  11. def populate():
  12.     #Create lists of dicts of pages
  13.     #Then dict of dicts for categories
  14.     #Allows iteration through each data structure
  15.    
  16.     python_pages = [
  17.         {"title": "Official Python Tutorial",
  18.          "url":"https://docs.python.org/3/tutorial/"},
  19.         #changed to py3 url
  20.         #clipping part after last / from all urls (i.e. "index.html"
  21.         {"title":"How to Think like a Computer Scientist",
  22.          "url":"http://www.greenteapress.com/thinkpython/"},
  23.         {"title":"Learn Python in 10 Minutes",
  24.          "url":"https://www.stavros.io/tutorials/python/"},  ]
  25.         #url changed from book
  26.        
  27.     django_pages = [
  28.         {"title":"Official Django Tutorial",
  29.          "url":"https://docs.djangoproject.com/en/2.1/intro/tutorial01/"},
  30.         {"title":"Django Rocks",
  31.          "url":"http://djangorocks.com"},
  32.         {"title":"How to Tango with Django",
  33.          "url":"http://www.tangowithdjango.com/"}   ]
  34.    
  35.     other_pages = [
  36.         {"title":"Bottle",
  37.          "url":"http://bottlepy.org/docs/dev/"},
  38.         {"title":"Flask",
  39.          "url":"http://flask.pocoo.org"}    ]
  40.    
  41.     cats = [
  42.         {"category": "Python",
  43.          "pages": {"pages": python_pages},
  44.          "views":128,
  45.          "likes":64},
  46.         {"category": "Django",
  47.          "pages": {"pages": django_pages},
  48.          "views":64,
  49.          "likes":32},
  50.         {"category": "Other Frameworks",
  51.          "pages": {"pages": other_pages},
  52.          "views":32,
  53.          "likes":16}    ]
  54.     # if I use curly brackets to make dict of dicts like original, get "unhashable type" error
  55.    
  56.     # goes through cat dict, adds each, adds assocaiated pages for each cat
  57.     # changed cats.items() to just cats:
  58.     for dicts in cats:
  59.         cat = dicts["category"]
  60.         catpages = dicts["pages"]
  61.         views = dicts["views"]
  62.         likes = dicts["likes"]
  63.        # python = dicts["Python"]
  64.         #printcheck print(cat, catpages, views, likes)
  65.         # so far is working
  66.         c = add_cat(cat, catpages, views, likes)
  67.         # this works minus the for p stuff
  68.         for pages in catpages:
  69.             cat_data = catpages["pages"]
  70.             for p in cat_data["pages"]:
  71.                 add_page(c, p["title"], p["url"])
  72.    
  73.     #print check
  74.     for c in Category.objects.all():
  75.         for p in Page.objects.filter(category=c):
  76.             print("- {0} - {1} - {2}".format(str(c), c.likes, str(p)))
  77.             #bracketed numbers are placeholders for later outputs (str c and p)
  78.            # c.likes gives zero for all
  79.            
  80. def add_page(cat, title, url, views=0):
  81.     p = Page.objects.get_or_create(category=cat, title=title)[0]
  82.     p.url=url
  83.     p.views=views
  84.     p.save()
  85.     return p
  86.  
  87. def add_cat(cat, catpages, views, likes):
  88.     c = Category.objects.get_or_create(name=cat)[0]
  89.     c.views = views
  90.     c.likes = likes
  91.     c.save()
  92.     return c
  93.  
  94. #this if block is trick to act as reusable module or script
  95. if __name__ == '__main__':
  96.     print("Starting Rango population script ... ")
  97.     populate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement