Skip to content

Commit

Permalink
Merge pull request #202 from sumandari/182_wavefront_model
Browse files Browse the repository at this point in the history
Add Wavefronts hub
  • Loading branch information
sumandari authored Nov 9, 2021
2 parents e07461f + 7c76865 commit a6f4c6d
Show file tree
Hide file tree
Showing 30 changed files with 1,075 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ jobs:
- name: Upload coverage to codecov
uses: codecov/codecov-action@v2
with:
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: true
5 changes: 4 additions & 1 deletion dockerize/docker/REQUIREMENTS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,7 @@ djangorestframework==3.11.2
sorl-thumbnail-serializer-field==0.2.1
django-rest-auth==0.9.5
drf-yasg==1.17.1
django-rest-multiple-models==2.1.3
django-rest-multiple-models==2.1.3

django-preferences==1.0.0
PyWavefront==1.3.3
6 changes: 6 additions & 0 deletions qgis-app/base/models/processing_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
import datetime
import uuid
import os

from django.db import models
from django.contrib.auth.models import User
Expand Down Expand Up @@ -121,6 +122,11 @@ def save(self, *args, **kwargs):
self.modified_date = datetime.datetime.now()
super().save(*args, **kwargs)

def delete(self, *args, **kwargs):
if os.path.isfile(self.file.path):
os.remove(self.file.path)
super(Resource, self).delete(*args, **kwargs)

def __str__(self):
return "%s" % (self.name)

Expand Down
28 changes: 27 additions & 1 deletion qgis-app/base/views/processing_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,18 @@ def get_context_data(self, **kwargs):
return context


@method_decorator(never_cache, name='dispatch')
class ResourceBaseCreateView(LoginRequiredMixin,
ResourceBaseContextMixin,
SuccessMessageMixin,
CreateView):
"""Upload a Resource File."""
"""Upload a Resource File.
We don't cache since there's a dynamic preference value on the template
"""

template_name = 'base/upload_form.html'
is_1mb_limit_enable = True

def form_valid(self, form):
self.obj = form.save(commit=False)
Expand All @@ -239,13 +244,29 @@ def get_success_url(self):
url_name = '%s_detail' % self.resource_name_url_base
return reverse(url_name, kwargs={'pk': self.object.id})

def get_context_data(self, **kwargs):
context = super().get_context_data()
context['limit_1mb'] = self.is_1mb_limit_enable
return context


class ResourceBaseDetailView(ResourceBaseContextMixin,
DetailView):
"""Base Class for Resource DetailView."""

context_object_name = 'object_detail'

# js source files
# e.g. js = ({'src': 'path/to/js/under/static/file.js', 'type': 'module'},)
# attribute src is mandatory, type is optional
js = ()

# css source files
# e.g css = ('path/to/css/file1.css', 'path/to/css/file1.css')
css = ()

is_3d_model = False

def get_template_names(self):
object = self.get_object()
if not object.approved:
Expand All @@ -259,6 +280,9 @@ def get_context_data(self, **kwargs):
context = super().get_context_data()
user = self.request.user
context['creator'] = self.object.get_creator_name
context['js'] = self.js
context['css'] = self.css
context['is_3d_model'] = self.is_3d_model
if self.object.review_set.exists():
if self.object.review_set.last().reviewer.first_name:
reviewer = "%s %s" % (
Expand All @@ -271,6 +295,8 @@ def get_context_data(self, **kwargs):
context['reviewer'] = reviewer
if user.is_staff or is_resources_manager(user):
context['form'] = ResourceBaseReviewForm()
if self.is_3d_model:
context['url_viewer'] = "%s_viewer" % self.resource_name_url_base
return context


Expand Down
7 changes: 6 additions & 1 deletion qgis-app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@
os.path.join(SITE_ROOT, "static"),
]

STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]


# Make this unique, and don't share it with anybody.
Expand Down Expand Up @@ -146,7 +150,7 @@
'bootstrapform',
'rest_framework',
'rest_framework_gis',

'preferences',
# styles:
'styles',
]
Expand All @@ -163,6 +167,7 @@
'django.template.context_processors.request',
# ABP: adds DEBUG and BASE_TEMPLATE vars
"qgis_context_processor.additions",
"preferences.context_processors.preferences_cp",
),
},
},
Expand Down
4 changes: 4 additions & 0 deletions qgis-app/settings_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@

'feedjack',

'preferences',

'rest_framework',
'sorl_thumbnail_serializer', # serialize image
'drf_multiple_model',
Expand All @@ -76,6 +78,7 @@
'geopackages',
# models (sharing .model3 file feature)
'models',
'wavefronts'
]

