diff --git a/coffee_scale/__init__.py b/coffee_scale/__init__.py index 6ddb4ad..61bc51a 100644 --- a/coffee_scale/__init__.py +++ b/coffee_scale/__init__.py @@ -1,5 +1,4 @@ from . import mqtt import logging -logging.info('Starting mqtt loop') mqtt.client.loop_start() diff --git a/locale/en/LC_MESSAGES/django.mo b/locale/en/LC_MESSAGES/django.mo index 7317b08..c8c0421 100644 Binary files a/locale/en/LC_MESSAGES/django.mo and b/locale/en/LC_MESSAGES/django.mo differ diff --git a/locale/en/LC_MESSAGES/django.po b/locale/en/LC_MESSAGES/django.po index 9070691..366ef4f 100644 --- a/locale/en/LC_MESSAGES/django.po +++ b/locale/en/LC_MESSAGES/django.po @@ -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 \n" "Language-Team: LANGUAGE \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" diff --git a/locale/fi/LC_MESSAGES/django.mo b/locale/fi/LC_MESSAGES/django.mo index bae8927..46d2998 100644 Binary files a/locale/fi/LC_MESSAGES/django.mo and b/locale/fi/LC_MESSAGES/django.mo differ diff --git a/locale/fi/LC_MESSAGES/django.po b/locale/fi/LC_MESSAGES/django.po index 76fb992..70e301e 100644 --- a/locale/fi/LC_MESSAGES/django.po +++ b/locale/fi/LC_MESSAGES/django.po @@ -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 \n" "Language-Team: LANGUAGE \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" diff --git a/webapp/admin.py b/webapp/admin.py index 2d0d3ba..cb9afd4 100644 --- a/webapp/admin.py +++ b/webapp/admin.py @@ -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) diff --git a/webapp/forms.py b/webapp/forms.py index 3c44262..ba273e7 100644 --- a/webapp/forms.py +++ b/webapp/forms.py @@ -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 = "" diff --git a/webapp/migrations/0031_auto_20171016_1455.py b/webapp/migrations/0031_auto_20171016_1455.py new file mode 100644 index 0000000..a35466e --- /dev/null +++ b/webapp/migrations/0031_auto_20171016_1455.py @@ -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), + ), + ] diff --git a/webapp/migrations/0032_delete_telegrammessage.py b/webapp/migrations/0032_delete_telegrammessage.py new file mode 100644 index 0000000..434af97 --- /dev/null +++ b/webapp/migrations/0032_delete_telegrammessage.py @@ -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', + ), + ] diff --git a/webapp/migrations/0033_telegramchannel_name.py b/webapp/migrations/0033_telegramchannel_name.py new file mode 100644 index 0000000..5a86550 --- /dev/null +++ b/webapp/migrations/0033_telegramchannel_name.py @@ -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, + ), + ] diff --git a/webapp/models.py b/webapp/models.py index bac9733..2aa1ed5 100644 --- a/webapp/models.py +++ b/webapp/models.py @@ -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) diff --git a/webapp/telegram_bot.py b/webapp/telegram_bot.py deleted file mode 100644 index 3da273d..0000000 --- a/webapp/telegram_bot.py +++ /dev/null @@ -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() \ No newline at end of file diff --git a/webapp/templates/kaehmy_list.html b/webapp/templates/kaehmy_list.html index d4153d4..b5cabcb 100644 --- a/webapp/templates/kaehmy_list.html +++ b/webapp/templates/kaehmy_list.html @@ -60,8 +60,12 @@

{{ application.name }}

-
{{ application.all_roles }}
- + {% if application.board_roles|length > 0 %} +
{{ application.board_roles }}
+ {% endif %} + {% if application.official_roles|length > 0 %} +
{{ application.official_roles }}
+ {% endif %}

{{ application.text }}

{% if application.comment_count > 0 %} diff --git a/webapp/views.py b/webapp/views.py index 316ae94..97f69e4 100644 --- a/webapp/views.py +++ b/webapp/views.py @@ -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 = {