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