DATABASES = {
Expand Down Expand Up @@ -119,3 +122,4 @@
REST_FRAMEWORK = {
'TEST_REQUEST_DEFAULT_FORMAT': 'json',
}

2 changes: 1 addition & 1 deletion qgis-app/templates/base/confirm_delete.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{% block content %}
<h2>{% trans "Delete"%} {{ resource_name }}: {{ object.name }}</h2>
<form action="" method="post">{% csrf_token %}
<p class="alert alert-danger">{% trans "Are you sure you want to permanently remove this GeoPackage?"%}</p>
<p class="alert alert-danger">{% trans "Are you sure you want to permanently remove this "%}{{ resource_name }}?</p>
<p><input type="submit" class="btn btn-danger" name="delete_confirm" value="{% trans "Delete" %}" /> <a class="btn btn-primary" href="{% url url_list %}">{% trans "Cancel" %}</a></p>
</form>

Expand Down
31 changes: 27 additions & 4 deletions qgis-app/templates/base/detail.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
{% extends 'base/base.html' %}{% load i18n static thumbnail resources_custom_tags%}

{% block extrajs %}
{{ block.super }}
{% for css_src in css %}
<link rel="stylesheet" href="{% static css_src %}" type="text/css" />
{% endfor %}
{% endblock %}

{% block content %}
<h3 class="style-title">{{ object_detail.name }}</h3>
{% if user == object_detail.creator or user.is_staff %}
Expand All @@ -10,16 +17,25 @@ <h3 class="style-title">{{ object_detail.name }}</h3>
{% endif %}
<hr />
<div class="row">
<div class="span4 mb-5">
<div class="span4 mb-5 view-resource">
<div class="style-polaroid">
{% thumbnail object_detail.thumbnail_image "420x420" format="PNG" as im %}
<img class="" alt="{% trans "image" %}" src="{{ im.url }}" width="{{ im.x }}" height="{{ im.y }}" />
<img class="image-resource" alt="{% trans "image" %}" src="{{ im.url }}" width="{{ im.x }}" height="{{ im.y }}" />
{% endthumbnail %}
{% if is_3d_model %}
<div class="middle">
{% thumbnail object_detail.thumbnail_image "150x150" format="PNG" as im %}
<img alt="{% trans "image" %}" src="{% static 'wavefront/img/cube-3d.png' %}" width="{{ im.x }}" height="{{ im.y }}" />
{% endthumbnail %}
</div>
</div>
<div id="urlView" data-url="{{ obj_url }}" data-mtl-url="{{ mtl_url }}"></div>

{% else %}
</div>

{% endif %}
</div>
<div class="span6">
<div class="span6 info-resource">
<dl class="dl-horizontal">
<dd></dd>
<dt>Name</dt>
Expand Down Expand Up @@ -62,4 +78,11 @@ <h3 class="style-title">{{ object_detail.name }}</h3>
</div>

</div>


{% for js_src in js %}
<script type="{% if not js_src.type %}text/javascript{% else %}{{ js_src.type }}{% endif %}" src="{% static js_src.src %}"></script>
{% endfor %}


{% endblock %}
34 changes: 24 additions & 10 deletions qgis-app/templates/base/review.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
}

</style>
{% for css_src in css %}
<link rel="stylesheet" href="{% static css_src %}" type="text/css" />
{% endfor %}
{% endblock %}

{% block content %}
Expand All @@ -23,14 +26,25 @@ <h3>{{ object_detail.name }} <small>in review</small></h3>
{% endif %}
<hr />
<div class="row">
<div class="span6 text-center mb-5">
<div class="style-polaroid">
{% thumbnail object_detail.thumbnail_image "420x420" format="PNG" as im %}
<img class="style-polaroid" alt="{% trans "Style icon" %}" src="{{ im.url }}" width="{{ im.x }}" height="{{ im.y }}" />
{% endthumbnail %}
<div class="span4 mb-5 view-resource">
<div class="style-polaroid">
{% thumbnail object_detail.thumbnail_image "420x420" format="PNG" as im %}
<img class="image-resource" alt="{% trans "image" %}" src="{{ im.url }}" width="{{ im.x }}" height="{{ im.y }}" />
{% endthumbnail %}
{% if is_3d_model %}
<div class="middle">
{% thumbnail object_detail.thumbnail_image "150x150" format="PNG" as im %}
<img alt="{% trans "image" %}" src="{% static 'wavefront/img/cube-3d.png' %}" width="{{ im.x }}" height="{{ im.y }}" />
{% endthumbnail %}
</div>
</div>
<div id="urlView" data-url="{{ obj_url }}" data-mtl-url="{{ mtl_url }}"></div>

