]> git.ipfire.org Git - ipfire.org.git/blame - src/backend/base.py
Support sending HTML emails
[ipfire.org.git] / src / backend / base.py
CommitLineData
a6dc0bad
MT
1#!/usr/bin/python
2
c5ddbd67 3import configparser
11347e46 4import io
23f84bbc 5import tornado.httpclient
11347e46
MT
6
7from . import accounts
05e8c602 8from . import blog
d73bba54 9from . import campaigns
11347e46
MT
10from . import database
11from . import geoip
12from . import fireinfo
13from . import iuse
e28b082e 14from . import memcached
d6df53bf 15from . import messages
11347e46
MT
16from . import mirrors
17from . import netboot
18from . import nopaste
372ef119 19from . import ratelimit
11347e46
MT
20from . import releases
21from . import settings
22from . import talk
611adbfb 23from . import tweets
181d08f3 24from . import wiki
2c361abc 25from . import zeiterfassung
d6df53bf 26from .decorators import *
2c361abc 27
11347e46 28DEFAULT_CONFIG = io.StringIO("""
9ed02e3b
MT
29[global]
30debug = false
31
32data_dir =
33static_dir = %(data_dir)s/static
34templates_dir = %(data_dir)s/templates
35""")
36
a6dc0bad 37class Backend(object):
2cd9af74 38 def __init__(self, configfile, debug=False):
9068dba1
MT
39 # Read configuration file.
40 self.config = self.read_config(configfile)
9ed02e3b
MT
41
42 # Enable debug logging if configured
43 self.debug = debug or self.config.getboolean("global", "debug")
9068dba1
MT
44
45 # Setup database.
46 self.setup_database()
a6dc0bad 47
23f84bbc
MT
48 # Create HTTPClient
49 self.http_client = tornado.httpclient.AsyncHTTPClient(
50 defaults = {
51 "User-Agent" : "IPFireWebApp",
52 }
53 )
9068dba1
MT
54 # Initialize settings first.
55 self.settings = settings.Settings(self)
e28b082e 56 self.memcache = memcached.Memcached(self)
9068dba1
MT
57
58 # Initialize backend modules.
a6dc0bad 59 self.accounts = accounts.Accounts(self)
9068dba1 60 self.geoip = geoip.GeoIP(self)
66862195 61 self.fireinfo = fireinfo.Fireinfo(self)
9068dba1
MT
62 self.iuse = iuse.IUse(self)
63 self.mirrors = mirrors.Mirrors(self)
64 self.netboot = netboot.NetBoot(self)
66862195 65 self.nopaste = nopaste.Nopaste(self)
9068dba1 66 self.releases = releases.Releases(self)
66862195 67 self.talk = talk.Talk(self)
9068dba1 68
0a6875dc 69 self.blog = blog.Blog(self)
181d08f3 70 self.wiki = wiki.Wiki(self)
2c361abc
MT
71 self.zeiterfassung = zeiterfassung.ZeiterfassungClient(self)
72
9068dba1
MT
73 def read_config(self, configfile):
74 cp = configparser.ConfigParser()
9ed02e3b
MT
75
76 # Initialize configuration with some sensible defaults
77 cp.readfp(DEFAULT_CONFIG)
78
79 # Parse file
9068dba1
MT
80 cp.read(configfile)
81
82 return cp
83
84 def setup_database(self):
85 """
86 Sets up the database connection.
87 """
88 credentials = {
89 "host" : self.config.get("database", "server"),
90 "database" : self.config.get("database", "database"),
91 "user" : self.config.get("database", "username"),
92 "password" : self.config.get("database", "password"),
93 }
94
95 self.db = database.Connection(**credentials)
c5ddbd67 96
9fdf4fb7 97 async def run_task(self, task, *args, **kwargs):
c5ddbd67 98 tasks = {
b2059099 99 "check-mirrors" : self.mirrors.check_all,
06dfa22d 100 "check-spam" : self.accounts.check_spam,
8e69850a 101 "cleanup" : self.cleanup,
d73bba54
MT
102 "launch-campaigns" : self.campaigns.launch_manually,
103 "run-campaigns" : self.campaigns.run,
e136d583 104 "scan-files" : self.releases.scan_files,
05873be1 105 "send-message" : self.messages.send_cli,
d6df53bf 106 "send-all-messages" : self.messages.queue.send_all,
8f94e19f 107 "test-blacklist" : self.geoip.test_blacklist,
30e11b1b 108 "test-ldap" : self.accounts.test_ldap,
611adbfb 109 "tweet" : self.tweets.tweet,
c5ddbd67
MT
110 "update-blog-feeds" : self.blog.update_feeds,
111 }
112
113 # Get the task from the list of all tasks
114 func = tasks.get(task, None)
115 if not func:
116 raise ValueError("Unknown task: %s" % task)
117
118 # Run the task
9fdf4fb7 119 r = await func(*args, **kwargs)
c5ddbd67
MT
120
121 # If any error code has been returned,
122 # we will end the program
123 if r:
124 raise SystemExit(r)
d6df53bf 125
d73bba54
MT
126 @lazy_property
127 def campaigns(self):
128 return campaigns.Campaigns(self)
129
d8b04c72
MT
130 @lazy_property
131 def groups(self):
132 return accounts.Groups(self)
133
d6df53bf
MT
134 @lazy_property
135 def messages(self):
136 return messages.Messages(self)
611adbfb 137
372ef119
MT
138 @lazy_property
139 def ratelimiter(self):
140 return ratelimit.RateLimiter(self)
141
611adbfb
MT
142 @lazy_property
143 def tweets(self):
144 return tweets.Tweets(self)
8e69850a 145
9fdf4fb7 146 async def cleanup(self):
8e69850a
MT
147 # Cleanup message queue
148 with self.db.transaction():
149 self.messages.queue.cleanup()
150
151 # Cleanup in accounts
152 with self.db.transaction():
153 self.accounts.cleanup()