We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Adjust initdb to use {POOTLE_TRANSLATION_DIRECTORY} placeholder


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `pootle/core/initdb.py`
Content:
```
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) Pootle contributors.
4 #
5 # This file is a part of the Pootle project. It is distributed under the GPL3
6 # or later license. See the LICENSE file for a copy of the license and the
7 # AUTHORS file for copyright and authorship information.
8 
9 import logging
10 import os
11 
12 from django.conf import settings
13 from django.contrib.auth import get_user_model
14 from django.contrib.auth.models import Permission
15 from django.contrib.contenttypes.models import ContentType
16 from django.db import transaction
17 from django.utils.translation import ugettext_noop as _
18 
19 from pootle.core.contextmanagers import keep_data
20 from pootle.core.models import Revision
21 from pootle.core.signals import update_revisions
22 from pootle_app.models import Directory
23 from pootle_app.models.permissions import PermissionSet, get_pootle_permission
24 from pootle_format.models import Format
25 from pootle_fs.utils import FSPlugin
26 from pootle_language.models import Language
27 from pootle_project.models import Project
28 from staticpages.models import StaticPage as Announcement
29 
30 
31 logger = logging.getLogger(__name__)
32 
33 
34 class InitDB(object):
35 
36     def init_db(self, create_projects=True):
37         with transaction.atomic():
38             with keep_data(signals=(update_revisions, )):
39                 self._init_db(create_projects)
40 
41     def _init_db(self, create_projects=True):
42         """Populate the database with default initial data.
43 
44         This creates the default database to get a working Pootle installation.
45         """
46         self.create_formats()
47         self.create_revision()
48         self.create_essential_users()
49         self.create_root_directories()
50         self.create_template_languages()
51         self.create_default_languages()
52         if create_projects:
53             self.create_terminology_project()
54         self.create_pootle_permissions()
55         self.create_pootle_permission_sets()
56         if create_projects:
57             self.create_default_projects()
58 
59     def create_formats(self):
60         from pootle.core.delegate import formats
61 
62         formats.get().initialize()
63 
64     def _create_object(self, model_klass, **criteria):
65         instance, created = model_klass.objects.get_or_create(**criteria)
66         if created:
67             logger.debug(
68                 "Created %s: '%s'",
69                 instance.__class__.__name__, instance)
70         else:
71             logger.debug(
72                 "%s already exists - skipping: '%s'",
73                 instance.__class__.__name__, instance)
74         return instance, created
75 
76     def _create_pootle_user(self, **criteria):
77         user, created = self._create_object(get_user_model(), **criteria)
78         if created:
79             user.set_unusable_password()
80             user.save()
81         return user
82 
83     def _create_pootle_permission_set(self, permissions, **criteria):
84         permission_set, created = self._create_object(PermissionSet,
85                                                       **criteria)
86         if created:
87             permission_set.positive_permissions.set(permissions)
88             permission_set.save()
89         return permission_set
90 
91     def create_revision(self):
92         Revision.initialize()
93 
94     def create_essential_users(self):
95         """Create the 'default' and 'nobody' User instances.
96 
97         These users are required for Pootle's permission system.
98         """
99         # The nobody user is used to represent an anonymous user in cases
100         # where we need to associate model information with such a user. An
101         # example is in the permission system: we need a way to store rights
102         # for anonymous users; thus we use the nobody user.
103         criteria = {
104             'username': u"nobody",
105             'full_name': u"any anonymous user",
106             'is_active': True,
107         }
108         self._create_pootle_user(**criteria)
109 
110         # The 'default' user represents any valid, non-anonymous user and is
111         # used to associate information any such user. An example is in the
112         # permission system: we need a way to store default rights for users.
113         # We use the 'default' user for this.
114         #
115         # In a future version of Pootle we should think about using Django's
116         # groups to do better permissions handling.
117         criteria = {
118             'username': u"default",
119             'full_name': u"any authenticated user",
120             'is_active': True,
121         }
122         self._create_pootle_user(**criteria)
123 
124     def create_pootle_permissions(self):
125         """Create Pootle's directory level permissions."""
126 
127         args = {
128             'app_label': "pootle_app",
129             'model': "directory",
130         }
131 
132         pootle_content_type = self._create_object(ContentType, **args)[0]
133         pootle_content_type.save()
134 
135         # Create the permissions.
136         permissions = [
137             {
138                 'name': _("Can access a project"),
139                 'codename': "view",
140             },
141             {
142                 'name': _("Cannot access a project"),
143                 'codename': "hide",
144             },
145             {
146                 'name': _("Can make a suggestion for a translation"),
147                 'codename': "suggest",
148             },
149             {
150                 'name': _("Can submit a translation"),
151                 'codename': "translate",
152             },
153             {
154                 'name': _("Can review suggestions"),
155                 'codename': "review",
156             },
157             {
158                 'name': _("Can perform administrative tasks"),
159                 'codename': "administrate",
160             },
161         ]
162 
163         criteria = {
164             'content_type': pootle_content_type,
165         }
166 
167         for permission in permissions:
168             criteria.update(permission)
169             self._create_object(Permission, **criteria)
170 
171     def create_pootle_permission_sets(self):
172         """Create the default permission set for the 'nobody' and 'default' users.
173 
174         'nobody' is the anonymous (non-logged in) user, and 'default' is the
175         logged in user.
176         """
177         User = get_user_model()
178 
179         nobody = User.objects.get(username='nobody')
180         default = User.objects.get(username='default')
181 
182         view = get_pootle_permission('view')
183         suggest = get_pootle_permission('suggest')
184         translate = get_pootle_permission('translate')
185 
186         # Default permissions for tree root.
187         criteria = {
188             'user': nobody,
189             'directory': Directory.objects.root,
190         }
191         self._create_pootle_permission_set([view, suggest], **criteria)
192 
193         criteria['user'] = default
194         self._create_pootle_permission_set(
195             [view, suggest, translate], **criteria)
196 
197         # Default permissions for templates language.
198         # Override with no permissions for templates language.
199         criteria = {
200             'user': nobody,
201             'directory': Directory.objects.get(pootle_path="/templates/"),
202         }
203         self._create_pootle_permission_set([], **criteria)
204 
205         criteria['user'] = default
206         self._create_pootle_permission_set([], **criteria)
207 
208     def require_english(self):
209         """Create the English Language item."""
210         criteria = {
211             'code': "en",
212             'fullname': u"English",
213             'nplurals': 2,
214             'pluralequation': "(n != 1)",
215         }
216         en = self._create_object(Language, **criteria)[0]
217         return en
218 
219     def create_root_directories(self):
220         """Create the root Directory items."""
221         root = self._create_object(Directory, **dict(name=""))[0]
222         self._create_object(Directory, **dict(name="projects", parent=root))
223 
224     def create_template_languages(self):
225         """Create the 'templates' and English languages.
226 
227         The 'templates' language is used to give users access to the
228         untranslated template files.
229         """
230         self._create_object(
231             Language, **dict(code="templates", fullname="Templates"))
232         self.require_english()
233 
234     def create_terminology_project(self):
235         """Create the terminology project.
236 
237         The terminology project is used to display terminology suggestions
238         while translating.
239         """
240         criteria = {
241             'code': "terminology",
242             'fullname': u"Terminology",
243             'source_language': self.require_english(),
244             'checkstyle': "terminology",
245         }
246         po = Format.objects.get(name="po")
247         terminology = self._create_object(Project, **criteria)[0]
248         terminology.filetypes.add(po)
249         terminology.config["pootle_fs.fs_url"] = os.path.join(
250             settings.POOTLE_TRANSLATION_DIRECTORY,
251             terminology.code)
252         terminology.config["pootle_fs.fs_type"] = "localfs"
253         terminology.config["pootle_fs.translation_mappings"] = dict(
254             default="/<language_code>/<dir_path>/<filename>.<ext>")
255         plugin = FSPlugin(terminology)
256         plugin.fetch()
257         plugin.add()
258         plugin.sync()
259 
260     def create_default_projects(self):
261         """Create the default projects that we host.
262 
263         You might want to add your projects here, although you can also add
264         things through the web interface later.
265         """
266         en = self.require_english()
267         po = Format.objects.get(name="po")
268 
269         criteria = {
270             'code': u"tutorial",
271             'source_language': en,
272             'fullname': u"Tutorial",
273             'checkstyle': "standard"}
274         tutorial = self._create_object(Project, **criteria)[0]
275         tutorial.filetypes.add(po)
276         tutorial.config["pootle_fs.fs_url"] = os.path.join(
277             settings.POOTLE_TRANSLATION_DIRECTORY,
278             tutorial.code)
279         tutorial.config["pootle_fs.fs_type"] = "localfs"
280         tutorial.config["pootle_fs.translation_mappings"] = dict(
281             default="/<language_code>/<dir_path>/<filename>.<ext>")
282         plugin = FSPlugin(tutorial)
283         plugin.fetch()
284         plugin.add()
285         plugin.sync()
286         criteria = {
287             'active': True,
288             'title': "Project instructions",
289             'body': (
290                 'Tutorial project where users can play with Pootle and learn '
291                 'more about translation and localisation.\n'
292                 '\n'
293                 'For more help on localisation, visit the [localization '
294                 'guide](http://docs.translatehouse.org/projects/'
295                 'localization-guide/en/latest/guide/start.html).'),
296             'virtual_path': "announcements/projects/"+tutorial.code,
297         }
298         self._create_object(Announcement, **criteria)
299 
300     def create_default_languages(self):
301         """Create the default languages."""
302         from translate.lang import data, factory
303 
304         # import languages from toolkit
305         for code in data.languages.keys():
306             ttk_lang = factory.getlanguage(code)
307             criteria = {
308                 'code': code,
309                 'fullname': ttk_lang.fullname,
310                 'nplurals': ttk_lang.nplurals,
311                 'pluralequation': ttk_lang.pluralequation}
312             if hasattr(ttk_lang, "specialchars"):
313                 criteria['specialchars'] = ttk_lang.specialchars
314             self._create_object(Language, **criteria)
315
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/pootle/core/initdb.py b/pootle/core/initdb.py
--- a/pootle/core/initdb.py
+++ b/pootle/core/initdb.py
@@ -7,9 +7,7 @@
 # AUTHORS file for copyright and authorship information.
 
 import logging
-import os
 
-from django.conf import settings
 from django.contrib.auth import get_user_model
 from django.contrib.auth.models import Permission
 from django.contrib.contenttypes.models import ContentType
@@ -246,9 +244,9 @@
         po = Format.objects.get(name="po")
         terminology = self._create_object(Project, **criteria)[0]
         terminology.filetypes.add(po)
-        terminology.config["pootle_fs.fs_url"] = os.path.join(
-            settings.POOTLE_TRANSLATION_DIRECTORY,
-            terminology.code)
+        terminology.config["pootle_fs.fs_url"] = (
+            "{POOTLE_TRANSLATION_DIRECTORY}%s"
+            % terminology.code)
         terminology.config["pootle_fs.fs_type"] = "localfs"
         terminology.config["pootle_fs.translation_mappings"] = dict(
             default="/<language_code>/<dir_path>/<filename>.<ext>")
@@ -273,9 +271,9 @@
             'checkstyle': "standard"}
         tutorial = self._create_object(Project, **criteria)[0]
         tutorial.filetypes.add(po)
-        tutorial.config["pootle_fs.fs_url"] = os.path.join(
-            settings.POOTLE_TRANSLATION_DIRECTORY,
-            tutorial.code)
+        tutorial.config["pootle_fs.fs_url"] = (
+            "{POOTLE_TRANSLATION_DIRECTORY}%s"
+            % tutorial.code)
         tutorial.config["pootle_fs.fs_type"] = "localfs"
         tutorial.config["pootle_fs.translation_mappings"] = dict(
             default="/<language_code>/<dir_path>/<filename>.<ext>")

{"golden_diff": "diff --git a/pootle/core/initdb.py b/pootle/core/initdb.py\n--- a/pootle/core/initdb.py\n+++ b/pootle/core/initdb.py\n@@ -7,9 +7,7 @@\n # AUTHORS file for copyright and authorship information.\n \n import logging\n-import os\n \n-from django.conf import settings\n from django.contrib.auth import get_user_model\n from django.contrib.auth.models import Permission\n from django.contrib.contenttypes.models import ContentType\n@@ -246,9 +244,9 @@\n         po = Format.objects.get(name=\"po\")\n         terminology = self._create_object(Project, **criteria)[0]\n         terminology.filetypes.add(po)\n-        terminology.config[\"pootle_fs.fs_url\"] = os.path.join(\n-            settings.POOTLE_TRANSLATION_DIRECTORY,\n-            terminology.code)\n+        terminology.config[\"pootle_fs.fs_url\"] = (\n+            \"{POOTLE_TRANSLATION_DIRECTORY}%s\"\n+            % terminology.code)\n         terminology.config[\"pootle_fs.fs_type\"] = \"localfs\"\n         terminology.config[\"pootle_fs.translation_mappings\"] = dict(\n             default=\"/<language_code>/<dir_path>/<filename>.<ext>\")\n@@ -273,9 +271,9 @@\n             'checkstyle': \"standard\"}\n         tutorial = self._create_object(Project, **criteria)[0]\n         tutorial.filetypes.add(po)\n-        tutorial.config[\"pootle_fs.fs_url\"] = os.path.join(\n-            settings.POOTLE_TRANSLATION_DIRECTORY,\n-            tutorial.code)\n+        tutorial.config[\"pootle_fs.fs_url\"] = (\n+            \"{POOTLE_TRANSLATION_DIRECTORY}%s\"\n+            % tutorial.code)\n         tutorial.config[\"pootle_fs.fs_type\"] = \"localfs\"\n         tutorial.config[\"pootle_fs.translation_mappings\"] = dict(\n             default=\"/<language_code>/<dir_path>/<filename>.<ext>\")\n", "issue": "Adjust initdb to use {POOTLE_TRANSLATION_DIRECTORY} placeholder\n\n", "before_files": [{"content": "# -*- coding: utf-8 -*-\n#\n# Copyright (C) Pootle contributors.\n#\n# This file is a part of the Pootle project. It is distributed under the GPL3\n# or later license. See the LICENSE file for a copy of the license and the\n# AUTHORS file for copyright and authorship information.\n\nimport logging\nimport os\n\nfrom django.conf import settings\nfrom django.contrib.auth import get_user_model\nfrom django.contrib.auth.models import Permission\nfrom django.contrib.contenttypes.models import ContentType\nfrom django.db import transaction\nfrom django.utils.translation import ugettext_noop as _\n\nfrom pootle.core.contextmanagers import keep_data\nfrom pootle.core.models import Revision\nfrom pootle.core.signals import update_revisions\nfrom pootle_app.models import Directory\nfrom pootle_app.models.permissions import PermissionSet, get_pootle_permission\nfrom pootle_format.models import Format\nfrom pootle_fs.utils import FSPlugin\nfrom pootle_language.models import Language\nfrom pootle_project.models import Project\nfrom staticpages.models import StaticPage as Announcement\n\n\nlogger = logging.getLogger(__name__)\n\n\nclass InitDB(object):\n\n    def init_db(self, create_projects=True):\n        with transaction.atomic():\n            with keep_data(signals=(update_revisions, )):\n                self._init_db(create_projects)\n\n    def _init_db(self, create_projects=True):\n        \"\"\"Populate the database with default initial data.\n\n        This creates the default database to get a working Pootle installation.\n        \"\"\"\n        self.create_formats()\n        self.create_revision()\n        self.create_essential_users()\n        self.create_root_directories()\n        self.create_template_languages()\n        self.create_default_languages()\n        if create_projects:\n            self.create_terminology_project()\n        self.create_pootle_permissions()\n        self.create_pootle_permission_sets()\n        if create_projects:\n            self.create_default_projects()\n\n    def create_formats(self):\n        from pootle.core.delegate import formats\n\n        formats.get().initialize()\n\n    def _create_object(self, model_klass, **criteria):\n        instance, created = model_klass.objects.get_or_create(**criteria)\n        if created:\n            logger.debug(\n                \"Created %s: '%s'\",\n                instance.__class__.__name__, instance)\n        else:\n            logger.debug(\n                \"%s already exists - skipping: '%s'\",\n                instance.__class__.__name__, instance)\n        return instance, created\n\n    def _create_pootle_user(self, **criteria):\n        user, created = self._create_object(get_user_model(), **criteria)\n        if created:\n            user.set_unusable_password()\n            user.save()\n        return user\n\n    def _create_pootle_permission_set(self, permissions, **criteria):\n        permission_set, created = self._create_object(PermissionSet,\n                                                      **criteria)\n        if created:\n            permission_set.positive_permissions.set(permissions)\n            permission_set.save()\n        return permission_set\n\n    def create_revision(self):\n        Revision.initialize()\n\n    def create_essential_users(self):\n        \"\"\"Create the 'default' and 'nobody' User instances.\n\n        These users are required for Pootle's permission system.\n        \"\"\"\n        # The nobody user is used to represent an anonymous user in cases\n        # where we need to associate model information with such a user. An\n        # example is in the permission system: we need a way to store rights\n        # for anonymous users; thus we use the nobody user.\n        criteria = {\n            'username': u\"nobody\",\n            'full_name': u\"any anonymous user\",\n            'is_active': True,\n        }\n        self._create_pootle_user(**criteria)\n\n        # The 'default' user represents any valid, non-anonymous user and is\n        # used to associate information any such user. An example is in the\n        # permission system: we need a way to store default rights for users.\n        # We use the 'default' user for this.\n        #\n        # In a future version of Pootle we should think about using Django's\n        # groups to do better permissions handling.\n        criteria = {\n            'username': u\"default\",\n            'full_name': u\"any authenticated user\",\n            'is_active': True,\n        }\n        self._create_pootle_user(**criteria)\n\n    def create_pootle_permissions(self):\n        \"\"\"Create Pootle's directory level permissions.\"\"\"\n\n        args = {\n            'app_label': \"pootle_app\",\n            'model': \"directory\",\n        }\n\n        pootle_content_type = self._create_object(ContentType, **args)[0]\n        pootle_content_type.save()\n\n        # Create the permissions.\n        permissions = [\n            {\n                'name': _(\"Can access a project\"),\n                'codename': \"view\",\n            },\n            {\n                'name': _(\"Cannot access a project\"),\n                'codename': \"hide\",\n            },\n            {\n                'name': _(\"Can make a suggestion for a translation\"),\n                'codename': \"suggest\",\n            },\n            {\n                'name': _(\"Can submit a translation\"),\n                'codename': \"translate\",\n            },\n            {\n                'name': _(\"Can review suggestions\"),\n                'codename': \"review\",\n            },\n            {\n                'name': _(\"Can perform administrative tasks\"),\n                'codename': \"administrate\",\n            },\n        ]\n\n        criteria = {\n            'content_type': pootle_content_type,\n        }\n\n        for permission in permissions:\n            criteria.update(permission)\n            self._create_object(Permission, **criteria)\n\n    def create_pootle_permission_sets(self):\n        \"\"\"Create the default permission set for the 'nobody' and 'default' users.\n\n        'nobody' is the anonymous (non-logged in) user, and 'default' is the\n        logged in user.\n        \"\"\"\n        User = get_user_model()\n\n        nobody = User.objects.get(username='nobody')\n        default = User.objects.get(username='default')\n\n        view = get_pootle_permission('view')\n        suggest = get_pootle_permission('suggest')\n        translate = get_pootle_permission('translate')\n\n        # Default permissions for tree root.\n        criteria = {\n            'user': nobody,\n            'directory': Directory.objects.root,\n        }\n        self._create_pootle_permission_set([view, suggest], **criteria)\n\n        criteria['user'] = default\n        self._create_pootle_permission_set(\n            [view, suggest, translate], **criteria)\n\n        # Default permissions for templates language.\n        # Override with no permissions for templates language.\n        criteria = {\n            'user': nobody,\n            'directory': Directory.objects.get(pootle_path=\"/templates/\"),\n        }\n        self._create_pootle_permission_set([], **criteria)\n\n        criteria['user'] = default\n        self._create_pootle_permission_set([], **criteria)\n\n    def require_english(self):\n        \"\"\"Create the English Language item.\"\"\"\n        criteria = {\n            'code': \"en\",\n            'fullname': u\"English\",\n            'nplurals': 2,\n            'pluralequation': \"(n != 1)\",\n        }\n        en = self._create_object(Language, **criteria)[0]\n        return en\n\n    def create_root_directories(self):\n        \"\"\"Create the root Directory items.\"\"\"\n        root = self._create_object(Directory, **dict(name=\"\"))[0]\n        self._create_object(Directory, **dict(name=\"projects\", parent=root))\n\n    def create_template_languages(self):\n        \"\"\"Create the 'templates' and English languages.\n\n        The 'templates' language is used to give users access to the\n        untranslated template files.\n        \"\"\"\n        self._create_object(\n            Language, **dict(code=\"templates\", fullname=\"Templates\"))\n        self.require_english()\n\n    def create_terminology_project(self):\n        \"\"\"Create the terminology project.\n\n        The terminology project is used to display terminology suggestions\n        while translating.\n        \"\"\"\n        criteria = {\n            'code': \"terminology\",\n            'fullname': u\"Terminology\",\n            'source_language': self.require_english(),\n            'checkstyle': \"terminology\",\n        }\n        po = Format.objects.get(name=\"po\")\n        terminology = self._create_object(Project, **criteria)[0]\n        terminology.filetypes.add(po)\n        terminology.config[\"pootle_fs.fs_url\"] = os.path.join(\n            settings.POOTLE_TRANSLATION_DIRECTORY,\n            terminology.code)\n        terminology.config[\"pootle_fs.fs_type\"] = \"localfs\"\n        terminology.config[\"pootle_fs.translation_mappings\"] = dict(\n            default=\"/<language_code>/<dir_path>/<filename>.<ext>\")\n        plugin = FSPlugin(terminology)\n        plugin.fetch()\n        plugin.add()\n        plugin.sync()\n\n    def create_default_projects(self):\n        \"\"\"Create the default projects that we host.\n\n        You might want to add your projects here, although you can also add\n        things through the web interface later.\n        \"\"\"\n        en = self.require_english()\n        po = Format.objects.get(name=\"po\")\n\n        criteria = {\n            'code': u\"tutorial\",\n            'source_language': en,\n            'fullname': u\"Tutorial\",\n            'checkstyle': \"standard\"}\n        tutorial = self._create_object(Project, **criteria)[0]\n        tutorial.filetypes.add(po)\n        tutorial.config[\"pootle_fs.fs_url\"] = os.path.join(\n            settings.POOTLE_TRANSLATION_DIRECTORY,\n            tutorial.code)\n        tutorial.config[\"pootle_fs.fs_type\"] = \"localfs\"\n        tutorial.config[\"pootle_fs.translation_mappings\"] = dict(\n            default=\"/<language_code>/<dir_path>/<filename>.<ext>\")\n        plugin = FSPlugin(tutorial)\n        plugin.fetch()\n        plugin.add()\n        plugin.sync()\n        criteria = {\n            'active': True,\n            'title': \"Project instructions\",\n            'body': (\n                'Tutorial project where users can play with Pootle and learn '\n                'more about translation and localisation.\\n'\n                '\\n'\n                'For more help on localisation, visit the [localization '\n                'guide](http://docs.translatehouse.org/projects/'\n                'localization-guide/en/latest/guide/start.html).'),\n            'virtual_path': \"announcements/projects/\"+tutorial.code,\n        }\n        self._create_object(Announcement, **criteria)\n\n    def create_default_languages(self):\n        \"\"\"Create the default languages.\"\"\"\n        from translate.lang import data, factory\n\n        # import languages from toolkit\n        for code in data.languages.keys():\n            ttk_lang = factory.getlanguage(code)\n            criteria = {\n                'code': code,\n                'fullname': ttk_lang.fullname,\n                'nplurals': ttk_lang.nplurals,\n                'pluralequation': ttk_lang.pluralequation}\n            if hasattr(ttk_lang, \"specialchars\"):\n                criteria['specialchars'] = ttk_lang.specialchars\n            self._create_object(Language, **criteria)\n", "path": "pootle/core/initdb.py"}], "after_files": [{"content": "# -*- coding: utf-8 -*-\n#\n# Copyright (C) Pootle contributors.\n#\n# This file is a part of the Pootle project. It is distributed under the GPL3\n# or later license. See the LICENSE file for a copy of the license and the\n# AUTHORS file for copyright and authorship information.\n\nimport logging\n\nfrom django.contrib.auth import get_user_model\nfrom django.contrib.auth.models import Permission\nfrom django.contrib.contenttypes.models import ContentType\nfrom django.db import transaction\nfrom django.utils.translation import ugettext_noop as _\n\nfrom pootle.core.contextmanagers import keep_data\nfrom pootle.core.models import Revision\nfrom pootle.core.signals import update_revisions\nfrom pootle_app.models import Directory\nfrom pootle_app.models.permissions import PermissionSet, get_pootle_permission\nfrom pootle_format.models import Format\nfrom pootle_fs.utils import FSPlugin\nfrom pootle_language.models import Language\nfrom pootle_project.models import Project\nfrom staticpages.models import StaticPage as Announcement\n\n\nlogger = logging.getLogger(__name__)\n\n\nclass InitDB(object):\n\n    def init_db(self, create_projects=True):\n        with transaction.atomic():\n            with keep_data(signals=(update_revisions, )):\n                self._init_db(create_projects)\n\n    def _init_db(self, create_projects=True):\n        \"\"\"Populate the database with default initial data.\n\n        This creates the default database to get a working Pootle installation.\n        \"\"\"\n        self.create_formats()\n        self.create_revision()\n        self.create_essential_users()\n        self.create_root_directories()\n        self.create_template_languages()\n        self.create_default_languages()\n        if create_projects:\n            self.create_terminology_project()\n        self.create_pootle_permissions()\n        self.create_pootle_permission_sets()\n        if create_projects:\n            self.create_default_projects()\n\n    def create_formats(self):\n        from pootle.core.delegate import formats\n\n        formats.get().initialize()\n\n    def _create_object(self, model_klass, **criteria):\n        instance, created = model_klass.objects.get_or_create(**criteria)\n        if created:\n            logger.debug(\n                \"Created %s: '%s'\",\n                instance.__class__.__name__, instance)\n        else:\n            logger.debug(\n                \"%s already exists - skipping: '%s'\",\n                instance.__class__.__name__, instance)\n        return instance, created\n\n    def _create_pootle_user(self, **criteria):\n        user, created = self._create_object(get_user_model(), **criteria)\n        if created:\n            user.set_unusable_password()\n            user.save()\n        return user\n\n    def _create_pootle_permission_set(self, permissions, **criteria):\n        permission_set, created = self._create_object(PermissionSet,\n                                                      **criteria)\n        if created:\n            permission_set.positive_permissions.set(permissions)\n            permission_set.save()\n        return permission_set\n\n    def create_revision(self):\n        Revision.initialize()\n\n    def create_essential_users(self):\n        \"\"\"Create the 'default' and 'nobody' User instances.\n\n        These users are required for Pootle's permission system.\n        \"\"\"\n        # The nobody user is used to represent an anonymous user in cases\n        # where we need to associate model information with such a user. An\n        # example is in the permission system: we need a way to store rights\n        # for anonymous users; thus we use the nobody user.\n        criteria = {\n            'username': u\"nobody\",\n            'full_name': u\"any anonymous user\",\n            'is_active': True,\n        }\n        self._create_pootle_user(**criteria)\n\n        # The 'default' user represents any valid, non-anonymous user and is\n        # used to associate information any such user. An example is in the\n        # permission system: we need a way to store default rights for users.\n        # We use the 'default' user for this.\n        #\n        # In a future version of Pootle we should think about using Django's\n        # groups to do better permissions handling.\n        criteria = {\n            'username': u\"default\",\n            'full_name': u\"any authenticated user\",\n            'is_active': True,\n        }\n        self._create_pootle_user(**criteria)\n\n    def create_pootle_permissions(self):\n        \"\"\"Create Pootle's directory level permissions.\"\"\"\n\n        args = {\n            'app_label': \"pootle_app\",\n            'model': \"directory\",\n        }\n\n        pootle_content_type = self._create_object(ContentType, **args)[0]\n        pootle_content_type.save()\n\n        # Create the permissions.\n        permissions = [\n            {\n                'name': _(\"Can access a project\"),\n                'codename': \"view\",\n            },\n            {\n                'name': _(\"Cannot access a project\"),\n                'codename': \"hide\",\n            },\n            {\n                'name': _(\"Can make a suggestion for a translation\"),\n                'codename': \"suggest\",\n            },\n            {\n                'name': _(\"Can submit a translation\"),\n                'codename': \"translate\",\n            },\n            {\n                'name': _(\"Can review suggestions\"),\n                'codename': \"review\",\n            },\n            {\n                'name': _(\"Can perform administrative tasks\"),\n                'codename': \"administrate\",\n            },\n        ]\n\n        criteria = {\n            'content_type': pootle_content_type,\n        }\n\n        for permission in permissions:\n            criteria.update(permission)\n            self._create_object(Permission, **criteria)\n\n    def create_pootle_permission_sets(self):\n        \"\"\"Create the default permission set for the 'nobody' and 'default' users.\n\n        'nobody' is the anonymous (non-logged in) user, and 'default' is the\n        logged in user.\n        \"\"\"\n        User = get_user_model()\n\n        nobody = User.objects.get(username='nobody')\n        default = User.objects.get(username='default')\n\n        view = get_pootle_permission('view')\n        suggest = get_pootle_permission('suggest')\n        translate = get_pootle_permission('translate')\n\n        # Default permissions for tree root.\n        criteria = {\n            'user': nobody,\n            'directory': Directory.objects.root,\n        }\n        self._create_pootle_permission_set([view, suggest], **criteria)\n\n        criteria['user'] = default\n        self._create_pootle_permission_set(\n            [view, suggest, translate], **criteria)\n\n        # Default permissions for templates language.\n        # Override with no permissions for templates language.\n        criteria = {\n            'user': nobody,\n            'directory': Directory.objects.get(pootle_path=\"/templates/\"),\n        }\n        self._create_pootle_permission_set([], **criteria)\n\n        criteria['user'] = default\n        self._create_pootle_permission_set([], **criteria)\n\n    def require_english(self):\n        \"\"\"Create the English Language item.\"\"\"\n        criteria = {\n            'code': \"en\",\n            'fullname': u\"English\",\n            'nplurals': 2,\n            'pluralequation': \"(n != 1)\",\n        }\n        en = self._create_object(Language, **criteria)[0]\n        return en\n\n    def create_root_directories(self):\n        \"\"\"Create the root Directory items.\"\"\"\n        root = self._create_object(Directory, **dict(name=\"\"))[0]\n        self._create_object(Directory, **dict(name=\"projects\", parent=root))\n\n    def create_template_languages(self):\n        \"\"\"Create the 'templates' and English languages.\n\n        The 'templates' language is used to give users access to the\n        untranslated template files.\n        \"\"\"\n        self._create_object(\n            Language, **dict(code=\"templates\", fullname=\"Templates\"))\n        self.require_english()\n\n    def create_terminology_project(self):\n        \"\"\"Create the terminology project.\n\n        The terminology project is used to display terminology suggestions\n        while translating.\n        \"\"\"\n        criteria = {\n            'code': \"terminology\",\n            'fullname': u\"Terminology\",\n            'source_language': self.require_english(),\n            'checkstyle': \"terminology\",\n        }\n        po = Format.objects.get(name=\"po\")\n        terminology = self._create_object(Project, **criteria)[0]\n        terminology.filetypes.add(po)\n        terminology.config[\"pootle_fs.fs_url\"] = (\n            \"{POOTLE_TRANSLATION_DIRECTORY}%s\"\n            % terminology.code)\n        terminology.config[\"pootle_fs.fs_type\"] = \"localfs\"\n        terminology.config[\"pootle_fs.translation_mappings\"] = dict(\n            default=\"/<language_code>/<dir_path>/<filename>.<ext>\")\n        plugin = FSPlugin(terminology)\n        plugin.fetch()\n        plugin.add()\n        plugin.sync()\n\n    def create_default_projects(self):\n        \"\"\"Create the default projects that we host.\n\n        You might want to add your projects here, although you can also add\n        things through the web interface later.\n        \"\"\"\n        en = self.require_english()\n        po = Format.objects.get(name=\"po\")\n\n        criteria = {\n            'code': u\"tutorial\",\n            'source_language': en,\n            'fullname': u\"Tutorial\",\n            'checkstyle': \"standard\"}\n        tutorial = self._create_object(Project, **criteria)[0]\n        tutorial.filetypes.add(po)\n        tutorial.config[\"pootle_fs.fs_url\"] = (\n            \"{POOTLE_TRANSLATION_DIRECTORY}%s\"\n            % tutorial.code)\n        tutorial.config[\"pootle_fs.fs_type\"] = \"localfs\"\n        tutorial.config[\"pootle_fs.translation_mappings\"] = dict(\n            default=\"/<language_code>/<dir_path>/<filename>.<ext>\")\n        plugin = FSPlugin(tutorial)\n        plugin.fetch()\n        plugin.add()\n        plugin.sync()\n        criteria = {\n            'active': True,\n            'title': \"Project instructions\",\n            'body': (\n                'Tutorial project where users can play with Pootle and learn '\n                'more about translation and localisation.\\n'\n                '\\n'\n                'For more help on localisation, visit the [localization '\n                'guide](http://docs.translatehouse.org/projects/'\n                'localization-guide/en/latest/guide/start.html).'),\n            'virtual_path': \"announcements/projects/\"+tutorial.code,\n        }\n        self._create_object(Announcement, **criteria)\n\n    def create_default_languages(self):\n        \"\"\"Create the default languages.\"\"\"\n        from translate.lang import data, factory\n\n        # import languages from toolkit\n        for code in data.languages.keys():\n            ttk_lang = factory.getlanguage(code)\n            criteria = {\n                'code': code,\n                'fullname': ttk_lang.fullname,\n                'nplurals': ttk_lang.nplurals,\n                'pluralequation': ttk_lang.pluralequation}\n            if hasattr(ttk_lang, \"specialchars\"):\n                criteria['specialchars'] = ttk_lang.specialchars\n            self._create_object(Language, **criteria)\n", "path": "pootle/core/initdb.py"}]}