-
Notifications
You must be signed in to change notification settings - Fork 0
/
populate_rango.py
92 lines (76 loc) · 2.72 KB
/
populate_rango.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE',
'tango_with_django_project.settings')
import django
django.setup()
from rango.models import Category, Page
def populate():
# First, we will create lists of dictionaries containing the pages
# we want to add into each category.
# Then we will create a dictionary of dictionaries for our categories.
# This might seem a little bit confusing, but it allows us to iterate
# through each data structure, and add the data to our models.
python_pages = [
{"title": "Official Python Tutorial",
"url":"http://docs.python.org/2/tutorial/",
"views":42},
{"title":"How to Think like a Computer Scientist",
"url":"http://www.greenteapress.com/thinkpython/",
"views":16},
{"title":"Learn Python in 10 Minutes",
"url":"http://www.korokithakis.net/tutorials/python/",
"views":76},
]
django_pages = [
{"title":"Official Django Tutorial",
"url":"https://docs.djangoproject.com/en/1.9/intro/tutorial01/",
"views":120,},
{"title":"Django Rocks",
"url":"http://www.djangorocks.com/",
"views":320},
{"title":"How to Tango with Django",
"url":"http://www.tangowithdjango.com/",
"views":22},
]
other_pages = [
{"title":"Bottle",
"url":"http://bottlepy.org/docs/dev/",
"views":100},
{"title":"Flask",
"url":"http://flask.pocoo.org",
"views":60},
]
cats = {"Python": {"pages": python_pages,"views":128,"likes":64,},
"Django": {"pages": django_pages,"views":64,"likes":32,},
"Other Frameworks": {"pages": other_pages,"views":32,"likes":16,} }
# If you want to add more categories or pages,
# add them to the dictionaries above.
# The code below goes through the cats dictionary, then adds each category,
# and then adds all the associated pages for that category.
# if you are using Python 2.x then use cats.iteritems() see
# http://docs.quantifiedcode.com/python-anti-patterns/readability/
# for more information about how to iterate over a dictionary properly.
for cat, cat_data in cats.iteritems():
c = add_cat(cat,views=cat_data["views"],likes=cat_data["likes"])
for p in cat_data["pages"]:
add_page(c, p["title"], p["url"],p["views"])
# Print out the categories we have added.
for c in Category.objects.all():
for p in Page.objects.filter(category=c):
print("- {0} - {1}".format(str(c), str(p)))
def add_page(cat, title, url,views=0):
p = Page.objects.get_or_create(category=cat, title=title)[0]
p.url=url
p.views=views
p.save()
return p
def add_cat(name,views,likes): #views,likes
c = Category.objects.get_or_create(name=name,views=views,likes=likes)[0]
#c.views = views
#c.likes = likes
c.save()
return c
# Start execution here!
if __name__ == '__main__':
print("Starting Rango population script...")
populate()