This repository has been archived by the owner on Apr 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
72 lines (63 loc) · 2.43 KB
/
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
from django.db import models
from xml.dom.minidom import Document
class FmnaSms(models.Model):
'''
Abstract class to for FMNA SMS message
'''
upstream_id = models.TextField('Message ID')
message = models.TextField('Message', blank=False, null=False)
session_state = models.TextField('Session State', blank=True, null=True)
customer_state = models.TextField('Session State', blank=True, null=True)
class Meta:
abstract = True
app_label = 'api'
class FmnaRequestSms(FmnaSms):
@staticmethod
def getFromPost(request):
"""
@request: django.HttpRequest
@return: FmnaRequestSms
"""
requestSms = FmnaRequestSms()
requestSms.upstream_id = request.POST.get('Id', None)
requestSms.message = request.POST.get('Message', None)
requestSms.customer_state = request.POST.get('CustomerState', None)
requestSms.session_state = request.POST.get('SessionState', None)
if requestSms.upstream_id is None:
raise Exception("Message ID not found")
return requestSms
def generateResponseSms(self):
response = FmnaResponseSms()
response.response_to = self.upstream_id
response.upstream_id = self.upstream_id
response.customer_state = self.customer_state
response.session_state = self.session_state
return response
pass
class FmnaResponseSms(FmnaSms):
response_to = models.TextField('In Response to', blank=True, null=True)
def getXML(self):
"""
@return: string
"""
import pprint
pprint.pprint(self)
doc = Document()
mt = doc.createElement('mt');
mt.setAttribute('id', self.upstream_id)
msg = doc.createElement('msg');
textNode = doc.createTextNode(self.message)
msg.appendChild(textNode)
mt.appendChild(msg)
if self.session_state is not None:
sessionNode = doc.createElement('sessionstate');
textNode = doc.createTextNode( (self.session_state))
sessionNode.appendChild(textNode)
mt.appendChild(sessionNode)
if self.customer_state is not None:
customerNode = doc.createElement('customerstate')
textNode = doc.createTextNode(self.customer_state)
customerNode.appendChild(textNode)
mt.appendChild(customerNode)
doc.appendChild(mt)
return doc.toxml()