Skip to content

Commit 962253e

Browse files
committed
[LIB-852] re-work hosting; add HostingFile models; add update_file method;
1 parent 6ab2429 commit 962253e

File tree

2 files changed

+87
-14
lines changed

2 files changed

+87
-14
lines changed

syncano/models/hosting.py

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# -*- coding: utf-8 -*-
2+
3+
from syncano.exceptions import SyncanoRequestError
4+
25
from . import fields
36
from .base import Instance, Model, logger
47

@@ -31,24 +34,51 @@ class Meta:
3134
}
3235

3336
def upload_file(self, path, file):
37+
"""
38+
Upload a new file to the hosting.
39+
:param path: the file path;
40+
:param file: the file to be uploaded;
41+
:return: the response from the API;
42+
"""
3443
files_path = self.links.files
3544
data = {'path': path}
3645
connection = self._get_connection()
37-
params = connection.build_params(params={})
38-
headers = params['headers']
39-
headers.pop('content-type')
40-
response = connection.session.post(connection.host + files_path, headers=headers,
46+
headers = self._prepare_header(connection)
47+
response = connection.session.post('{}{}'.format(connection.host, files_path), headers=headers,
4148
data=data, files=[('file', file)])
4249
if response.status_code != 201:
4350
logger.error(response.text)
4451
return
45-
return response
52+
return HostingFile(**response.json())
53+
54+
def update_file(self, path, file):
55+
"""
56+
Updates an existing file.
57+
:param path: the file path;
58+
:param file: the file to be uploaded;
59+
:return: the response from the API;
60+
"""
61+
hosting_files = self._get_files()
62+
is_found = False
63+
for hosting_file in hosting_files:
64+
if hosting_file.path == path:
65+
is_found = True
66+
break
67+
68+
if not is_found:
69+
raise SyncanoRequestError('File with path {} not found.'.format(path))
4670

47-
def list_files(self):
48-
files_path = self.links.files
4971
connection = self._get_connection()
50-
response = connection.request('GET', files_path)
51-
return [f['path'] for f in response['objects']]
72+
headers = self._prepare_header(connection)
73+
response = connection.session.patch('{}{}'.format(connection.host, hosting_file.links.self), headers=headers,
74+
files=[('file', file)])
75+
if response.status_code != 200:
76+
logger.error(response.text)
77+
return
78+
return HostingFile(**response.json())
79+
80+
def list_files(self):
81+
return self._get_files()
5282

5383
def set_default(self):
5484
default_path = self.links.set_default
@@ -57,3 +87,35 @@ def set_default(self):
5787
response = connection.make_request('POST', default_path)
5888
self.to_python(response)
5989
return self
90+
91+
def _prepare_header(self, connection):
92+
params = connection.build_params(params={})
93+
headers = params['headers']
94+
headers.pop('content-type')
95+
return headers
96+
97+
def _get_files(self):
98+
return [hfile for hfile in HostingFile.please.list(hosting_id=self.id)]
99+
100+
101+
class HostingFile(Model):
102+
"""
103+
OO wrapper around hosting file.
104+
"""
105+
106+
path = fields.StringField(max_length=300)
107+
file = fields.FileField()
108+
links = fields.LinksField()
109+
110+
class Meta:
111+
parent = Hosting
112+
endpoints = {
113+
'detail': {
114+
'methods': ['delete', 'get', 'put', 'patch'],
115+
'path': '/files/{id}/',
116+
},
117+
'list': {
118+
'methods': ['post', 'get'],
119+
'path': '/files/',
120+
}
121+
}

tests/integration_tests_hosting.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,23 @@ def test_create_file(self):
2525
a_hosting_file.write('h1 {color: #541231;}')
2626
a_hosting_file.seek(0)
2727

28-
self.hosting.upload_file(path='styles/main.css', file=a_hosting_file)
29-
30-
files_list = self.hosting.list_files()
31-
32-
self.assertIn('styles/main.css', files_list)
28+
hosting_file = self.hosting.upload_file(path='styles/main.css', file=a_hosting_file)
29+
self.assertEqual(hosting_file.path, 'styles/main.css')
3330

3431
def test_set_default(self):
3532
hosting = self.hosting.set_default()
3633
self.assertIn('default', hosting.domains)
34+
35+
def test_update_file(self):
36+
a_hosting_file = StringIO()
37+
a_hosting_file.write('h1 {color: #541231;}')
38+
a_hosting_file.seek(0)
39+
40+
self.hosting.upload_file(path='styles/main.css', file=a_hosting_file)
41+
42+
a_hosting_file = StringIO()
43+
a_hosting_file.write('h2 {color: #541231;}')
44+
a_hosting_file.seek(0)
45+
46+
hosting_file = self.hosting.update_file(path='styles/main.css', file=a_hosting_file)
47+
self.assertEqual(hosting_file.path, 'styles/main.css')

0 commit comments

Comments
 (0)