-
Notifications
You must be signed in to change notification settings - Fork 0
/
old.models.py
490 lines (413 loc) · 14.6 KB
/
old.models.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# -*- coding: utf-8 -*-
#!/usr/bin/python
import datetime
from passlib.hash import bcrypt
from faker import Factory
import json
import logging
import logging.handlers
import os
from peewee import *
from playhouse.postgres_ext import *
import shortuuid
from slugify import slugify, Slugify
import unittest
SITES_DIR = "/home/jason/Desktop/muckamuck_shit/sites"
json_dir = "/home/jason/Desktop/muckamuck_shit"
user_json_dir = os.path.join(json_dir, "user")
fake = Factory.create()
sluggy = Slugify(to_lower=True)
def jsonifyer(someDict):
return json.dumps(someDict, sort_keys=True, indent=4, separators=(',', ': '))
####################################################
# Logging Boilerplate
####################################################
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)15s - %(levelname)s - %(message)s')
console_handle = logging.StreamHandler()
console_handle.setFormatter(formatter)
logger.addHandler(console_handle)
LOG_FILENAME = "muchamuck_models.log"
file_handle = logging.handlers.RotatingFileHandler(
LOG_FILENAME, maxBytes=5 * 1024 * 1024, backupCount=5)
file_handle = logging.FileHandler('muchamuck_models.log')
file_handle.setFormatter(formatter)
logger.addHandler(file_handle)
#logger.info('log message')
db = PostgresqlExtDatabase('directorium', user='jason', register_hstore=False)
####################################################
# Base Model
####################################################
class BaseModel(Model):
created_date = DateTimeField(default=datetime.datetime.now)
uuid = CharField(index=True)
class Meta:
database = db
def to_json(self):
return jsonifyer(self.to_dict())
def generate_UUID(self):
self.uuid = shortuuid.ShortUUID().random()
#logger.debug("Generated UUID: " + self.uuid)
####################################################
# User Model
####################################################
class User(BaseModel):
admin = BooleanField(default=False)
email = CharField(index=True, unique=True)
password = CharField(null=True)
public_name = CharField(default="")
public_email = CharField(default="")
def json_location(self):
return os.path.join(user_json_dir, self.uuid + ".json")
def encrypt_password(self, password):
self.password = bcrypt.encrypt(password)
def verify_password(self, password):
return bcrypt.verify(password, self.password)
def to_dict(self):
userDict = {}
userDict["created_date"] = self.created_date.isoformat()
userDict["email"] = self.public_email
userDict["name"] = self.public_name
userDict["uuid"] = self.uuid
return userDict
def dummy(self):
self.generate_UUID()
self.email = fake.free_email()
self.encrypt_password(fake.password())
self.public_name = fake.name()
self.public_email = fake.free_email()
def create_dummy_user():
user = User()
user.generate_UUID()
user.email = fake.free_email()
user.encrypt_password(fake.password())
user.public_name = fake.name()
user.public_email = fake.free_email()
return user
####################################################
# Site Model
####################################################
class Site(BaseModel):
description = CharField(null=True)
domain = CharField(unique=True)
language = CharField(default="en-us")
owner = ForeignKeyField(User)
title = CharField()
def site_dir(self):
return os.path.join(SITES_DIR, self.uuid)
def site_post_dir(self):
return os.path.join(self.site_dir(), "post")
def site_json_dir(self):
return os.path.join(self.site_dir(), "json")
def make_dir(self):
if not os.path.exists(self.site_dir()):
os.makedirs(self.site_dir())
if not os.path.exists(self.site_post_dir()):
os.makedirs(self.site_post_dir())
if not os.path.exists(self.site_json_dir()):
os.makedirs(self.site_json_dir())
def to_dict(self):
siteDict = {}
siteDict["created_date"] = self.created_date.isoformat()
siteDict['description'] = self.description
siteDict['domain'] = self.domain
siteDict['language'] = self.language
siteDict['owner'] = self.owner.to_dict()
siteDict['title'] = self.title
siteDict['uuid'] = self.uuid
return siteDict
def dummy(self, owner):
self.generate_UUID()
self.description = fake.text(max_nb_chars=200)
self.domain = fake.domain_name()
self.language = fake.locale()
self.owner = owner
self.title = fake.company()
def create_dummy_site(owner):
site = Site()
site.generate_UUID()
site.description = fake.text(max_nb_chars=200)
site.domain = fake.word() + "_" + fake.word() + "_" + fake.word() + ".muckamuck.net"
site.language = fake.locale()
site.owner = owner
site.title = fake.company()
return site
def get_random_site():
site = Site.select().order_by(fn.Random()).limit(1)[0]
return site
####################################################
# Theme Model
####################################################
class Theme(BaseModel):
site = ForeignKeyField(Site, unique=True)
template = TextField()
def create_dummy_theme(site):
theme = Theme()
theme.generate_UUID()
theme.site = site
file_object = open("render_templates/dummy_theme.html", "r")
theme.template = file_object.read()
return theme
####################################################
# Membership Model
####################################################
class Membership(BaseModel):
site = ForeignKeyField(Site, unique=True)
user = ForeignKeyField(User)
class Meta:
indexes = (
(('site', 'user'), True),
)
def check_membership(site, user):
try:
membership = Membership.get( (Membership.site == site) & (Membership.user == user))
except Membership.DoesNotExist:
return False
return True
def make_member(site, user):
membership = Membership()
membership.generate_UUID()
membership.site = site
membership.user = user
return membership
####################################################
# Page Model
####################################################
class Page(BaseModel):
author = ForeignKeyField(User)
body = TextField()
description = CharField()
slug = CharField()
site = ForeignKeyField(Site)
title = CharField()
class Meta:
indexes = (
(('site', 'slug'), True),
)
def to_dict(self):
pageDict = {}
pageDict["created_date"] = self.created_date.isoformat()
pageDict['author'] = self.author.to_dict()
pageDict['body'] = self.body
pageDict['site'] = self.site.to_dict()
pageDict['slug'] = self.slug
pageDict['title'] = self.title
pageDict['uuid'] = self.uuid
return pageDict
def create_dummy_page(site, author):
page = Page()
page.generate_UUID()
page.author = author
page.body = fake.paragraph(nb_sentences=5, variable_nb_sentences=True)
page.site = site
page.title = fake.words(nb=3)
page.slug = sluggy(page.title)
return page
####################################################
# Post Model
####################################################
class Post(Page):
#author = ForeignKeyField(User)
#body = TextField()
#description = CharField()
#slug = CharField()
#site = ForeignKeyField(Site)
tags = ArrayField(CharField)
#title = CharField()
def to_dict(self):
postDict = {}
postDict["created_date"] = self.created_date.isoformat()
postDict['author'] = self.author.to_dict()
postDict['body'] = self.body
postDict['description'] = self.description
postDict['site'] = self.site.to_dict()
postDict['slug'] = self.slug
postDict['tags'] = self.tags
postDict['title'] = self.title
postDict['uuid'] = self.uuid
return postDict
def dummy(self, site, author):
self.generate_UUID()
self.author = author
self.body = fake.paragraph(nb_sentences=15, variable_nb_sentences=True)
self.description = fake.text(max_nb_chars=200)
self.site = site
self.title = fake.word() + "_" + fake.company() + " " + fake.word() + " " + fake.word()
self.slug = sluggy(self.title)
self.tags = fake.words(nb=3)
self.tags.append("tag")
def json_path(self):
return os.path.join(self.site.site_json_dir(), self.slug + ".json")
def write_json(self):
file_object = open(self.post_json_path(), "wb")
file_object.write(self.to_json())
file_object.close()
def create_dummy_post(site, author):
post = Post()
post.generate_UUID()
post.author = author
post.body = fake.paragraph(nb_sentences=5, variable_nb_sentences=True)
post.description = fake.text(max_nb_chars=200)
post.site = site
post.title = fake.word() + "_" + fake.company() + " " + fake.word() + " " + fake.word()
post.slug = sluggy(post.title)
post.tags = fake.words(nb=3)
post.tags.append("tag")
return post
def get_random_post_from_site(site_uuid):
site = Site.select().where(Site.uuid == site_uuid).get()
post = Post.select().where(Post.site == site).order_by(fn.Random()).limit(1)[0]
return post
####################################################
# Tags
####################################################
def get_site_tags(uuid):
site = Site.select().where(Site.uuid == uuid).get()
tag_set = set()
for post in Post.select().where(Post.site == site).iterator():
for tag in post.tags:
tag_set.add(tag)
return tag_set
def reset_db():
db.drop_tables([User, Site, Page, Post, Membership, Theme])
db.create_tables([User, Site, Page, Post, Membership, Theme])
####################################################
# Tests
####################################################
#create_dummy_user().save()
class FooTest(unittest.TestCase):
def setUp(self):
db.connect()
try:
db.drop_tables([User, Site, Page, Post, Membership, Theme])
except:
pass
db.create_tables([User, Site, Page, Post, Membership, Theme])
# ending the test
def tearDown(self):
pass
####################################################
# Membership Tests
####################################################
def test_Check_Membership(self):
user = create_dummy_user()
user.save()
site = create_dummy_site(user)
site.save()
membership = make_member(site, user)
membership.save()
self.assertTrue(check_membership(site, user))
def test_Check_No_Membership(self):
user = create_dummy_user()
user.save()
site = create_dummy_site(user)
site.save()
self.assertFalse(check_membership(site, user))
def test_Duplicate_Membership(self):
user = create_dummy_user()
user.save()
site = create_dummy_site(user)
site.save()
membership1 = make_member(site, user)
membership1.save()
with self.assertRaises(IntegrityError):
membership2 = make_member(site, user)
membership2.save()
####################################################
# Page Tests
####################################################
def test_Count_Page(self):
user = create_dummy_user()
user.save()
site = create_dummy_site(user)
site.save()
for i in range(10):
page = create_dummy_page(site, user)
page.save()
#print page.to_json()
self.assertEqual(Page.select().count(), 10)
####################################################
# Post Tests
####################################################
def test_Count_Page(self):
user = create_dummy_user()
user.save()
site = create_dummy_site(user)
site.save()
for i in range(10):
post = Post()
post.dummy(site, user)
post.save()
#print post.to_json()
self.assertEqual(Post.select().count(), 10)
####################################################
# Site Tests
####################################################
def test_Count_Sites(self):
user = create_dummy_user()
user.save()
for i in range(10):
site = create_dummy_site(user)
site.save()
#print site.to_json()
self.assertEqual(Site.select().count(), 10)
####################################################
# User Tests
####################################################
def test_Count_Users(self):
for i in range(10):
user = create_dummy_user()
user.save()
#print user.to_json()
self.assertEqual(User.select().count(), 10)
def test_Create_User(self):
user_email = fake.free_email()
user = User()
user.generate_UUID()
original_uuid = user.uuid
user.email = user_email
user.save()
user2 = User.get(User.email == user_email)
self.assertEqual(user2.uuid, user.uuid)
def test_Create_Duplicate_User(self):
user_email = fake.free_email()
user1 = User()
user1.generate_UUID()
user1.email = user_email
user1.save()
user2 = User()
user2.generate_UUID()
user2.email = user_email
with self.assertRaises(IntegrityError):
user2.save()
def test_User_Password(self):
user_email = fake.free_email()
user_password = fake.password()
user = User()
user.email = user_email
user.encrypt_password(user_password)
user.generate_UUID()
user.save()
self.assertTrue(user.verify_password(user_password))
def test_User_BlankPassword(self):
user_email = fake.free_email()
user_password = fake.password()
user = User()
user.email = user_email
user.generate_UUID()
user.save()
with self.assertRaises(TypeError):
user.verify_password(user_password)
def test_User_BadPassword(self):
user_email = fake.free_email()
user_password = fake.password()
user_BADpassword = fake.password()
user = User()
user.email = user_email
user.encrypt_password(user_password)
user.generate_UUID()
user.save()
self.assertFalse(user.verify_password(user_BADpassword))
#if __name__ == '__main__':
# unittest.main()