Skip to content

Commit 78e35bb

Browse files
authored
Update/python base (#13)
* updating python base and pinning django version pip, when upgraded, will install a version that is no longer supporting python 3.5 because of a specific kind of format string. So here we want to upgrade the python in the container to 3.8. I am also pinning the Django version because the latest needs a bit of work to upgrade (and this can be done when needed in another PR) Signed-off-by: vsoch <[email protected]> * black formatting Signed-off-by: vsoch <[email protected]> Co-authored-by: vsoch <[email protected]>
1 parent 18e3571 commit 78e35bb

File tree

11 files changed

+188
-187
lines changed

11 files changed

+188
-187
lines changed

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM python:3.5.1
1+
FROM python:3.8
22
ENV PYTHONUNBUFFERED 1
33

44
################################################################################

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.0.12
1+
0.0.13

push.py

+61-50
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#!/usr/bin/env python
22

3-
'''
3+
"""
44
55
push.py: example of how to push image to nginx upload server!
66
7-
Copyright (C) 2018 Vanessa Sochat.
7+
Copyright (C) 2018-2021 Vanessa Sochat.
88
99
This program is free software: you can redistribute it and/or modify it
1010
under the terms of the GNU Affero General Public License as published by
@@ -19,50 +19,55 @@
1919
You should have received a copy of the GNU Affero General Public License
2020
along with this program. If not, see <https://www.gnu.org/licenses/>.
2121
22-
'''
22+
"""
2323

2424
from __future__ import print_function
25-
from requests_toolbelt.streaming_iterator import StreamingIterator
26-
from requests_toolbelt import (
27-
MultipartEncoder,
28-
MultipartEncoderMonitor
29-
)
25+
from requests_toolbelt import MultipartEncoder, MultipartEncoderMonitor
3026

3127
import requests
3228
import argparse
33-
import hashlib
3429
import sys
3530
import os
3631

3732

3833
def get_parser():
3934
parser = argparse.ArgumentParser(description="Dinosaur Nginx Upload Example")
4035

41-
description = 'example push client to upload files to nginx upload endpoint'
42-
43-
parser.add_argument("--host", dest='host',
44-
help="the host where the server is running",
45-
type=str, default='127.0.0.1')
46-
47-
parser.add_argument("--port", "-p", dest='port',
48-
help="the port where the server is running",
49-
type=str, default='')
50-
51-
parser.add_argument("--schema", "-s", dest='schema',
52-
help="http:// or https://",
53-
type=str, default='http://')
54-
55-
parser.add_argument("file", nargs=1,
56-
help="full path to file to push",
57-
type=str)
36+
description = "example push client to upload files to nginx upload endpoint"
37+
38+
parser.add_argument(
39+
"--host",
40+
dest="host",
41+
help="the host where the server is running",
42+
type=str,
43+
default="127.0.0.1",
44+
)
45+
46+
parser.add_argument(
47+
"--port",
48+
"-p",
49+
dest="port",
50+
help="the port where the server is running",
51+
type=str,
52+
default="",
53+
)
54+
55+
parser.add_argument(
56+
"--schema",
57+
"-s",
58+
dest="schema",
59+
help="http:// or https://",
60+
type=str,
61+
default="http://",
62+
)
63+
64+
parser.add_argument("file", nargs=1, help="full path to file to push", type=str)
5865

5966
return parser
6067

6168

62-
6369
def main():
64-
'''the main entrypoint for pushing!
65-
'''
70+
"""the main entrypoint for pushing!"""
6671

6772
parser = get_parser()
6873

@@ -88,62 +93,68 @@ def main():
8893

8994
def assemble_url(schema, host, port):
9095
if port:
91-
port = ':%s' % port
92-
return '%s%s%s/upload' %(schema, host, port)
96+
port = ":%s" % port
97+
return "%s%s%s/upload" % (schema, host, port)
9398

9499

95100
def push(path, url):
96-
'''push an image to the dinosaur nginx upload server!
97-
98-
Parameters
99-
==========
100-
path: the full path to the image.
101+
"""
102+
Push an image to the dinosaur nginx upload server!
101103
102-
'''
104+
Parameters
105+
==========
106+
path: the full path to the image.
107+
"""
103108

104109
path = os.path.abspath(path)
105110
image = os.path.basename(path)
106-
print("PUSH %s" % path)
107111

108112
if not os.path.exists(path):
109-
print('ERROR: %s does not exist.' %path)
113+
print("ERROR: %s does not exist." % path)
110114
sys.exit(1)
111115

112116
image_size = os.path.getsize(path) >> 20
117+
print("PUSH %s of size %s" % (image, image_size))
113118

114119
upload_to = os.path.basename(path)
115120

116-
encoder = MultipartEncoder(fields={'name': upload_to,
117-
'terminal': "yes",
118-
'file1': (upload_to, open(path, 'rb'), 'text/plain')})
121+
encoder = MultipartEncoder(
122+
fields={
123+
"name": upload_to,
124+
"terminal": "yes",
125+
"file1": (upload_to, open(path, "rb"), "text/plain"),
126+
}
127+
)
119128

120129
progress_callback = create_callback(encoder)
121130
monitor = MultipartEncoderMonitor(encoder, progress_callback)
122-
headers = {'Content-Type': monitor.content_type }
131+
headers = {"Content-Type": monitor.content_type}
123132

124133
try:
125134
r = requests.post(url, data=monitor, headers=headers)
126-
message = r.json()['message']
127-
print('\n[Return status {0} {1}]'.format(r.status_code, message))
135+
message = r.json()["message"]
136+
print("\n[Return status {0} {1}]".format(r.status_code, message))
128137
except KeyboardInterrupt:
129-
print('\nUpload cancelled.')
138+
print("\nUpload cancelled.")
130139
except Exception as e:
131140
print(e)
132141

133142
sys.stdout.write("\n")
134143

135144

136145
def create_callback(encoder):
137-
encoder_len = int(encoder.len / (1024*1024.0))
146+
encoder_len = int(encoder.len / (1024 * 1024.0))
138147
sys.stdout.write("[0 of %s MB]" % (encoder_len))
139148
sys.stdout.flush()
149+
140150
def callback(monitor):
141-
sys.stdout.write('\r')
142-
bytes_read = int(monitor.bytes_read / (1024*1024.0))
151+
sys.stdout.write("\r")
152+
bytes_read = int(monitor.bytes_read / (1024 * 1024.0))
143153
sys.stdout.write("[%s of %s MB]" % (bytes_read, encoder_len))
144154
sys.stdout.flush()
155+
145156
return callback
146157

147158

148-
if __name__ == '__main__':
159+
if __name__ == "__main__":
149160
main()

requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
uwsgi
2-
Django>=2.0.8
2+
Django==2.0.8
33
psycopg2-binary

upload/main/apps.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33

44
class MainConfig(AppConfig):
5-
name = 'main'
5+
name = "main"

upload/main/models.py

+13-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
'''
1+
"""
22
3-
Copyright (C) 2018 Vanessa Sochat.
3+
Copyright (C) 2018-2021 Vanessa Sochat.
44
55
This program is free software: you can redistribute it and/or modify it
66
under the terms of the GNU Affero General Public License as published by
@@ -15,44 +15,40 @@
1515
You should have received a copy of the GNU Affero General Public License
1616
along with this program. If not, see <https://www.gnu.org/licenses/>.
1717
18-
'''
18+
"""
1919

2020
from django.core.files.storage import FileSystemStorage
2121
from django.conf import settings
2222
from django.db import models
23-
import uuid
2423
import time
2524
import hashlib
2625
import os
2726

2827

29-
3028
################################################################################
3129
# Storage
3230

31+
3332
def get_upload_to(instance, filename):
34-
filename = os.path.join(settings.UPLOAD_PATH, instance.upload_id + '.simg')
33+
filename = os.path.join(settings.UPLOAD_PATH, instance.upload_id + ".simg")
3534
return time.strftime(filename)
3635

3736

3837
class OverwriteStorage(FileSystemStorage):
39-
4038
def get_available_name(self, name, max_length=None):
4139
if self.exists(name):
4240
os.remove(os.path.join(settings.MEDIA_ROOT, name))
4341
return name
4442

4543

46-
4744
################################################################################
48-
# Models
49-
45+
# Models
5046

5147

5248
class ImageFile(models.Model):
53-
''' a base image upload to hold a file temporarily during upload
54-
based off of django-chunked-uploads BaseChunkedUpload model
55-
'''
49+
"""a base image upload to hold a file temporarily during upload
50+
based off of django-chunked-uploads BaseChunkedUpload model
51+
"""
5652

5753
file = models.FileField(upload_to=get_upload_to, storage=OverwriteStorage())
5854
filename = models.CharField(max_length=255)
@@ -68,22 +64,21 @@ def get_abspath(self):
6864

6965
@property
7066
def md5(self):
71-
'''calculate the md5 sum of the file'''
72-
if getattr(self, '_md5', None) is None:
67+
"""calculate the md5 sum of the file"""
68+
if getattr(self, "_md5", None) is None:
7369
md5 = hashlib.md5()
7470
for chunk in self.file.chunks():
7571
md5.update(chunk)
7672
self._md5 = md5.hexdigest()
7773
return self._md5
7874

79-
8075
def delete(self, delete_file=True, *args, **kwargs):
81-
'''delete the file and make sure to also delete from storage'''
76+
"""delete the file and make sure to also delete from storage"""
8277
if self.file:
8378
storage, path = self.file.storage, self.file.path
8479
super(ImageFile, self).delete(*args, **kwargs)
8580
if self.file and delete_file:
8681
storage.delete(path)
8782

8883
class Meta:
89-
app_label = 'main'
84+
app_label = "main"

upload/main/utils.py

+26-26
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
'''
1+
"""
22
3-
Copyright (C) 2018 Vanessa Sochat.
3+
Copyright (C) 2018-2021 Vanessa Sochat.
44
55
This program is free software: you can redistribute it and/or modify it
66
under the terms of the GNU Affero General Public License as published by
@@ -15,50 +15,50 @@
1515
You should have received a copy of the GNU Affero General Public License
1616
along with this program. If not, see <https://www.gnu.org/licenses/>.
1717
18-
'''
18+
"""
1919

2020
from upload.main.models import ImageFile
2121
from upload.settings import MEDIA_ROOT
2222
import shutil
23-
import uuid
24-
import json
2523
import os
2624

2725

2826
def move_upload_to_storage(source, name):
29-
'''moving an uploaded file (from nginx module) to storage means.
30-
1. create a folder for the collection, if doesn't exist
31-
2. an image in storage pointing to the moved file
27+
"""moving an uploaded file (from nginx module) to storage means.
28+
1. create a folder for the collection, if doesn't exist
29+
2. an image in storage pointing to the moved file
3230
33-
Parameters
34-
==========
35-
collection: the collection the image will belong to
36-
source: the source file (under /var/www/images/_upload/{0-9}
37-
dest: the destination filename
38-
'''
31+
Parameters
32+
==========
33+
collection: the collection the image will belong to
34+
source: the source file (under /var/www/images/_upload/{0-9}
35+
dest: the destination filename
36+
"""
3937
new_path = os.path.join(MEDIA_ROOT, os.path.basename(name))
4038
shutil.move(source, new_path)
4139
return new_path
4240

4341

4442
def upload_file(name, version, path, size=None):
45-
'''save an uploaded container, usually coming from an ImageUpload
43+
"""save an uploaded container, usually coming from an ImageUpload
4644
47-
Parameters
48-
==========
49-
path: the path to the file uploaded
50-
name: the requested name for the container
51-
version: the md5 sum of the file
52-
size: the file size. if not provided, is calculated
45+
Parameters
46+
==========
47+
path: the path to the file uploaded
48+
name: the requested name for the container
49+
version: the md5 sum of the file
50+
size: the file size. if not provided, is calculated
5351
54-
'''
52+
"""
5553

5654
if os.path.exists(path):
5755
new_path = move_upload_to_storage(path, name)
58-
image = ImageFile.objects.create(file=new_path,
59-
size=size,
60-
version=version,
61-
filename=os.path.basename(new_path))
56+
image = ImageFile.objects.create(
57+
file=new_path,
58+
size=size,
59+
version=version,
60+
filename=os.path.basename(new_path),
61+
)
6262

6363
image.save()
6464
return image

0 commit comments

Comments
 (0)