Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Django upgrade 3.2 #21

Merged
merged 15 commits into from
Sep 28, 2023
Merged
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ jobs:

strategy:
matrix:
python: [3.7, 3.8]
django: [2.2]
python: ["3.8", "3.9", "3.10"]
django: ["3.2"]

steps:
- uses: actions/checkout@v2
Expand Down
26 changes: 26 additions & 0 deletions .github/workflows/pypi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Upload Python Package

on:
release:
types: [created]

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.8'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine
- name: Build and publish
env:
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
run: |
python setup.py sdist bdist_wheel
twine upload dist/*
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
boto3==1.9.129
Django==2.2.12
django-fsm==2.6.0
boto3==1.18.63
Django==3.2.8
django-fsm==2.8.0
2 changes: 1 addition & 1 deletion s3_file_uploads/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.0.6'
__version__ = '0.0.7'
22 changes: 12 additions & 10 deletions s3_file_uploads/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from django.db import models
from django.urls import reverse
from django_fsm import FSMField, transition, ConcurrentTransitionMixin
from model_utils import Choices

from s3_file_uploads.aws import S3AssetHandler

Expand All @@ -12,19 +11,22 @@


class UploadedFile(ConcurrentTransitionMixin, models.Model):
UPLOAD_STATES = Choices(
('NEW', 'New'),
('AWAIT_COMPLETE', 'Awaiting Completion'),
('COMPLETED', 'Completed')
)
NEW = 'NEW'
AWAIT_COMPLETE = 'AWAIT_COMPLETE'
COMPLETED = 'COMPLETED'
UPLOAD_STATES = [
(NEW, 'New'),
(AWAIT_COMPLETE, 'Awaiting Completion'),
(COMPLETED, 'Completed'),
]

id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
created = models.DateTimeField(auto_now_add=True, db_index=True)
modified = models.DateTimeField(auto_now=True)

file_key = models.CharField(max_length=255, blank=True)
filename = models.CharField(max_length=255, blank=True)
file_upload_state = FSMField(default=UPLOAD_STATES.NEW)
file_upload_state = FSMField(default=NEW)

user = models.ForeignKey(
USER_MODEL,
Expand Down Expand Up @@ -52,19 +54,19 @@ def get_upload_form(self, *args, **kwargs) -> dict:
self.save()
return form

@transition(field=file_upload_state, source=[UPLOAD_STATES.NEW, UPLOAD_STATES.AWAIT_COMPLETE], target=UPLOAD_STATES.AWAIT_COMPLETE)
@transition(field=file_upload_state, source=[NEW, AWAIT_COMPLETE], target=AWAIT_COMPLETE)
def trans_get_upload_form(self, *args, **kwargs) -> dict:
return self.asset_handler.get_upload_form(*args, **kwargs)

def completed_upload(self) -> None:
self.trans_completed_upload()
self.save()

@transition(field=file_upload_state, source=UPLOAD_STATES.AWAIT_COMPLETE, target=UPLOAD_STATES.COMPLETED)
@transition(field=file_upload_state, source=AWAIT_COMPLETE, target=COMPLETED)
def trans_completed_upload(self) -> None:
pass

@transition(field=file_upload_state, source=[UPLOAD_STATES.NEW, UPLOAD_STATES.COMPLETED], target=UPLOAD_STATES.AWAIT_COMPLETE)
@transition(field=file_upload_state, source=[NEW, COMPLETED], target=AWAIT_COMPLETE)
def permit_reupload(self) -> None:
# by default we don't allow re-uploading, but you can explicitly call
# this to allow it. Useful for when something invalid was uploaded and
Expand Down
4 changes: 2 additions & 2 deletions s3_file_uploads/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ def post(self, request, file_id):
uploaded_file = get_object_or_404(
UploadedFile,
id=file_id,
file_upload_state=UploadedFile.UPLOAD_STATES.AWAIT_COMPLETE
file_upload_state=UploadedFile.AWAIT_COMPLETE
)
uploaded_file.completed_upload()
return Response({
'status': UploadedFile.UPLOAD_STATES.COMPLETED
'status': UploadedFile.COMPLETED
})


Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
author_email = '[email protected]'
license = 'BSD'
install_requires = [
'boto3==1.9.129',
'Django>=1.11',
'django-fsm==2.6.0'
'boto3==1.18.63',
'Django>=3.2',
'django-fsm==2.8.0'
]
long_description_content_type="text/markdown",
long_description = """
Expand Down
5 changes: 2 additions & 3 deletions tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
-r ../requirements.txt
django-model-utils==3.1.2
djangorestframework==3.9.2
model_mommy==1.6.0
djangorestframework==3.12.4
model_mommy==2.0.0
16 changes: 8 additions & 8 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,37 +101,37 @@ def test_complete_url(self, boto_client_mock):
boto_client_mock.return_value.generate_presigned_post.return_value = "https://cat.com/b/a/"
response = self.api_client.post(reverse('s3_file_uploads:upload-file-create'))
new_file = UploadedFile.objects.first()
self.assertEqual(new_file.file_upload_state, UploadedFile.UPLOAD_STATES.AWAIT_COMPLETE)
self.assertEqual(new_file.file_upload_state, UploadedFile.AWAIT_COMPLETE)
response = self.api_client.post(response.data['complete_url'])
self.assertEqual(response.status_code, 200)
new_file.refresh_from_db()
self.assertEqual(new_file.file_upload_state, UploadedFile.UPLOAD_STATES.COMPLETED)
self.assertEqual(new_file.file_upload_state, UploadedFile.COMPLETED)


class UploadedFileUploadCompleteViewTestCase(BaseTestCase):
def setUp(self):
super().setUp()
self.uploaded_file = mommy.make(
UploadedFile,
file_upload_state=UploadedFile.UPLOAD_STATES.AWAIT_COMPLETE
file_upload_state=UploadedFile.AWAIT_COMPLETE
)

def test_completes(self):
self.api_client.post(reverse('s3_file_uploads:upload-file-complete', kwargs={
'file_id': str(self.uploaded_file.id)
}))
self.uploaded_file.refresh_from_db()
self.assertEqual(self.uploaded_file.file_upload_state, UploadedFile.UPLOAD_STATES.COMPLETED)
self.assertEqual(self.uploaded_file.file_upload_state, UploadedFile.COMPLETED)

def test_cant_complete_in_wrong_state(self):
self.uploaded_file.file_upload_state = UploadedFile.UPLOAD_STATES.COMPLETED
self.uploaded_file.file_upload_state = UploadedFile.COMPLETED
self.uploaded_file.save()
response = self.api_client.post(reverse('s3_file_uploads:upload-file-complete', kwargs={
'file_id': str(self.uploaded_file.id)
}))
self.assertEqual(response.status_code, 404)

self.uploaded_file.file_upload_state = UploadedFile.UPLOAD_STATES.NEW
self.uploaded_file.file_upload_state = UploadedFile.NEW
self.uploaded_file.save()
response = self.api_client.post(reverse('s3_file_uploads:upload-file-complete', kwargs={
'file_id': str(self.uploaded_file.id)
Expand All @@ -144,7 +144,7 @@ def setUp(self):
super().setUp()
self.uploaded_file = mommy.make(
UploadedFile,
file_upload_state=UploadedFile.UPLOAD_STATES.COMPLETED,
file_upload_state=UploadedFile.COMPLETED,
file_key='file key',
filename='foo.pdf'
)
Expand All @@ -165,7 +165,7 @@ def setUp(self):
self.in_valid_user = mommy.make(User)
self.uploaded_file = mommy.make(
UploadedFile,
file_upload_state=UploadedFile.UPLOAD_STATES.COMPLETED,
file_upload_state=UploadedFile.COMPLETED,
file_key='file key',
filename='foo.pdf',
user=self.valid_user
Expand Down