-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathforms.py
49 lines (41 loc) · 1.51 KB
/
forms.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
import uuid
import datetime
from django.forms import ModelForm
from posts.models import Post
from events.models import Event, EventData
from events.event_types import EventTypes
from events.event_handler import EventHandler
class PostForm(ModelForm):
class Meta:
model = Post
fields = ['title', 'content']
def __init__(self, *args, **kwargs):
super(PostForm, self).__init__(*args, **kwargs)
self.fields['title'].widget.attrs.update({'class': 'input-field'})
self.fields['content'].widget.attrs.update({'class': 'materialize-textarea'})
def save(self, commit=True, action=None):
# This is where the write model separates.
# Use event handler.
event_data = EventData(
title=self.cleaned_data['title'],
content=self.cleaned_data['content']
)
if action == 'update':
# this is an update
event = Event(
event_id=uuid.uuid4(),
event_time=datetime.datetime.now(),
event_type=EventTypes.post_updated_event,
event_data=event_data,
post_id=self.instance.id
)
else:
event = Event(
event_id=uuid.uuid4(),
event_time=datetime.datetime.now(),
event_type=EventTypes.post_created_event,
event_data=event_data,
post_id=uuid.uuid4()
)
event_handler = EventHandler([event])
event_handler.process()