-
Notifications
You must be signed in to change notification settings - Fork 0
/
populate_rango.py
163 lines (140 loc) · 4.77 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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():
# Lists of dictionaries containing pages to be
# added to each category
python_pages = [
{"title":"Official Python Tutorial",
"url":"http://docs.python.org/2/tutorial/",
"views": 1024},
{"title":"How to think like a Computer Scientist",
"url":"http://www.greenteapress.com/thinkpython/",
"views": 512},
{"title":"Learn Python in 10 Minutes",
"url":"http://www.korokithakis.net/tutorials/python/",
"views": 64}
]
django_pages = [
{"title":"Official Django Tutorial",
"url":"https://docs.djangoproject.com/en/1.9/intro/tutorial01/",
"views": 768},
{"title":"How to Tango with Django",
"url":"http://www.tangowithdjango.com/",
"views": 256}
]
other_pages = [
{"title":"Bottle",
"url":"http://bottlepy.org/docs/dev/",
"views": 8},
{"title":"Flask",
"url":"http://flask.pocoo.org",
"views": 32}
]
other_pages = [
{"title":"Bottle",
"url":"http://bottlepy.org/docs/dev/",
"views": 8},
{"title":"Flask",
"url":"http://flask.pocoo.org",
"views": 32}
]
pascal_pages = [
{"title":"Pascal tutorial for beginners",
"url":"http://www.pascal-programming.info/index.php",
"views": 4},
{"title":"Free Pascal",
"url":"http://www.freepascal.org/advantage.var",
"views": 6}
]
prolog_pages = [
{"title":"SWI-Prolog",
"url":"http://www.swi-prolog.org/",
"views": 16},
{"title":"Visual Prolog",
"url":"http://www.visual-prolog.com/default.htm",
"views": 12}
]
postscript_pages = [
{"title":"Postscript Tutorial",
"url":"http://paulbourke.net/dataformats/postscript/",
"views": 78},
{"title":"Adobe Postscript",
"url":"http://www.adobe.com/products/postscript.html",
"views": 32}
]
php_pages = [
{"title":"PHP",
"url":"http://php.net/",
"views": 291},
{"title":"PHP Tutorial",
"url":"http://www.w3schools.com/php/",
"views": 60}
]
programming_pages = [
{"title":"Programming - Wikipedia",
"url":"http://en.wikipedia.org/wiki/Programming/",
"views": 512},
]
perl_pages = [
{"title":"The perl programming language",
"url":"http://www.perl.org/",
"views": 8},
]
# Dictionary of dictionaries for our categories
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},
"Pascal": {"pages": pascal_pages,
"views": 8,
"likes": 4},
"Prolog": {"pages": prolog_pages,
"views": 4,
"likes": 2},
"Perl": {"pages": perl_pages,
"views": 48,
"likes": 5},
"PHP": {"pages": php_pages,
"views": 256,
"likes": 128},
"PostScript": {"pages": postscript_pages,
"views": 32,
"likes": 8},
"Programming": {"pages": programming_pages,
"views": 2048,
"likes": 1024}
}
# Iterate through the cats dictionary adding each category,
# and then add the pages associated to that category
for cat, cat_data in cats.items():
c = add_cat(cat, cat_data["views"], 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=0, likes=0):
c = Category.objects.get_or_create(name=name)[0]
c.views=views
c.likes=likes
c.save()
return c
if __name__ == '__main__':
print("Starting Rango population script...")
populate()