{% else %}
</div>
{% endif %}
</div>
</div>
<div class="span6">
<div class="span6 info-resource">
<dl class="dl-horizontal">
<dt>Name</dt>
<dd>{{ object_detail.name }}</dd>
Expand Down Expand Up @@ -82,7 +96,7 @@ <h3>{{ object_detail.name }} <small>in review</small></h3>
<div class="span12"></div>

</div>
<script>

</script>
{% for js_src in js %}
<script type="{% if not js_src.type %}text/javascript{% else %}{{ js_src.type }}{% endif %}" src="{% static js_src.src %}"></script>
{% endfor %}
{% endblock %}
6 changes: 4 additions & 2 deletions qgis-app/templates/base/upload_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ <h2>{% trans "Upload" %} {{ resource_name }}</h2>

<script type="text/javascript" src="{% static 'js/resource_upload.js' %}"></script>


<script>
// check filesize
$('#id_file').bind('change', function() {
let maxFileSize = 1000000;
let maxFileSize = {% if limit_1mb %}1000000{% else %}{{ preferences.FilesizePreferences.wavefront_filesize_limit }} * 1000000{% endif %};
let fileSize = this.files[0].size
if (fileSize > maxFileSize){
alert('You have selected a file that is more than 1mb, files larger than this are not supported.');
alert(`You have selected a file that is more than ${maxFileSize/1000000}mb, files larger than this are not supported.`);
$('#id_file').val('');
};
});
</script>

{% endblock %}
2 changes: 2 additions & 0 deletions qgis-app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
url(r'^styles/', include('styles.urls')),
url(r'^geopackages/', include('geopackages.urls')),
url(r'^models/', include('models.urls')),
url(r'^wavefronts/', include('wavefronts.urls')),
]

# ABP: temporary home page
Expand Down Expand Up @@ -123,6 +124,7 @@
'/styles/?order_by=-upload_date&&is_gallery=true',
'/geopackages/?order_by=-upload_date&&is_gallery=true',
'/models/?order_by=-upload_date&&is_gallery=true',
'/wavefronts/?order_by=-upload_date&&is_gallery=true',
FlatPage.objects.all(),
simplemenu.models.URLItem.objects.all(),
)
Empty file added qgis-app/wavefronts/__init__.py
Empty file.
27 changes: 27 additions & 0 deletions qgis-app/wavefronts/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from django.contrib import admin
from wavefronts.models import Wavefront, Review

# django-preferences
from preferences.admin import PreferencesAdmin
from wavefronts.models import FilesizePreferences


class WavefrontInline(admin.TabularInline):
model = Review
list_display = ('review_date', 'comment', 'reviewer')


@admin.register(Wavefront)
class WavefrontAdmin(admin.ModelAdmin):
inlines = [WavefrontInline, ]
list_display = ('name', 'description', 'creator', 'upload_date',)
search_fields = ('name', 'description',)


@admin.register(Review)
class WavefrontReviewAdmin(admin.ModelAdmin):
list_display = ('resource', 'reviewer', 'comment', 'review_date',)


# django-preferences
admin.site.register(FilesizePreferences, PreferencesAdmin)
5 changes: 5 additions & 0 deletions qgis-app/wavefronts/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class WavefrontsConfig(AppConfig):
name = 'wavefronts'
34 changes: 34 additions & 0 deletions qgis-app/wavefronts/forms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from django import forms

from wavefronts.models import Wavefront
from wavefronts.validator import WavefrontValidator

from base.forms.processing_forms import ResourceBaseCleanFileForm


class ResourceFormMixin(forms.ModelForm):
class Meta:
model = Wavefront
fields = ['file', 'thumbnail_image', 'name', 'description', ]


class UploadForm(ResourceBaseCleanFileForm, ResourceFormMixin):
"""Upload Form."""

file_path = ''

def clean_file(self):
zip_file = self.cleaned_data['file']
if zip_file:
self.file_path = WavefrontValidator(zip_file).validate_wavefront()
return zip_file


class UpdateForm(ResourceFormMixin):
"""Model Update Form."""

def clean_file(self):
zip_file = self.cleaned_data['file']
if zip_file:
self.file_path = WavefrontValidator(zip_file).validate_wavefront()
return zip_file
Loading

0 comments on commit a6f4c6d

Please sign in to comment.