forked from cc-d/ieddit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_db.py
163 lines (141 loc) · 5.75 KB
/
create_db.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 logging
from flask import Flask, render_template, session, request
from flask_sqlalchemy import SQLAlchemy
from werkzeug.security import generate_password_hash, check_password_hash
from ieddit import db
from models import *
from functions import *
import os
import string
from random import randint, choice
from faker import Faker
import json
import config
import psycopg2
import os
fake = Faker()
if config.DB_TYPE == 'postgres':
if config.USE_RECREATE == False:
conn = psycopg2.connect(host=config.PG_HOST, database=config.DATABASE, user=config.PG_USER, password=config.PG_PASSWORD)
conn.set_session(autocommit=True)
cur = conn.cursor()
logging.info('connected to {0} on database {1}'.format(config.PG_HOST, config.DATABASE))
cur.execute("DROP SCHEMA public CASCADE;")
cur.execute("CREATE SCHEMA public;")
# commented queries below throw errors about not existing
#cur.execute("GRANT ALL ON SCHEMA public TO postgres;")
cur.execute("GRANT ALL ON SCHEMA public TO public;")
cur.execute("COMMENT ON SCHEMA public IS 'standard public schema';")
#cur.execute("CREATE USER test WITH PASSWORD 'test';")
#cur.execute("ALTER SCHEMA public OWNER to postgres;")
logging.info('Succesfully provisioned database')
else:
os.system('bash recreate_psql_db.sh')
elif config.DB_TYPE == 'sqlite':
try:
os.remove('{0}.db'.format(config.PG_USER))
except FileNotFoundError:
pass
# force clear user sessions by changing key
with open('config.py', 'r+') as f:
ctext = f.read()
ctext = ctext.split('|r|')
ctext[1] = rstring(10)
f.seek(0)
f.write('|r|'.join(ctext))
db.create_all()
db.session.commit()
new_user = Iuser(username='test', email='[email protected]',
password=generate_password_hash('test'))
db.session.add(new_user)
db.session.commit()
new_user = Iuser(username='Anonymous', email='[email protected]',
password=generate_password_hash('test'))
db.session.add(new_user)
db.session.commit()
new_user = Iuser(username='a', email='[email protected]',
password=generate_password_hash('a'), admin=True, anonymous=True)
db.session.add(new_user)
db.session.commit()
for i in range(20):
new_user = Iuser(username=rstring(3,10), email= rstring(4) + '@test.com',
password=generate_password_hash('test'))
db.session.add(new_user)
db.session.commit()
new_sub = Sub(name='test', created_by='test', created_by_id=1, title='for testing')
db.session.add(new_sub)
db.session.commit()
new_mod = Moderator(username='test', sub='test')
db.session.add(new_mod)
db.session.commit()
for i in range(10):
new_sub = Sub(name=rstring(3, 10), created_by='test', created_by_id=1)
db.session.add(new_sub)
db.session.commit()
new_mod = Moderator(username=new_sub.created_by, sub=new_sub.name)
db.session.add(new_mod)
db.session.commit()
new_post = Post(url='https://google.com', title='Test Title', inurl_title=convert_ied('Test Title'),
author='test', author_id=1, sub='test', ups=randint(100,200), downs=randint(1,5), post_type='url',
author_type='mod', stickied=True)
db.session.add(new_post)
db.session.commit()
new_post.permalink = config.URL + '/i/' + new_post.sub + '/' + str(new_post.id) + '/' + new_post.inurl_title + '/'
db.session.commit()
test_perma = new_post.permalink
fp = new_post
for i in range(50):
title = fake.text()[:randint(10,200)]
new_post = Post(url='https://google.com/' + rstring(5, 10), title=title, inurl_title=convert_ied(title),
author='test', author_id=1, sub='test', ups=randint(1,20), downs=randint(1,5), post_type='url', author_type='mod')
db.session.add(new_post)
db.session.commit()
new_post.permalink = config.URL + '/i/' + new_post.sub + '/' + str(new_post.id) + '/' + new_post.inurl_title + '/'
db.session.commit()
for i in range(50):
title = fake.text()[:randint(10,200)]
new_post = Post(title=title, inurl_title=convert_ied(title), self_text=pseudo_markup(fake.text(2000))[:randint(500,2000)],
author='test', author_id=1, sub='test', ups=randint(1,20), downs=randint(1,5), post_type='self_post', author_type='mod')
db.session.add(new_post)
db.session.commit()
new_post.permalink = config.URL + '/i/' + new_post.sub + '/' + str(new_post.id) + '/' + new_post.inurl_title + '/'
db.session.commit()
db.session.commit()
new_comment = Comment(post_id=1, sub_name='test', text='this is comment text', author='test', author_id=1, ups=randint(1,20), downs=randint(1,5),
author_type='mod')
db.session.add(new_comment)
db.session.commit()
post = db.session.query(Post).filter_by(id=1).first()
new_comment.permalink = post.permalink + str(new_comment.id)
db.session.commit()
new_comment = Comment(post_id=1, sub_name='test', text='this is a reply', author='test', author_id=1, parent_id=1, ups=randint(1,20),
downs=randint(1,5), author_type='mod')
db.session.add(new_comment)
db.session.commit()
post = db.session.query(Post).filter_by(id=1).first()
new_comment.permalink = post.permalink + str(new_comment.id)
db.session.commit()
comments = list(Comment.query.all())
for i in range(200):
if choice([x for x in range(3)]) == 0:
pid = None
level = None
else:
rancom = choice(comments)
pid = rancom.id
level = rancom.level + 1
new_comment = Comment(post_id=1, sub_name='test', text=pseudo_markup(fake.text()[:randint(1,200)]), author='test', author_id=1,
parent_id=pid, level=level, ups=randint(1,20), downs=randint(1,5), author_type='mod')
db.session.add(new_comment)
db.session.commit()
post = db.session.query(Post).filter_by(id=1).first()
new_comment.permalink = post.permalink + str(new_comment.id)
db.session.commit()
comments.append(new_comment)
new_message = Message(sent_to='a', sender='test', title='this is a title', text='this is text')
db.session.add(new_message)
db.session.commit()
new_message = Message(sent_to='a', sender='test', title='this is a title', text='this is text', in_reply_to=fp.permalink)
db.session.add(new_message)
db.session.commit()
db.session.commit()