1
|
|
|
from django.contrib.auth import get_user_model |
2
|
|
|
|
3
|
|
|
from simple_forums import models |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
def create_message(**kwargs): |
7
|
|
|
""" Create a message instance with the given arguments. |
8
|
|
|
|
9
|
|
|
Since this method is used for testing, it will create default |
10
|
|
|
values to pass to the instance. |
11
|
|
|
""" |
12
|
|
|
|
13
|
|
|
user = kwargs.pop('user', None) |
14
|
|
|
thread = kwargs.pop('thread', None) |
15
|
|
|
body = kwargs.pop('body', 'test body') |
16
|
|
|
time_created = kwargs.pop('time_created', None) |
17
|
|
|
|
18
|
|
|
if len(kwargs) > 0: |
19
|
|
|
raise ValueError("Received unexpected kwargs: %s" % kwargs) |
20
|
|
|
|
21
|
|
|
if user is None: |
22
|
|
|
if get_user_model().objects.filter(username='test').exists(): |
23
|
|
|
user = get_user_model().objects.get(username='test') |
24
|
|
|
else: |
25
|
|
|
user = get_user_model().objects.create_user( |
26
|
|
|
username='test', |
27
|
|
|
password='test') |
28
|
|
|
|
29
|
|
|
if thread is None: |
30
|
|
|
thread = create_thread() |
31
|
|
|
|
32
|
|
|
message_kwargs = dict( |
33
|
|
|
user=user, |
34
|
|
|
thread=thread, |
35
|
|
|
body=body) |
36
|
|
|
|
37
|
|
|
# Since this field has a default value defined in the model itself, |
38
|
|
|
# don't pass it if its value is None |
39
|
|
|
if time_created is not None: |
40
|
|
|
message_kwargs['time_created'] = time_created |
41
|
|
|
|
42
|
|
|
return models.Message.objects.create(**message_kwargs) |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
def create_thread(**kwargs): |
46
|
|
|
""" Create a thread instance for testing. |
47
|
|
|
|
48
|
|
|
Fills in default values for testing purposes. |
49
|
|
|
""" |
50
|
|
|
|
51
|
|
|
topic = kwargs.pop('topic', None) |
52
|
|
|
title = kwargs.pop('title', 'test thread') |
53
|
|
|
sticky = kwargs.pop('sticky', None) |
54
|
|
|
time_created = kwargs.pop('time_created', None) |
55
|
|
|
|
56
|
|
|
if len(kwargs) > 0: |
57
|
|
|
raise ValueError("Received unexpected kwargs: %s" % kwargs) |
58
|
|
|
|
59
|
|
|
if topic is None: |
60
|
|
|
topic = create_topic() |
61
|
|
|
|
62
|
|
|
thread_kwargs = dict( |
63
|
|
|
topic=topic, |
64
|
|
|
title=title) |
65
|
|
|
|
66
|
|
|
# Since these fields have a default value defined in the model |
67
|
|
|
# itself, don't pass them if their value is None |
68
|
|
|
if sticky is not None: |
69
|
|
|
thread_kwargs['sticky'] = sticky |
70
|
|
|
|
71
|
|
|
if time_created is not None: |
72
|
|
|
thread_kwargs['time_created'] = time_created |
73
|
|
|
|
74
|
|
|
# Create the thread with the given parameters |
75
|
|
|
return models.Thread.objects.create(**thread_kwargs) |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
def create_topic(**kwargs): |
79
|
|
|
""" Create topic instance for testing. |
80
|
|
|
|
81
|
|
|
Fills in default values for testing purposes. |
82
|
|
|
""" |
83
|
|
|
|
84
|
|
|
title = kwargs.pop('title', 'test topic') |
85
|
|
|
description = kwargs.pop('description', 'test description') |
86
|
|
|
|
87
|
|
|
if len(kwargs) > 0: |
88
|
|
|
raise ValueError("Received unexpected kwargs: %s" % kwargs) |
89
|
|
|
|
90
|
|
|
topic_kwargs = dict( |
91
|
|
|
title=title, |
92
|
|
|
description=description) |
93
|
|
|
|
94
|
|
|
return models.Topic.objects.create(**topic_kwargs) |
95
|
|
|
|