From ea14ae5e386442e5af7a97243331e1f5a6fe1850 Mon Sep 17 00:00:00 2001 From: Thomas Steen Rasmussen Date: Tue, 31 Jan 2017 22:50:28 +0100 Subject: [PATCH] remove django-environ and more work on irc bot --- src/bornhack/.env.dist | 36 ---------- src/bornhack/environment_settings.py | 65 +++++++++++++++++++ src/bornhack/environment_settings.py.dist | 65 +++++++++++++++++++ src/bornhack/settings.py | 59 +---------------- src/ircbot/admin.py | 4 +- src/ircbot/irc3module.py | 22 +++++-- src/ircbot/management/commands/ircbot.py | 10 +-- .../0002_outgoingircmessage_timeout.py | 23 +++++++ .../0003_outgoingircmessage_expired.py | 20 ++++++ src/ircbot/models.py | 13 +++- src/requirements.txt | 1 - 11 files changed, 211 insertions(+), 107 deletions(-) delete mode 100644 src/bornhack/.env.dist create mode 100644 src/bornhack/environment_settings.py create mode 100644 src/bornhack/environment_settings.py.dist create mode 100644 src/ircbot/migrations/0002_outgoingircmessage_timeout.py create mode 100644 src/ircbot/migrations/0003_outgoingircmessage_expired.py diff --git a/src/bornhack/.env.dist b/src/bornhack/.env.dist deleted file mode 100644 index f485fd14..00000000 --- a/src/bornhack/.env.dist +++ /dev/null @@ -1,36 +0,0 @@ -### set database url and allowed_hosts here -DATABASE_URL=postgres://username:password@host:port/database -ALLOWED_HOSTS=localhost,127.0.0.1 -DEBUG=True - -### changes below here are only needed for production -SECRET_KEY=somethingverysecretandunique - -EMAIL_HOST='mailhost.example.com' -EMAIL_PORT=587 -EMAIL_HOST_USER='mymailuser' -EMAIL_HOST_PASSWORD='mymailpassword' -EMAIL_USE_TLS=True -DEFAULT_FROM_EMAIL='noreply@example.com' -ARCHIVE_EMAIL='archive@example.com' - -ADMINS=( - ('bornhack sysadm', 'sysadm@bornhack.org'), -) - -EPAY_MERCHANT_NUMBER=123 -EPAY_MD5_SECRET='abc' -TICKET_CATEGORY_ID='304e9b44-0b48-472d-a05b-963e52614a69' -COINIFY_API_KEY='123' -COINIFY_API_SECRET='123' -COINIFY_IPN_SECRET='123' -PDF_LETTERHEAD_FILENAME='letterhead.pdf' -MEDIA_ROOT='/path/to/media/root/outside/django/root/' -BANKACCOUNT_IBAN='123' -BANKACCOUNT_SWIFTBIC='123' -BANKACCOUNT_REG='123' -BANKACCOUNT_ACCOUNT='123' -TIME_ZONE='Europe/Copenhagen' -SCHEDULE_MIDNIGHT_OFFSET_HOURS=6 -SCHEDULE_TIMESLOT_LENGTH_MINUTES=30 - diff --git a/src/bornhack/environment_settings.py b/src/bornhack/environment_settings.py new file mode 100644 index 00000000..66f78794 --- /dev/null +++ b/src/bornhack/environment_settings.py @@ -0,0 +1,65 @@ +# make this a long 100+ chars random string +SECRET_KEY = 'secret' + +# debug settings - remember to set allowed_hosts if debug is disabled +DEBUG = True +ALLOWED_HOSTS = ['localhost', '127.0.0.1'] + +# Database settings +# https://docs.djangoproject.com/en/1.10/ref/settings/#databases +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql_psycopg2', + 'NAME': 'bornhackdb', + 'USER': 'bornhack', + 'PASSWORD': 'bornhack', + 'HOST': '127.0.0.1', + }, +} + +### changes below here are only needed for production + +# email settings +EMAIL_HOST='mailhost.example.com' +EMAIL_PORT=587 +EMAIL_HOST_USER='mymailuser' +EMAIL_HOST_PASSWORD='mymailpassword' +EMAIL_USE_TLS=True +DEFAULT_FROM_EMAIL='noreply@example.com' +ARCHIVE_EMAIL='archive@example.com' + +ADMINS=( + ('sysadm', 'sysadm@example.com') +) + +# misc settings +TIME_ZONE='Europe/Copenhagen' +MEDIA_ROOT='/path/to/media/root/outside/django/root/' +PDF_ARCHIVE_PATH='/usr/local/www/pdf_archive/' + +# PSP settings +EPAY_MERCHANT_NUMBER=123 +EPAY_MD5_SECRET='abc' +COINIFY_API_KEY='123' +COINIFY_API_SECRET='123' +COINIFY_IPN_SECRET='123' + +# shop settings +PDF_LETTERHEAD_FILENAME='letterhead.pdf' +BANKACCOUNT_IBAN='123' +BANKACCOUNT_SWIFTBIC='123' +BANKACCOUNT_REG='123' +BANKACCOUNT_ACCOUNT='123' + +# schedule settings +SCHEDULE_MIDNIGHT_OFFSET_HOURS=6 +SCHEDULE_TIMESLOT_LENGTH_MINUTES=30 + +# irc bot settings +IRCBOT_CHECK_MESSAGE_INTERVAL_SECONDS=60 +IRCBOT_NICK='BornHack' +IRCBOT_SCHEDULE_ANNOUNCE_CHANNEL='#test' +IRCBOT_SERVER_HOSTNAME='ircd.tyknet.dk' +IRCBOT_SERVER_PORT=6697 +IRCBOT_SERVER_USETLS=True + diff --git a/src/bornhack/environment_settings.py.dist b/src/bornhack/environment_settings.py.dist new file mode 100644 index 00000000..66f78794 --- /dev/null +++ b/src/bornhack/environment_settings.py.dist @@ -0,0 +1,65 @@ +# make this a long 100+ chars random string +SECRET_KEY = 'secret' + +# debug settings - remember to set allowed_hosts if debug is disabled +DEBUG = True +ALLOWED_HOSTS = ['localhost', '127.0.0.1'] + +# Database settings +# https://docs.djangoproject.com/en/1.10/ref/settings/#databases +DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql_psycopg2', + 'NAME': 'bornhackdb', + 'USER': 'bornhack', + 'PASSWORD': 'bornhack', + 'HOST': '127.0.0.1', + }, +} + +### changes below here are only needed for production + +# email settings +EMAIL_HOST='mailhost.example.com' +EMAIL_PORT=587 +EMAIL_HOST_USER='mymailuser' +EMAIL_HOST_PASSWORD='mymailpassword' +EMAIL_USE_TLS=True +DEFAULT_FROM_EMAIL='noreply@example.com' +ARCHIVE_EMAIL='archive@example.com' + +ADMINS=( + ('sysadm', 'sysadm@example.com') +) + +# misc settings +TIME_ZONE='Europe/Copenhagen' +MEDIA_ROOT='/path/to/media/root/outside/django/root/' +PDF_ARCHIVE_PATH='/usr/local/www/pdf_archive/' + +# PSP settings +EPAY_MERCHANT_NUMBER=123 +EPAY_MD5_SECRET='abc' +COINIFY_API_KEY='123' +COINIFY_API_SECRET='123' +COINIFY_IPN_SECRET='123' + +# shop settings +PDF_LETTERHEAD_FILENAME='letterhead.pdf' +BANKACCOUNT_IBAN='123' +BANKACCOUNT_SWIFTBIC='123' +BANKACCOUNT_REG='123' +BANKACCOUNT_ACCOUNT='123' + +# schedule settings +SCHEDULE_MIDNIGHT_OFFSET_HOURS=6 +SCHEDULE_TIMESLOT_LENGTH_MINUTES=30 + +# irc bot settings +IRCBOT_CHECK_MESSAGE_INTERVAL_SECONDS=60 +IRCBOT_NICK='BornHack' +IRCBOT_SCHEDULE_ANNOUNCE_CHANNEL='#test' +IRCBOT_SERVER_HOSTNAME='ircd.tyknet.dk' +IRCBOT_SERVER_PORT=6697 +IRCBOT_SERVER_USETLS=True + diff --git a/src/bornhack/settings.py b/src/bornhack/settings.py index 8a7a12d1..5c5945ec 100644 --- a/src/bornhack/settings.py +++ b/src/bornhack/settings.py @@ -1,8 +1,5 @@ import os - -import environ -env = environ.Env() -environ.Env.read_env() +from .environment_settings import * def local_dir(entry): return os.path.join( @@ -13,15 +10,9 @@ def local_dir(entry): WSGI_APPLICATION = 'bornhack.wsgi.application' ROOT_URLCONF = 'bornhack.urls' -SECRET_KEY = env('SECRET_KEY') -ALLOWED_HOSTS = env('ALLOWED_HOSTS').split(',') SITE_ID = 1 -DATABASES = { - 'default': env.db(), -} - ADMINS = ( ('bornhack sysadm', 'sysadm@bornhack.org'), ) @@ -54,9 +45,7 @@ INSTALLED_APPS = [ STATIC_URL = '/static/' STATIC_ROOT = local_dir('static') STATICFILES_DIRS = [local_dir('static_src')] -MEDIA_ROOT = env('MEDIA_ROOT') LANGUAGE_CODE = 'en-us' -TIME_ZONE = env('TIME_ZONE') #USE_I18N = True #USE_L10N = True USE_TZ = True @@ -84,18 +73,6 @@ TEMPLATES = [ }, ] - -MIDDLEWARE_CLASSES = [ - 'django.contrib.sessions.middleware.SessionMiddleware', - 'django.middleware.common.CommonMiddleware', - 'django.middleware.csrf.CsrfViewMiddleware', - 'django.contrib.auth.middleware.AuthenticationMiddleware', - 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', - 'django.contrib.messages.middleware.MessageMiddleware', - 'django.middleware.clickjacking.XFrameOptionsMiddleware', - 'django.middleware.security.SecurityMiddleware', -] - LOGIN_REDIRECT_URL = 'profiles:detail' AUTHENTICATION_BACKENDS = ( @@ -118,38 +95,10 @@ BOOTSTRAP3 = { 'javascript_url': '/static/js/bootstrap.min.js' } -EPAY_MERCHANT_NUMBER = env('EPAY_MERCHANT_NUMBER') -EPAY_MD5_SECRET = env('EPAY_MD5_SECRET') - -COINIFY_API_KEY = env('COINIFY_API_KEY') -COINIFY_API_SECRET = env('COINIFY_API_SECRET') -COINIFY_IPN_SECRET = env('COINIFY_IPN_SECRET') - -LETTERHEAD_PDF_PATH = os.path.join(local_dir('static_src'), 'pdf', env('PDF_LETTERHEAD_FILENAME')) -PDF_ARCHIVE_PATH='/usr/local/www/pdf_archive/' - -BANKACCOUNT_IBAN = env('BANKACCOUNT_IBAN') -BANKACCOUNT_SWIFTBIC = env('BANKACCOUNT_SWIFTBIC') -BANKACCOUNT_REG = env('BANKACCOUNT_REG') -BANKACCOUNT_ACCOUNT = env('BANKACCOUNT_ACCOUNT') - -TICKET_CATEGORY_ID = env('TICKET_CATEGORY_ID') - -DEBUG = env('DEBUG') if DEBUG: EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' INSTALLED_APPS += ['debug_toolbar', ] - MIDDLEWARE_CLASSES += ['debug_toolbar.middleware.DebugToolbarMiddleware', ] - -else: - EMAIL_HOST = env('EMAIL_HOST') - EMAIL_PORT = env('EMAIL_PORT') - EMAIL_HOST_USER = env('EMAIL_HOST_USER') - EMAIL_HOST_PASSWORD = env('EMAIL_HOST_PASSWORD') - EMAIL_USE_TLS = env('EMAIL_USE_TLS') - DEFAULT_FROM_EMAIL = env('DEFAULT_FROM_EMAIL') - SERVER_EMAIL = env('DEFAULT_FROM_EMAIL') - ARCHIVE_EMAIL = env('ARCHIVE_EMAIL') + MIDDLEWARE = ['debug_toolbar.middleware.DebugToolbarMiddleware'] LOGGING = { 'version': 1, @@ -173,7 +122,3 @@ LOGGING = { } } -# schedule settings -SCHEDULE_MIDNIGHT_OFFSET_HOURS=int(env('SCHEDULE_MIDNIGHT_OFFSET_HOURS')) -SCHEDULE_TIMESLOT_LENGTH_MINUTES=int(env('SCHEDULE_TIMESLOT_LENGTH_MINUTES')) - diff --git a/src/ircbot/admin.py b/src/ircbot/admin.py index 8c38f3f3..91278423 100644 --- a/src/ircbot/admin.py +++ b/src/ircbot/admin.py @@ -1,3 +1,5 @@ from django.contrib import admin +from .models import * + +admin.site.register(OutgoingIrcMessage) -# Register your models here. diff --git a/src/ircbot/irc3module.py b/src/ircbot/irc3module.py index feb28369..93f1e4d9 100644 --- a/src/ircbot/irc3module.py +++ b/src/ircbot/irc3module.py @@ -63,29 +63,39 @@ class Plugin(object): if settings.DEBUG: print("inside on_kick(), kwargs: %s" % kwargs) - ############################################################################################### ### custom irc3 methods + @irc3.extend def get_outgoing_messages(self): """ This method gets unprocessed OutgoingIrcMessage objects and attempts to send them to the target channel. Messages are skipped if the bot is not in the channel. """ - # TODO: handle privmsg to users - # TODO: set a timeout for messages.. a few minutes maybe? - # TODO: make sleep time configurable print("inside get_outgoing_messages()") for msg in OutgoingIrcMessage.objects.filter(processed=False).order_by('created_date'): + # if this message expired mark it as expired and processed without doing anything + if msg.timeout < timezone.now(): + # this message is expired + msg.expired=True + msg.processed=True + msg.save() + continue + + # is this message for a channel or a nick? if msg.target[0] == "#" and msg.target in self.bot.channels: print("sending privmsg to %s: %s" % (msg.target, msg.message)) self.bot.privmsg(msg.target, msg.message) msg.processed=True msg.save() + elif msg.target: + self.bot.privmsg(msg.target, msg.message) + msg.processed=True + msg.save() else: - print("skipping message to channel %s because the bot is not in the channel" % msg.target) + print("skipping message to %s" % msg.target) # call this function again in 60 seconds - self.bot.loop.call_later(60, self.bot.get_outgoing_messages) + self.bot.loop.call_later(settings.IRCBOT_CHECK_MESSAGE_INTERVAL_SECONDS, self.bot.get_outgoing_messages) diff --git a/src/ircbot/management/commands/ircbot.py b/src/ircbot/management/commands/ircbot.py index a9ceb799..52ce7e69 100644 --- a/src/ircbot/management/commands/ircbot.py +++ b/src/ircbot/management/commands/ircbot.py @@ -16,11 +16,11 @@ class Command(BaseCommand): self.output('IRC bot worker running...') # connect to IRC config = { - 'nick': 'BornHack', - 'autojoins': ['#tirsdagsfilm'], - 'host': 'ircd.tyknet.dk', - 'port': 6697, - 'ssl': True, + 'nick': settings.IRCBOT_NICK, + 'autojoins': [settings.IRCBOT_SCHEDULE_ANNOUNCE_CHANNEL], + 'host': settings.IRCBOT_SERVER_HOSTNAME, + 'port': settings.IRCBOT_SERVER_PORT, + 'ssl': settings.IRCBOT_SERVER_USETLS, 'timeout': 30, 'includes': [ 'ircbot.irc3module', diff --git a/src/ircbot/migrations/0002_outgoingircmessage_timeout.py b/src/ircbot/migrations/0002_outgoingircmessage_timeout.py new file mode 100644 index 00000000..16e455bc --- /dev/null +++ b/src/ircbot/migrations/0002_outgoingircmessage_timeout.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.5 on 2017-01-31 17:49 +from __future__ import unicode_literals + +import datetime +from django.db import migrations, models +from django.utils.timezone import utc + + +class Migration(migrations.Migration): + + dependencies = [ + ('ircbot', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='outgoingircmessage', + name='timeout', + field=models.DateTimeField(default=datetime.datetime(2017, 1, 31, 17, 49, 36, 925459, tzinfo=utc)), + preserve_default=False, + ), + ] diff --git a/src/ircbot/migrations/0003_outgoingircmessage_expired.py b/src/ircbot/migrations/0003_outgoingircmessage_expired.py new file mode 100644 index 00000000..83d1c666 --- /dev/null +++ b/src/ircbot/migrations/0003_outgoingircmessage_expired.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.5 on 2017-01-31 20:03 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('ircbot', '0002_outgoingircmessage_timeout'), + ] + + operations = [ + migrations.AddField( + model_name='outgoingircmessage', + name='expired', + field=models.BooleanField(default=False), + ), + ] diff --git a/src/ircbot/models.py b/src/ircbot/models.py index 2875a150..3fdc4ecb 100644 --- a/src/ircbot/models.py +++ b/src/ircbot/models.py @@ -1,4 +1,4 @@ - +from django.core.exceptions import ValidationError from utils.models import UUIDModel, CreatedUpdatedModel from django.db import models @@ -7,4 +7,15 @@ class OutgoingIrcMessage(CreatedUpdatedModel): target = models.CharField(max_length=100) message = models.CharField(max_length=200) processed = models.BooleanField(default=False) + timeout = models.DateTimeField() + expired = models.BooleanField(default=False) + + def __str__(self): + return "PRIVMSG %s %s (%s)" % (self.target, self.message, 'processed' if self.processed else 'unprocessed') + + def clean(self): + if not self.pk: + # this is a new outgoing message being saved + if self.timeout < timezone.now(): + raise ValidationError({'timeout': 'The timeout can not be in the past'}) diff --git a/src/requirements.txt b/src/requirements.txt index 64adb22b..bcaa54ec 100644 --- a/src/requirements.txt +++ b/src/requirements.txt @@ -11,7 +11,6 @@ django-allauth==0.30.0 django-bleach==0.3.0 django-bootstrap3==8.1.0 django-debug-toolbar==1.6 -django-environ==0.4.1 django-wkhtmltopdf==3.1.0 docopt==0.6.2 future==0.16.0