-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodel_comment.py
150 lines (135 loc) · 3.76 KB
/
model_comment.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
#
# The specific model classes and functions for comments.
# This is separate from model.py because it is getting big.
import re
import utils
import pages
import derrors, storage
# ---
# Original (legacy) comment format, which has only DWiki login and
# comment IP address.
# Comments are stored in a packed form.
commentbody_re = re.compile("USER ([^\n]+)\nIP ([^\n]+)\n(.*)$",
re.DOTALL)
class Comment:
def __init__(self, context = None, data = None):
if context:
self.user = context.login
self.ip = context["remote-ip"]
else:
self.user = None
self.ip = None
if not data:
self.data = ''
else:
self.data = data
self.time = None
self.name = None
self.username = ''
self.userurl = ''
self.anon = None
def __str__(self):
if not self.data:
return ''
return 'USER %s\nIP %s\n%s' % (self.user, self.ip, self.data)
def fromstore(self, fileobj, name):
blob = fileobj.contents()
if not blob:
return False
mo = commentbody_re.match(blob)
if not mo:
return False
self.user = mo.group(1)
self.ip = mo.group(2)
self.data = mo.group(3)
self.time = fileobj.timestamp()
self.name = name
return True
def is_anon(self, context):
return self.user == context.default_user()
# ---
# New format comments now include an explicit version field as the first
# field.
commentver_re = re.compile("^VER (\d+)\n")
# V1 new comment format. This adds a user-supplied name and URL so that
# DWiki acts more like traditional blog comments and while I'm at it,
# a marker of whether the DWiki login was the default/anonymous/guest
# user at the time of comment submission (... in case you remove or
# change it later or something).
#
commentv1_re = re.compile("^VER 1\nUSER ([^\n]+)\nANON (Yes|No)\nNAME ([^\n]*)\nURL ([^\n]*)\nIP ([^\n]+)\n(.*)$",
re.DOTALL)
class CommentV1:
def __init__(self):
self.user = None
self.anon = None
self.ip = None
self.data = ''
self.username = ''
self.userurl = ''
self.time = None
self.name = None
def __str__(self):
if not self.data:
return ''
return "VER 1\nUSER %s\nANON %s\nNAME %s\nURL %s\nIP %s\n%s" % \
(self.user, self.anon,
self.username, self.userurl, self.ip,
self.data)
# Called only if the blob is non-null and asserts to be a V1 format.
def fromstore(self, fileobj, name):
blob = fileobj.contents()
if not blob:
raise derrors.IntErr("CommentV1 fromstore blob is empty")
mo = commentv1_re.match(blob)
if not mo:
return False
self.user = mo.group(1)
self.anon = mo.group(2)
self.username = mo.group(3).strip()
self.userurl = mo.group(4).strip()
self.ip = mo.group(5)
self.data = mo.group(6)
self.time = fileobj.timestamp()
self.name = name
return True
def fromform(self, context, data, username, userurl):
self.user = context.login
self.ip = context["remote-ip"]
self.data = data
self.username = username
self.userurl = userurl
if context.is_login_default():
self.anon = "Yes"
else:
self.anon = "No"
def is_anon(self, _):
return self.anon == "Yes"
# ----
# This loads comments in any known format from the disk, working out
# which format the comment is in itself. Returns the comment or None
# and may raise derrors.IntErr in some situations.
def loadcomment(fileobj, name):
blob = fileobj.contents()
if not blob:
return None
mo = commentver_re.match(blob)
# might be a version zero comment.
if not mo:
mo = commentbody_re.match(blob)
if mo:
c = Comment()
else:
return None
elif mo.group(1) == "1":
c = CommentV1()
else:
raise derrors.IntErr("Uknown comment format version: '%s' in %s" %
(mo.group(1), name))
# Load:
if c.fromstore(fileobj, name):
return c
else:
return None
# TODO: should we have a createcomment() function? Probably not, since the
# arguments are likely to keep evolving.