Browse Source

Refactor editing logic

Add wakapost merging to add fields from the request to the original post
happy-birthday-weedy
dequis 6 years ago
parent
commit
1344c3b338
3 changed files with 44 additions and 21 deletions
  1. +4
    -4
      app.py
  2. +14
    -16
      board.py
  3. +26
    -1
      wakapost.py

+ 4
- 4
app.py View File

@@ -178,13 +178,13 @@ def task_editpost(environ, start_response):

cookie = get_cookie_from_request(request, 'wakaadmin')

wakapost = WakaPost.from_request(request)
request_post = WakaPost.from_request(request)

if wakapost.admin_post:
return StaffAction(cookie, 'admin_edit', wakapost=wakapost,
if request_post.admin_post:
return StaffAction(cookie, 'admin_edit', request_post=request_post,
board=board).execute()
else:
return board.edit_stuff(wakapost)
return board.edit_stuff(request_post)


def task_report(environ, start_response):


+ 14
- 16
board.py View File

@@ -436,7 +436,7 @@ class Board(object):
session = model.Session()

# get a timestamp for future use
timestamp = wakapost.timestamp = time.time()
timestamp = time.time()

if admin_data:
admin_data.user.check_access(self.name)
@@ -481,6 +481,7 @@ class Board(object):
# check if thread exists, and get lasthit value
parent_res = None
if not editing:
wakapost.timestamp = timestamp
if wakapost.parent:
parent_res = self.get_parent_post(wakapost.parent)
if not parent_res:
@@ -518,7 +519,7 @@ class Board(object):
# choose whether we need an SQL UPDATE (editing) or INSERT (posting)
if editing:
db_update = self.table.update().where(
self.table.c.num == editing.num)
self.table.c.num == wakapost.num)
else:
db_update = self.table.insert()

@@ -1078,33 +1079,30 @@ class Board(object):
return Template('post_edit_template', loop=[wakapost],
admin=admin_mode)

def edit_stuff(self, wakapost, admin_data=None):

session = model.Session()
def edit_stuff(self, request_post, admin_data=None):

original_row = session.execute(
self.table.select(self.table.c.num == wakapost.num))\
.fetchone()
original_post = self.get_post(request_post.num)

if original_row == None:
if not original_post:
raise WakaError('Post not found')

if not admin_data and wakapost.password != original_row['password']:
if not admin_data and request_post.password != original_post.password:
raise WakaError('Wrong password for editing')

original_post = WakaPost(original_row)
edited_post = WakaPost.copy(original_post)
edited_post.merge(request_post, which='request')

if wakapost.killtrip:
original_post.trip = ''
if edited_post.killtrip:
edited_post.trip = ''

try:
self._handle_post(wakapost, original_post, admin_data)
self._handle_post(edited_post, original_post, admin_data)
except util.SpamError:
return Template('edit_successful')

if admin_data:
admin_data.contents\
.append('/%s/%d' % (self.name, int(wakapost.num)))
admin_data.contents.append(
'/%s/%d' % (self.name, int(request_post.num)))

return Template('edit_successful')



+ 26
- 1
wakapost.py View File

@@ -129,7 +129,32 @@ class WakaPost(object):
self.locked = request.values.get('lock', '0') == '1'
self.req_no_format = request.values.get('no_format', '0') == '1'
self.req_file = request.files.get('file', None)
self.name = request.values.get('field1', '')

return self

@classmethod
def copy(cls, instance):
'''Copies all the attributes of another instance into a new one'''
return cls().merge(instance)

def merge(self, other, which='all'):
'''Merges only the attributes relevant to edited posts

which: either 'all', 'request', or a list of keys'''

keys = []
if which == 'all':
keys = WakaPost.__slots__
elif which == 'request':
keys = ['num', 'parent', 'email', 'subject', 'comment',
'password', 'killtrip', 'postfix', 'ninja', 'nofile',
'name', 'admin_post', 'stickied', 'locked',
'req_no_format', 'req_file']
elif type(which) == list:
keys = which

for key in keys:
setattr(self, key, getattr(other, key, ''))

return self



Loading…
Cancel
Save