Updated import.

see #1030
This commit is contained in:
Antoine Nguyen
2017-02-15 16:36:12 +01:00
parent b9342f7801
commit e8ceadaa1d
4 changed files with 26 additions and 15 deletions
+17 -7
View File
@@ -241,28 +241,38 @@ class Domain(AdminObject):
The expected fields order is the following::
"domain", name, default_mailbox_quota, enabled
"domain", name, quota, default_mailbox_quota, enabled
:param ``core.User`` user: user creating the domain
:param str row: a list containing domain's definition
"""
if len(row) < 4:
if len(row) < 5:
raise BadRequest(_("Invalid line"))
self.name = row[1].strip()
if Domain.objects.filter(name=self.name).exists():
raise Conflict
try:
self.default_mailbox_quota = int(row[2].strip())
self.quota = int(row[2].strip())
except ValueError:
raise BadRequest(
_("Invalid default mailbox quota value for domain '%s'")
% self.name)
self.enabled = (row[3].strip() in ["True", "1", "yes", "y"])
_("Invalid quota value for domain '{}'")
.format(self.name)
)
try:
self.default_mailbox_quota = int(row[3].strip())
except ValueError:
raise BadRequest(
_("Invalid default mailbox quota value for domain '{}'")
.format(self.name)
)
self.enabled = (row[4].strip() in ["True", "1", "yes", "y"])
self.save(creator=user)
def to_csv(self, csvwriter):
csvwriter.writerow([
"domain", self.name, self.default_mailbox_quot, self.enabled])
"domain", self.name, self.quota, self.default_mailbox_quot,
self.enabled
])
for dalias in self.domainalias_set.all():
dalias.to_csv(csvwriter)
@@ -6,7 +6,7 @@
{% blocktrans trimmed %}
Provide a CSV file where lines respect one of the following formats:
<ul>
<li><em>domain; name; default mailbox quota; enabled</em></li>
<li><em>domain; name; quota; default mailbox quota; enabled</em></li>
<li><em>domainalias; name; targeted domain; enabled</em></li>
<li><em>relaydomain; name; target host; target port; service; enabled; verify recipients</em></li>
</ul>
+5 -4
View File
@@ -25,8 +25,8 @@ class ImportTestCase(ModoTestCase):
self.assertEqual(response.status_code, 200)
self.assertIn("Provide a CSV", response.content)
f = ContentFile(b"""domain; domain1.com; 100; True
domain; domain2.com; 200; False
f = ContentFile(b"""domain; domain1.com; 1000; 100; True
domain; domain2.com; 1000; 200; False
domainalias; domalias1.com; domain1.com; True
""", name="domains.csv")
self.client.post(
@@ -36,6 +36,7 @@ domainalias; domalias1.com; domain1.com; True
)
admin = User.objects.get(username="admin")
dom = Domain.objects.get(name="domain1.com")
self.assertEqual(dom.quota, 1000)
self.assertEqual(dom.default_mailbox_quota, 100)
self.assertTrue(dom.enabled)
self.assertTrue(admin.is_owner(dom))
@@ -49,7 +50,7 @@ domainalias; domalias1.com; domain1.com; True
self.assertTrue(admin.is_owner(dom))
def test_import_domains_with_conflict(self):
f = ContentFile(b"""domain;test.alias;10;True
f = ContentFile(b"""domain;test.alias;100;10;True
domainalias;test.alias;test.com;True
""", name="domains.csv")
resp = self.client.post(
@@ -160,7 +161,7 @@ account; user1@test.com; toto; User; One; True; SimpleUsers; user1@test.com; ; t
self.client.logout()
self.client.login(username="admin@test.com", password="toto")
f = ContentFile(b"""
domain; domain2.com; 200; False
domain; domain2.com; 1000; 200; False
""", name="identities.csv")
resp = self.client.post(
reverse("admin:identity_import"),
+3 -3
View File
@@ -147,8 +147,8 @@ class UserLimitImportTestCase(LimitImportTestCase):
username=self.reseller.username, password="toto")
limit = self.reseller.userobjectlimit_set.get(name="domains")
self.assertFalse(limit.is_exceeded())
f = ContentFile(b"""domain; domain1.com; 100; True
domain; domain2.com; 200; False
f = ContentFile(b"""domain; domain1.com; 1000; 100; True
domain; domain2.com; 1000; 200; False
""", name="domains.csv")
self.client.post(
reverse("admin:domain_import"), {
@@ -157,7 +157,7 @@ domain; domain2.com; 200; False
)
self.assertTrue(limit.is_exceeded())
f = ContentFile(b"""domain; domain3.com; 100; True
f = ContentFile(b"""domain; domain3.com; 1000; 100; True
""", name="domains.csv")
response = self.client.post(
reverse("admin:domain_import"), {