add draft of notification system

This commit is contained in:
Davide Alberani 2018-01-21 13:29:36 +01:00
parent fcbef86f3b
commit 208e028569
2 changed files with 52 additions and 6 deletions

View file

@ -7,6 +7,8 @@ import io
import json import json
import shutil import shutil
import urllib import urllib
import smtplib
from email.mime.text import MIMEText
import logging import logging
import datetime import datetime
import requests import requests
@ -78,17 +80,60 @@ def run_job(id_=None, *args, **kwargs):
content = req.text content = req.text
req_path = urllib.parse.urlparse(req.url).path req_path = urllib.parse.urlparse(req.url).path
base_name = os.path.basename(req_path) or 'index' base_name = os.path.basename(req_path) or 'index'
def _commit(id_, filename, content): def _commit(id_, filename, content, queue):
os.chdir('storage/%s' % id_) os.chdir('storage/%s' % id_)
current_lines = 0
if os.path.isfile(filename):
with open(filename, 'r') as fd:
for line in fd:
current_lines += 1
with open(filename, 'w') as fd: with open(filename, 'w') as fd:
fd.write(content) fd.write(content)
p = subprocess.Popen([GIT_CMD, 'add', filename]) p = subprocess.Popen([GIT_CMD, 'add', filename])
p.communicate() p.communicate()
p = subprocess.Popen([GIT_CMD, 'commit', '-m', '%s' % datetime.datetime.utcnow(), '--allow-empty']) p = subprocess.Popen([GIT_CMD, 'commit', '-m', '%s' % datetime.datetime.utcnow(), '--allow-empty'],
p.communicate() stdout=subprocess.PIPE)
p = multiprocessing.Process(target=_commit, args=(id_, base_name, content)) stdout, _ = p.communicate()
stdout = stdout.decode('utf-8')
insert = re_insertion.findall(stdout)
if insert:
insert = int(insert[0])
else:
insert = 0
delete = re_deletion.findall(stdout)
if delete:
delete = int(delete[0])
else:
delete = 0
queue.put({'insertions': insert, 'deletions': delete, 'previous_lines': current_lines,
'changes': max(insert, delete)})
queue = multiprocessing.Queue()
p = multiprocessing.Process(target=_commit, args=(id_, base_name, content, queue))
p.start() p.start()
res = queue.get()
p.join() p.join()
email = schedule.get('email')
if not email:
return
changes = res.get('changes')
if not changes:
return
min_change = schedule.get('minimum_change')
previous_lines = res.get('previous_lines')
if min_change and previous_lines:
min_change = float(min_change)
change_fraction = res.get('changes') / previous_lines
if change_fraction < min_change:
return
# send notification
diff = get_diff(id_)
msg = MIMEText('changes:\n\n%s' % diff.get('diff'))
msg['Subject'] = '%s page changed' % schedule.get('title')
msg['From'] = 'Diffido <da@mimante.net>'
msg['To'] = email
s = smtplib.SMTP('localhost')
s.send_message(msg)
s.quit()
def get_history(id_): def get_history(id_):
@ -132,7 +177,7 @@ def get_history(id_):
return {'history': history, 'lastid': lastid} return {'history': history, 'lastid': lastid}
def get_diff(id_, diff, oldid=None): def get_diff(id_, diff='HEAD', oldid=None):
def _history(id_, diff, oldid, queue): def _history(id_, diff, oldid, queue):
os.chdir('storage/%s' % id_) os.chdir('storage/%s' % id_)
p = subprocess.Popen([GIT_CMD, 'diff', diff, oldid or '%s~' % diff], stdout=subprocess.PIPE) p = subprocess.Popen([GIT_CMD, 'diff', diff, oldid or '%s~' % diff], stdout=subprocess.PIPE)

3
dist/history.html vendored
View file

@ -10,12 +10,13 @@
</md-table-toolbar> </md-table-toolbar>
<md-table-row slot="md-table-row" slot-scope="{item}"> <md-table-row slot="md-table-row" slot-scope="{item}">
<md-table-cell> <md-table-cell>
(<a :href="'/diff.html?id=' + id + '&oldid=' + item.id + '&diff=' + lastid">cur</a> | <a :href="'/diff.html?id=' + id + '&diff=' + item.id">prev</a>) (<a v-if="item.seq > 1" :href="'/diff.html?id=' + id + '&oldid=' + item.id + '&diff=' + lastid">cur</a><span v-if="item.seq == 1">cur</span> | <a :href="'/diff.html?id=' + id + '&diff=' + item.id">prev</a>)
<md-radio name="oldid" v-model="oldid" :value="item.id" v-if="item.seq > 1" :seq="item.seq"></md-radio><span id="placeholder" v-if="item.seq == 1"> ---- </span> <md-radio name="oldid" v-model="oldid" :value="item.id" v-if="item.seq > 1" :seq="item.seq"></md-radio><span id="placeholder" v-if="item.seq == 1"> ---- </span>
<md-radio name="diff" v-model="diff" :value="item.id" :seq="item.seq"></md-radio> <md-radio name="diff" v-model="diff" :value="item.id" :seq="item.seq"></md-radio>
</md-table-cell> </md-table-cell>
<md-table-cell md-label="commit ID" md-sort-by="id">${ item.id }</md-table-cell> <md-table-cell md-label="commit ID" md-sort-by="id">${ item.id }</md-table-cell>
<md-table-cell md-label="message" md-sort-by="message">${ item.message }</md-table-cell> <md-table-cell md-label="message" md-sort-by="message">${ item.message }</md-table-cell>
<md-table-cell md-label="changes" md-sort-by="message">+${ item.insertions || 0 },-${ item.deletions || 0 }</md-table-cell>
</md-table-row> </md-table-row>
</md-table> </md-table>
</div> </div>