Skip to content

Commit

Permalink
Merge pull request #1070 from opengisch/random_changes
Browse files Browse the repository at this point in the history
A bunch of changes on logging, typing, config and command clean-up
  • Loading branch information
suricactus authored Nov 30, 2024
2 parents 32e421c + e781e1c commit 6c9a85e
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 94 deletions.

This file was deleted.

Empty file.
2 changes: 1 addition & 1 deletion docker-app/qfieldcloud/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1372,7 +1372,7 @@ def owner_can_create_job(self):

def save(self, recompute_storage=False, *args, **kwargs):
self.clean()
logger.info(f"Saving project {self}...")
logger.debug(f"Saving project {self}...")

if recompute_storage:
self.file_storage_bytes = storage.get_project_file_storage_in_bytes(self.id)
Expand Down
2 changes: 1 addition & 1 deletion docker-app/qfieldcloud/core/querysets_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def get_team_members(team):
return TeamMember.objects.filter(team=team)


def get_organization_members(organization) -> "QuerySet[Person]":
def get_organization_members(organization) -> QuerySet[Person]:
org_members = Person.objects.filter(
organizationmember__organization=organization
).annotate(role=F("organizationmember__role"))
Expand Down
48 changes: 0 additions & 48 deletions docker-app/qfieldcloud/core/tests/test_qgis_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
import time
from pathlib import PurePath

from django.core.management import call_command
from django.http import FileResponse
from qfieldcloud.authentication.models import AuthToken
from qfieldcloud.core import utils
from qfieldcloud.core.models import Job, Person, ProcessProjectfileJob, Project
from rest_framework import status
from rest_framework.test import APITransactionTestCase
Expand Down Expand Up @@ -656,52 +654,6 @@ def test_upload_10mb_file(self):
self.assertEqual("bigfile.big", response.json()[0]["name"])
self.assertEqual(response.json()[0]["size"], 10000000)

def test_purge_old_versions_command(self):
"""This tests manual purging of old versions with the management command"""

self.client.credentials(HTTP_AUTHORIZATION="Token " + self.token1.key)

set_subscription(self.user1, storage_keep_versions=3)

def count_versions():
"""counts the versions in first file of project1"""
file = Project.objects.get(pk=self.project1.pk).files[0]
return len(file.versions)

def read_version(n):
"""returns the content of version in first file of project1"""
file = Project.objects.get(pk=self.project1.pk).files[0]
return file.versions[n]._data.get()["Body"].read().decode()

# Create 20 versions (direct upload to s3)
bucket = utils.get_s3_bucket()
key = f"projects/{self.project1.id}/files/file.txt/"
for i in range(20):
test_file = io.BytesIO(f"v{i}".encode())
bucket.upload_fileobj(test_file, key)

# Ensure it worked
self.assertEqual(count_versions(), 20)
self.assertEqual(read_version(0), "v0")
self.assertEqual(read_version(19), "v19")

# Run management command on other project should have no effect
other = Project.objects.create(name="other", owner=self.user1)
call_command("purge_old_file_versions", "--force", "--projects", other.pk)
self.assertEqual(count_versions(), 20)

# Run management command should leave 3
call_command("purge_old_file_versions", "--force")
self.assertEqual(count_versions(), 3)
self.assertEqual(read_version(0), "v17")
self.assertEqual(read_version(2), "v19")

# Run management command is idempotent
call_command("purge_old_file_versions", "--force")
self.assertEqual(count_versions(), 3)
self.assertEqual(read_version(0), "v17")
self.assertEqual(read_version(2), "v19")

def test_purge_old_versions(self):
"""This tests automated purging of old versions when uploading files"""

Expand Down
5 changes: 4 additions & 1 deletion docker-app/qfieldcloud/core/views/files_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import qfieldcloud.core.utils2 as utils2
from django.core.exceptions import ObjectDoesNotExist
from django.db import transaction
from django.conf import settings
from django.utils import timezone
from drf_spectacular.utils import (
OpenApiParameter,
Expand Down Expand Up @@ -82,7 +83,9 @@ def get(self, request: Request, projectid: str) -> Response:

path = PurePath(version.key)
filename = str(path.relative_to(*path.parts[:3]))
last_modified = version.last_modified.strftime("%d.%m.%Y %H:%M:%S %Z")
last_modified = version.last_modified.strftime(
settings.QFIELDCLOUD_STORAGE_DT_LAST_MODIFIED_FORMAT
)
# NOTE ETag is a MD5. But for the multipart uploaded files, the MD5 is computed from the concatenation of the MD5s of each uploaded part.
# TODO make sure when file metadata is in the DB (QF-2760), this is a real md5sum of the current file.
md5sum = version.e_tag.replace('"', "")
Expand Down
4 changes: 4 additions & 0 deletions docker-app/qfieldcloud/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,10 @@ def before_send(event, hint):
# NOTE automatically set when running tests, don't change manually!
IN_TEST_SUITE = False

# The format used in the deprecated field `last_modified` in storage related serializers.
# The settings and `last_modified` field are deprecated and soon to be removed.
QFIELDCLOUD_STORAGE_DT_LAST_MODIFIED_FORMAT = "%d.%m.%Y %H:%M:%S %Z"

QFIELDCLOUD_SUBSCRIPTION_MODEL = os.environ.get(
"QFIELDCLOUD_SUBSCRIPTION_MODEL", "subscription.Subscription"
)
Expand Down
2 changes: 1 addition & 1 deletion docker-qgis/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ def cmd_process_projectfile(args: argparse.Namespace):
)


def main():
def main() -> None:
from qfc_worker.utils import setup_basic_logging_config

setup_basic_logging_config()
Expand Down

0 comments on commit 6c9a85e

Please sign in to comment.