Merge branch 'develop' into 'master'

Develop

See merge request !64
This commit is contained in:
Jan Tuomi
2017-10-16 17:42:00 +03:00
14 changed files with 137 additions and 127 deletions
-1
View File
@@ -1,5 +1,4 @@
from . import mqtt
import logging
logging.info('Starting mqtt loop')
mqtt.client.loop_start()
Binary file not shown.
+20 -12
View File
@@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-10-16 12:20+0300\n"
"POT-Creation-Date: 2017-10-16 15:35+0300\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@@ -192,7 +192,7 @@ msgid "English"
msgstr "English"
#: infoscreen/templates/infoscreen_admin.html:166
#: members/templates/settings.html:23 webapp/templates/kaehmy.html:19
#: members/templates/settings.html:23 webapp/templates/kaehmy.html:54
#, fuzzy
#| msgid "Submitted"
msgid "Submit"
@@ -556,17 +556,17 @@ msgstr "Copyright Aalto-yliopiston Sähköinsinöörikilta ry"
msgid "Custom roles"
msgstr "Total challenges:"
#: webapp/forms.py:30 webapp/forms.py:33
#: webapp/forms.py:35 webapp/templates/kaehmy.html:30
#, fuzzy
#| msgid "Total challenges:"
msgid "Preset roles"
msgstr "Total challenges:"
#: webapp/forms.py:41
#: webapp/forms.py:57
msgid "Invalid phone number"
msgstr ""
#: webapp/forms.py:49
#: webapp/forms.py:65
msgid "Custom role with the same name already exists."
msgstr ""
@@ -750,7 +750,7 @@ msgstr ""
msgid "Timestamp"
msgstr ""
#: webapp/models.py:178 webapp/models.py:242
#: webapp/models.py:178 webapp/models.py:249
msgid "Phone number"
msgstr ""
@@ -772,19 +772,27 @@ msgstr ""
msgid "Kaehmy application: {}"
msgstr "Member applications"
#: webapp/models.py:227
#: webapp/models.py:214
msgid "Board: {}"
msgstr ""
#: webapp/models.py:220
msgid "Official: {}"
msgstr ""
#: webapp/models.py:234
msgid "Role"
msgstr ""
#: webapp/models.py:229
#: webapp/models.py:236
msgid "Start date"
msgstr ""
#: webapp/models.py:230
#: webapp/models.py:237
msgid "End date"
msgstr ""
#: webapp/models.py:240
#: webapp/models.py:247
msgid "Official"
msgstr ""
@@ -835,11 +843,11 @@ msgstr ""
msgid "Comment"
msgstr ""
#: webapp/templates/kaehmy_list.html:69
#: webapp/templates/kaehmy_list.html:73
msgid "Show comments"
msgstr ""
#: webapp/templates/kaehmy_list.html:75
#: webapp/templates/kaehmy_list.html:79
#, fuzzy
#| msgid "Add payment"
msgid "Add comment"
Binary file not shown.
+20 -24
View File
@@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-10-16 12:20+0300\n"
"POT-Creation-Date: 2017-10-16 15:35+0300\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@@ -193,7 +193,7 @@ msgid "English"
msgstr "englanti"
#: infoscreen/templates/infoscreen_admin.html:166
#: members/templates/settings.html:23 webapp/templates/kaehmy.html:19
#: members/templates/settings.html:23 webapp/templates/kaehmy.html:54
msgid "Submit"
msgstr "Lisää"
@@ -547,15 +547,15 @@ msgstr "Copyright Aalto-yliopiston Sähköinsinöörikilta ry"
msgid "Custom roles"
msgstr "Uudet virat"
#: webapp/forms.py:30 webapp/forms.py:33
#: webapp/forms.py:35 webapp/templates/kaehmy.html:30
msgid "Preset roles"
msgstr "Kaehmyvirat"
#: webapp/forms.py:41
#: webapp/forms.py:57
msgid "Invalid phone number"
msgstr "Virheellinen puhelinnumero"
#: webapp/forms.py:49
#: webapp/forms.py:65
msgid "Custom role with the same name already exists."
msgstr "Samanniminen virka on jo olemassa"
@@ -709,7 +709,7 @@ msgstr "Viesti"
msgid "Timestamp"
msgstr "Aikaleima"
#: webapp/models.py:178 webapp/models.py:242
#: webapp/models.py:178 webapp/models.py:249
msgid "Phone number"
msgstr "Puhelinnumero"
@@ -729,19 +729,27 @@ msgstr "Uusi virka"
msgid "Kaehmy application: {}"
msgstr "Kaehmy: {}"
#: webapp/models.py:227
#: webapp/models.py:214
msgid "Board: {}"
msgstr "Hallitus: {}"
#: webapp/models.py:220
msgid "Official: {}"
msgstr "Toimari: {}"
#: webapp/models.py:234
msgid "Role"
msgstr "Rooli"
#: webapp/models.py:229
#: webapp/models.py:236
msgid "Start date"
msgstr "Alkupäivämäärä"
#: webapp/models.py:230
#: webapp/models.py:237
msgid "End date"
msgstr "Loppupäivämäärä"
#: webapp/models.py:240
#: webapp/models.py:247
msgid "Official"
msgstr "Toimihenkilö"
@@ -790,11 +798,11 @@ msgstr "Kommentoidaan viestiin lähettäjältä"
msgid "Comment"
msgstr "Kommentti"
#: webapp/templates/kaehmy_list.html:69
#: webapp/templates/kaehmy_list.html:73
msgid "Show comments"
msgstr "Näytä kommentit"
#: webapp/templates/kaehmy_list.html:75
#: webapp/templates/kaehmy_list.html:79
msgid "Add comment"
msgstr "Kommentoi"
@@ -862,15 +870,3 @@ msgstr "Kaikki haasteet"
#: webapp/templates/ohlhafv_list.html:15
msgid "Total challenges:"
msgstr "Haasteita yhteensä:"
#~ msgid "Invalid value"
#~ msgstr "Epäkelpo arvo"
#~ msgid "Roles"
#~ msgstr "Roolit"
#~ msgid "Apply"
#~ msgstr "Hakulomake"
#~ msgid "language"
#~ msgstr "Kieli"
+1 -2
View File
@@ -5,7 +5,7 @@ from webapp.models import Official, Role
from webapp.models import Feed, Tag, BaseFeed, Event, Registration
from webapp.models import KaehmyForm, KaehmyMessage
from webapp.models import CustomKaehmyRole, PresetKaehmyRole
from webapp.models import TelegramChannel, TelegramMessage
from webapp.models import TelegramChannel
from modeltranslation.admin import TranslationAdmin
from django.contrib.auth.models import Permission
# this is needed so that the models get registered for translation
@@ -24,4 +24,3 @@ admin.site.register(KaehmyMessage)
admin.site.register(CustomKaehmyRole)
admin.site.register(PresetKaehmyRole)
admin.site.register(TelegramChannel)
admin.site.register(TelegramMessage)
+2 -2
View File
@@ -30,7 +30,7 @@ class KaehmyForm_Form(forms.ModelForm):
for cat_id, category in BaseRole.CATEGORIES:
key = 'preset_roles_{}'.format(cat_id)
qset = PresetKaehmyRole.objects.filter(category=cat_id).order_by('category', 'is_board')
qset = PresetKaehmyRole.objects.filter(category=cat_id).order_by('category', '-is_board')
self.fields[key] = forms.ModelMultipleChoiceField(qset)
self.fields[key].widget = forms.widgets.CheckboxSelectMultiple(attrs={'title': _('Preset roles'), 'name': 'preset_roles'})
self.fields[key].help_text = ""
@@ -65,7 +65,7 @@ class KaehmyForm_Form(forms.ModelForm):
raise ValidationError(_('Custom role with the same name already exists.'))
def non_role_fields(self):
return [self.fields[k] for k in self.fields.keys() if not k in ["preset_roles", "custom_roles"]]
return [self.fields[k] for k in self.fields.keys() if k not in ["preset_roles", "custom_roles"]]
class KaehmyCommentForm(forms.ModelForm):
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2017-10-16 11:55
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('webapp', '0030_telegrammessage'),
]
operations = [
migrations.AlterField(
model_name='telegramchannel',
name='channel_id',
field=models.CharField(max_length=255, unique=True),
),
]
@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2017-10-16 14:28
from __future__ import unicode_literals
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('webapp', '0031_auto_20171016_1455'),
]
operations = [
migrations.DeleteModel(
name='TelegramMessage',
),
]
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2017-10-16 14:31
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('webapp', '0032_delete_telegrammessage'),
]
operations = [
migrations.AddField(
model_name='telegramchannel',
name='name',
field=models.CharField(default='Jan Tuomi private', max_length=255),
preserve_default=False,
),
]
+15 -12
View File
@@ -207,10 +207,17 @@ class KaehmyForm(MessageParent):
return total
def all_roles(self):
presets = [r.name.capitalize() for r in self.preset_roles.all()]
customs = [r.name.capitalize() for r in self.custom_roles.all()]
return ', '.join(presets + customs)
def board_roles(self):
presets = [r.name.capitalize() for r in self.preset_roles.filter(is_board=True)]
customs = [r.name.capitalize() for r in self.custom_roles.filter(is_board=True)]
combined = presets + customs
return _('Board: {}').format(', '.join(combined)) if len(combined) > 0 else ''
def official_roles(self):
presets = [r.name.capitalize() for r in self.preset_roles.filter(is_board=False)]
customs = [r.name.capitalize() for r in self.custom_roles.filter(is_board=False)]
combined = presets + customs
return _('Official: {}').format(', '.join(combined)) if len(combined) > 0 else ''
class Role(PresetRole):
@@ -264,15 +271,11 @@ class OhlhafvChallenge(models.Model):
class TelegramChannel(models.Model):
"""Model containing the channel id of a Telegram chat"""
channel_id = models.CharField(max_length=255)
name = models.CharField(max_length=255)
channel_id = models.CharField(max_length=255, unique=True)
class TelegramMessage(models.Model):
"""Single Telegram message"""
message_id = models.CharField(max_length=255, unique=True)
channel_id = models.CharField(max_length=255)
text = models.CharField(max_length=255)
def __str__(self):
return 'Telegram channel: "{}"'.format(self.name)
auditlog.register(Tag)
-67
View File
@@ -1,67 +0,0 @@
import sys
import asyncio
import logging
import telepot
from django.conf import settings
from django.dispatch import receiver
from django.db.models.signals import post_save, post_delete, pre_save
from webapp.models import TelegramChannel, TelegramMessage
TOKEN = settings.TELEGRAM_BOT_TOKEN
bot = telepot.Bot(TOKEN)
logging.getLogger('urllib3.connectionpool').setLevel(logging.WARNING)
logging.getLogger('urllib3.util.retry').setLevel(logging.WARNING)
class KaehmyHandler:
def handle(self, msg):
flavor = telepot.flavor(msg)
if flavor == 'chat':
text = msg['text']
id = msg['chat']['id']
msg_id = msg['message_id']
if text == '/start':
TelegramChannel.objects.create(channel_id=id)
try:
TelegramMessage.objects.create(message_id=msg_id, channel_id=id, text='Moro! Uudet kaehmyt postataan tälle kanavalle.')
except:
pass
elif text == '/stop':
channels_started = [int(channel.channel_id) for channel in TelegramChannel.objects.all()]
if id in channels_started:
TelegramChannel.objects.get(channel_id=id).delete()
try:
TelegramMessage.objects.create(message_id=msg_id, channel_id=id, text='Lopetetaan kaehmyjen postailu.')
except:
pass
def announce(self, url, name):
channels_started = [channel.channel_id for channel in TelegramChannel.objects.all()]
logging.debug('Announcing to {} Telegram channels.'.format(len(channels_started)))
for id in channels_started:
bot.sendMessage(id, 'Uusi kaehmy/New kaehmy! {} -> {}'.format(name, url))
kaehmy_handler = KaehmyHandler()
def main():
bot.message_loop(kaehmy_handler.handle)
logging.debug('Telepot listening...')
try:
bot.getMe()
main()
except Exception as ex:
logging.exception('Failed to create Telegram bot with token "{}"'.format(TOKEN))
@receiver(post_save, sender=TelegramMessage, dispatch_uid="save_tg_message")
def save_message(sender, instance, **kwargs):
bot.sendMessage(instance.channel_id, instance.text)
instance.save()
+6 -2
View File
@@ -60,8 +60,12 @@
<div class="card">
<h4 class="card-header">{{ application.name }}</h4>
<div class="card-block">
<h5 style="padding-bottom: 1rem" class="card-subtitle mb-2 text-muted">{{ application.all_roles }}</h5>
{% if application.board_roles|length > 0 %}
<h5 style="padding-bottom: 1rem" class="card-subtitle mb-2 text-muted">{{ application.board_roles }}</h5>
{% endif %}
{% if application.official_roles|length > 0 %}
<h5 style="padding-bottom: 1rem" class="card-subtitle mb-2 text-muted">{{ application.official_roles }}</h5>
{% endif %}
<p class="card-text">{{ application.text }}</p>
{% if application.comment_count > 0 %}
+14 -5
View File
@@ -7,15 +7,16 @@ from django.views.decorators.csrf import ensure_csrf_cookie
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth.decorators import permission_required, login_required
from django.conf import settings
import logging
import requests
from webapp.models import PresetKaehmyRole, CustomKaehmyRole
from webapp.models import OhlhafvChallenge, KaehmyForm
from webapp.models import OhlhafvChallenge, KaehmyForm, TelegramChannel
from webapp.forms import OhlhafvForm, KaehmyForm_Form, KaehmyCommentForm
from webapp.tables import OhlhafvTable
from webapp.telegram_bot import kaehmy_handler
from django.core.mail import send_mail
from django.conf import settings
def send_email(to, subject, body):
@@ -159,8 +160,16 @@ def kaehmy_submit(request, *args, **kwargs):
send_email(email, subject, body)
logging.debug('Sent kaehmy email to recipient <{}>'.format(email))
kaehmy_handler.announce(url, name)
logging.debug('Sent kaehmy announcement to telegram.')
CHAT_IDS = [channel.channel_id for channel in TelegramChannel.objects.all()]
for chat_id in CHAT_IDS:
tg_string = 'https://api.telegram.org/bot{}/sendMessage?chat_id={}&text={}'.format(
settings.TELEGRAM_BOT_TOKEN,
chat_id,
'Uusi kaehmy/New kaehmy! {} -> {}'.format(name, url)
)
response = requests.get(tg_string).json()
logging.debug('Telegram API response:\n{}'.format(response))
logging.debug('Sent kaehmy announcement to {} channels.'.format(len(CHAT_IDS)))
else:
context = {