Changeset - 2bd2c02967d9
[Not reviewed]
default
0 2 0
Dennis Fink - 11 years ago 2014-07-15 19:52:46
dennis.fink@c3l.lu
Really fix mail send and version bump
2 files changed with 2 insertions and 2 deletions:
0 comments (0 inline, 0 general)
ennstatus/status/functions.py
Show inline comments
 
import os
 
import os.path
 
import json
 

	
 
from collections import defaultdict
 
from datetime import datetime
 

	
 
from flask import current_app
 
from flask_mail import Mail, Message
 

	
 

	
 
mail = Mail()
 

	
 

	
 
def _send_mail(server_name, status, last_updated):
 

	
 
    current_app.logger.debug('Try sending mail')
 
    try:
 
        msg = Message('[Ennstatus] %s went %s' % (server_name, status),
 
                      sender='ennstatus@enn.lu')
 
        msg.add_recipient(current_app.config['SERVER_ADMINS'][server_name])
 
        msg.body = ('%s went to %s. I received the last update at %s'
 
                    % (server_name, status, last_updated))
 
        mail.send(msg)
 
    except KeyError:
 
        current_app.logger.error('Admin for %s not found!' % server_name)
 
    except AssertionError:
 
        pass
 
    except Exception as e:
 
        current_app.logger.error('Unexpected error: %s' % e,
 
                                 exc_info=True)
 
    current_app.logger.debug('Finished trying!')
 

	
 

	
 
def _check_server(data):
 

	
 
    server_name = data['server_name']
 
    last_updated = data['last_updated']
 
    filename = os.path.join('data', '.'.join([server_name.lower(), 'json']))
 

	
 
    date = datetime.strptime(last_updated, '%d-%m-%Y %H:%M:%S')
 
    now = datetime.utcnow()
 
    delta = now - date
 

	
 
    if delta.days >= 7:
 
        os.remove(filename)
 
        current_app.logger.error('%s was removed!' % server_name)
 
        return False
 
    elif data['server_status'] == 'Offline':
 
        return data
 

	
 
    if delta.seconds >= 3600:
 
        status = 'Offline'
 
    elif delta.seconds >= 1200:
 
        status = 'Unknown'
 
    else:
 
        status = None
 

	
 
    if status is not None:
 
        current_app.logger.error('%s is set to %s' % (server_name,
 
                                 status))
 

	
 
        if data['server_status'] != 'Unknown' and status != 'Offline':
 
        if data['server_status'] == 'Unknown' and status == 'Offline':
 
            _send_mail(server_name, status, last_updated)
 

	
 
        for key in ('server_status', 'tor_status'):
 
            data[key] = status
 

	
 
        with open(filename, mode='w', encoding='utf-8') as file_object:
 
            json.dump(data, file_object)
 

	
 
    return data
 

	
 

	
 
def _load_single_server(filename):
 

	
 
    try:
 
        with open(filename, encoding='utf-8') as f:
 
            server = json.load(f)
 
    except IOError:
 
        return False
 

	
 
    server = _check_server(server)
 
    return server
 

	
 

	
 
def single_server(name):
 

	
 
    filename = ''.join(['data/', name, '.json'])
 
    return _load_single_server(filename)
 

	
 

	
 
def _get_json_files(root, files):
 

	
 
    for f in files:
 
        if f.endswith('.json'):
 
            yield os.path.join(root, f)
 

	
 

	
 
def all_servers():
 

	
 
    for root, _, files in os.walk('data'):
 
        for f in _get_json_files(root, files):
 
            yield _load_single_server(f)
 

	
 

	
 
def all_servers_by_type(type):
 

	
 
    for server in all_servers():
 
        try:
 
            if server['server_type'] == type:
 
                yield server
 
        except TypeError:
 
            continue
 

	
 

	
 
def split_all_servers_to_types():
 

	
 
    servers = defaultdict(list)
 

	
 
    for server in all_servers():
 
        try:
 
            servers[server['server_type']].append(server)
 
        except TypeError:
 
            continue
 

	
 
    return servers
setup.py
Show inline comments
 
from setuptools import setup, find_packages
 

	
 

	
 
def _get_requirements():
 

	
 
    with open('requirements.in', encoding='utf-8') as f:
 
        lines = f.readlines()
 

	
 
    lines = [line[:-1] for line in lines if not line.startswith('#')]
 
    return lines
 

	
 

	
 
setup(name='Ennstatus',
 
      version='4.1.2',
 
      version='4.1.3',
 
      description=('Ennstatus provides the user with vital information about '
 
                   'the status of the organizations Tor servers.'),
 
      author='Frënn vun der Ënn',
 
      author_email='info@enn.lu',
 
      url='https://bitbucket.org/fvde/ennstatus',
 
      license='GPLv3+',
 
      packages=find_packages(),
 
      package_data={'ennstatus': ['static/css/*.css',
 
                                  'static/css/flags/img/png/*.png',
 
                                  'static/css/flags/img/gif/*.gif',
 
                                  'static/css/flags/*.css',
 
                                  'static/files/*',
 
                                  'static/images/*',
 
                                  'static/videos/*',
 
                                  'templates/*.html',
 
                                  'templates/api/export/xml/*.xml',
 
                                  'templates/donate/*.html',
 
                                  'templates/root/*.html',
 
                                  'templates/status/*.html',
 
                                  'templates/stats/*.html',
 
                                  'templates/errorpages/*.html',
 
                                  ]},
 
      install_requires=_get_requirements(),
 
      classifiers=['Development Status :: 5 - Production/Stable',
 
                   'Environment :: Web Environment',
 
                   'Operating System :: POSIX',
 
                   'Programming Language :: Python',
 
                   'Programming Language :: Python :: 3',
 
                   'Programming Language :: Python :: 3.3',
 
                   'Topic :: Internet',
 
                   'Topic :: Internet :: WWW/HTTP',
 
                   'Topic :: Internet :: WWW/HTTP :: Dynamic Content',
 
                   'Topic :: Internet :: WWW/HTTP :: WSGI',
 
                   'Topic :: Internet :: WWW/HTTP :: WSGI :: Application',
 
                   ('License :: OSI Approved :: '
 
                    'GNU General Public License v3 or later (GPLv3+)'),
 
                   ]
 
      )
0 comments (0 inline, 0 general)