Overall fixes

This commit is contained in:
Jan Tuomi
2017-10-16 17:38:39 +03:00
parent e9185b0a03
commit b609fd3688
14 changed files with 136 additions and 126 deletions
-1
View File
@@ -1,5 +1,4 @@
from . import mqtt from . import mqtt
import logging import logging
logging.info('Starting mqtt loop')
mqtt.client.loop_start() mqtt.client.loop_start()
Binary file not shown.
+20 -12
View File
@@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: PACKAGE VERSION\n" "Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \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" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
@@ -192,7 +192,7 @@ msgid "English"
msgstr "English" msgstr "English"
#: infoscreen/templates/infoscreen_admin.html:166 #: 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 #, fuzzy
#| msgid "Submitted" #| msgid "Submitted"
msgid "Submit" msgid "Submit"
@@ -556,17 +556,17 @@ msgstr "Copyright Aalto-yliopiston Sähköinsinöörikilta ry"
msgid "Custom roles" msgid "Custom roles"
msgstr "Total challenges:" msgstr "Total challenges:"
#: webapp/forms.py:30 webapp/forms.py:33 #: webapp/forms.py:35 webapp/templates/kaehmy.html:30
#, fuzzy #, fuzzy
#| msgid "Total challenges:" #| msgid "Total challenges:"
msgid "Preset roles" msgid "Preset roles"
msgstr "Total challenges:" msgstr "Total challenges:"
#: webapp/forms.py:41 #: webapp/forms.py:57
msgid "Invalid phone number" msgid "Invalid phone number"
msgstr "" msgstr ""
#: webapp/forms.py:49 #: webapp/forms.py:65
msgid "Custom role with the same name already exists." msgid "Custom role with the same name already exists."
msgstr "" msgstr ""
@@ -750,7 +750,7 @@ msgstr ""
msgid "Timestamp" msgid "Timestamp"
msgstr "" msgstr ""
#: webapp/models.py:178 webapp/models.py:242 #: webapp/models.py:178 webapp/models.py:249
msgid "Phone number" msgid "Phone number"
msgstr "" msgstr ""
@@ -772,19 +772,27 @@ msgstr ""
msgid "Kaehmy application: {}" msgid "Kaehmy application: {}"
msgstr "Member applications" 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" msgid "Role"
msgstr "" msgstr ""
#: webapp/models.py:229 #: webapp/models.py:236
msgid "Start date" msgid "Start date"
msgstr "" msgstr ""
#: webapp/models.py:230 #: webapp/models.py:237
msgid "End date" msgid "End date"
msgstr "" msgstr ""
#: webapp/models.py:240 #: webapp/models.py:247
msgid "Official" msgid "Official"
msgstr "" msgstr ""
@@ -835,11 +843,11 @@ msgstr ""
msgid "Comment" msgid "Comment"
msgstr "" msgstr ""
#: webapp/templates/kaehmy_list.html:69 #: webapp/templates/kaehmy_list.html:73
msgid "Show comments" msgid "Show comments"
msgstr "" msgstr ""
#: webapp/templates/kaehmy_list.html:75 #: webapp/templates/kaehmy_list.html:79
#, fuzzy #, fuzzy
#| msgid "Add payment" #| msgid "Add payment"
msgid "Add comment" msgid "Add comment"
Binary file not shown.
+20 -24
View File
@@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: PACKAGE VERSION\n" "Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \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" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n" "Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n" "Language-Team: LANGUAGE <LL@li.org>\n"
@@ -193,7 +193,7 @@ msgid "English"
msgstr "englanti" msgstr "englanti"
#: infoscreen/templates/infoscreen_admin.html:166 #: 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" msgid "Submit"
msgstr "Lisää" msgstr "Lisää"
@@ -547,15 +547,15 @@ msgstr "Copyright Aalto-yliopiston Sähköinsinöörikilta ry"
msgid "Custom roles" msgid "Custom roles"
msgstr "Uudet virat" msgstr "Uudet virat"
#: webapp/forms.py:30 webapp/forms.py:33 #: webapp/forms.py:35 webapp/templates/kaehmy.html:30
msgid "Preset roles" msgid "Preset roles"
msgstr "Kaehmyvirat" msgstr "Kaehmyvirat"
#: webapp/forms.py:41 #: webapp/forms.py:57
msgid "Invalid phone number" msgid "Invalid phone number"
msgstr "Virheellinen puhelinnumero" msgstr "Virheellinen puhelinnumero"
#: webapp/forms.py:49 #: webapp/forms.py:65
msgid "Custom role with the same name already exists." msgid "Custom role with the same name already exists."
msgstr "Samanniminen virka on jo olemassa" msgstr "Samanniminen virka on jo olemassa"
@@ -709,7 +709,7 @@ msgstr "Viesti"
msgid "Timestamp" msgid "Timestamp"
msgstr "Aikaleima" msgstr "Aikaleima"
#: webapp/models.py:178 webapp/models.py:242 #: webapp/models.py:178 webapp/models.py:249
msgid "Phone number" msgid "Phone number"
msgstr "Puhelinnumero" msgstr "Puhelinnumero"
@@ -729,19 +729,27 @@ msgstr "Uusi virka"
msgid "Kaehmy application: {}" msgid "Kaehmy application: {}"
msgstr "Kaehmy: {}" 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" msgid "Role"
msgstr "Rooli" msgstr "Rooli"
#: webapp/models.py:229 #: webapp/models.py:236
msgid "Start date" msgid "Start date"
msgstr "Alkupäivämäärä" msgstr "Alkupäivämäärä"
#: webapp/models.py:230 #: webapp/models.py:237
msgid "End date" msgid "End date"
msgstr "Loppupäivämäärä" msgstr "Loppupäivämäärä"
#: webapp/models.py:240 #: webapp/models.py:247
msgid "Official" msgid "Official"
msgstr "Toimihenkilö" msgstr "Toimihenkilö"
@@ -790,11 +798,11 @@ msgstr "Kommentoidaan viestiin lähettäjältä"
msgid "Comment" msgid "Comment"
msgstr "Kommentti" msgstr "Kommentti"
#: webapp/templates/kaehmy_list.html:69 #: webapp/templates/kaehmy_list.html:73
msgid "Show comments" msgid "Show comments"
msgstr "Näytä kommentit" msgstr "Näytä kommentit"
#: webapp/templates/kaehmy_list.html:75 #: webapp/templates/kaehmy_list.html:79
msgid "Add comment" msgid "Add comment"
msgstr "Kommentoi" msgstr "Kommentoi"
@@ -862,15 +870,3 @@ msgstr "Kaikki haasteet"
#: webapp/templates/ohlhafv_list.html:15 #: webapp/templates/ohlhafv_list.html:15
msgid "Total challenges:" msgid "Total challenges:"
msgstr "Haasteita yhteensä:" 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 Feed, Tag, BaseFeed, Event, Registration
from webapp.models import KaehmyForm, KaehmyMessage from webapp.models import KaehmyForm, KaehmyMessage
from webapp.models import CustomKaehmyRole, PresetKaehmyRole from webapp.models import CustomKaehmyRole, PresetKaehmyRole
from webapp.models import TelegramChannel, TelegramMessage from webapp.models import TelegramChannel
from modeltranslation.admin import TranslationAdmin from modeltranslation.admin import TranslationAdmin
from django.contrib.auth.models import Permission from django.contrib.auth.models import Permission
# this is needed so that the models get registered for translation # 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(CustomKaehmyRole)
admin.site.register(PresetKaehmyRole) admin.site.register(PresetKaehmyRole)
admin.site.register(TelegramChannel) admin.site.register(TelegramChannel)
admin.site.register(TelegramMessage)
+1 -1
View File
@@ -30,7 +30,7 @@ class KaehmyForm_Form(forms.ModelForm):
for cat_id, category in BaseRole.CATEGORIES: for cat_id, category in BaseRole.CATEGORIES:
key = 'preset_roles_{}'.format(cat_id) 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] = forms.ModelMultipleChoiceField(qset)
self.fields[key].widget = forms.widgets.CheckboxSelectMultiple(attrs={'title': _('Preset roles'), 'name': 'preset_roles'}) self.fields[key].widget = forms.widgets.CheckboxSelectMultiple(attrs={'title': _('Preset roles'), 'name': 'preset_roles'})
self.fields[key].help_text = "" self.fields[key].help_text = ""
@@ -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 return total
def all_roles(self): def board_roles(self):
presets = [r.name.capitalize() for r in self.preset_roles.all()] presets = [r.name.capitalize() for r in self.preset_roles.filter(is_board=True)]
customs = [r.name.capitalize() for r in self.custom_roles.all()] customs = [r.name.capitalize() for r in self.custom_roles.filter(is_board=True)]
return ', '.join(presets + customs) 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): class Role(PresetRole):
@@ -264,15 +271,11 @@ class OhlhafvChallenge(models.Model):
class TelegramChannel(models.Model): class TelegramChannel(models.Model):
"""Model containing the channel id of a Telegram chat""" """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)
def __str__(self):
class TelegramMessage(models.Model): return 'Telegram channel: "{}"'.format(self.name)
"""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)
auditlog.register(Tag) 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"> <div class="card">
<h4 class="card-header">{{ application.name }}</h4> <h4 class="card-header">{{ application.name }}</h4>
<div class="card-block"> <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> <p class="card-text">{{ application.text }}</p>
{% if application.comment_count > 0 %} {% 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.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth.decorators import permission_required, login_required from django.contrib.auth.decorators import permission_required, login_required
from django.conf import settings from django.conf import settings
import logging import logging
import requests
from webapp.models import PresetKaehmyRole, CustomKaehmyRole 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.forms import OhlhafvForm, KaehmyForm_Form, KaehmyCommentForm
from webapp.tables import OhlhafvTable from webapp.tables import OhlhafvTable
from webapp.telegram_bot import kaehmy_handler
from django.core.mail import send_mail from django.core.mail import send_mail
from django.conf import settings
def send_email(to, subject, body): def send_email(to, subject, body):
@@ -159,8 +160,16 @@ def kaehmy_submit(request, *args, **kwargs):
send_email(email, subject, body) send_email(email, subject, body)
logging.debug('Sent kaehmy email to recipient <{}>'.format(email)) logging.debug('Sent kaehmy email to recipient <{}>'.format(email))
kaehmy_handler.announce(url, name) CHAT_IDS = [channel.channel_id for channel in TelegramChannel.objects.all()]
logging.debug('Sent kaehmy announcement to telegram.') 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: else:
context = { context = {