Commit c4c90024 authored by onny's avatar onny
Browse files

further improvements to media upload

parent ff5f1b2d
......@@ -16,14 +16,15 @@ Including another URLconf
from django.conf.urls import url, include
from django.contrib import admin
from rest_framework import routers
from openart.views import index_page, add_page, artists_page, projects_page, entries_page, artist_page, login
from openart.views import ArtistViewSet, ProjectViewSet, EntryViewSet
from openart.views import index_page, add_page, artists_page, projects_page, entries_page, artist_page, media_page, login
from openart.views import ArtistViewSet, ProjectViewSet, EntryViewSet, MediaViewSet
# Routers provide an easy way of automatically determining the URL conf.
router = routers.DefaultRouter()
router.register(r'artist', ArtistViewSet)
router.register(r'project', ProjectViewSet)
router.register(r'entry', EntryViewSet)
router.register(r'media', MediaViewSet)
urlpatterns = [
url(r'^admin/', admin.site.urls),
......@@ -33,6 +34,7 @@ urlpatterns = [
url(r'^en/(.*)$', artist_page),
url(r'^projects/', projects_page),
url(r'^entries/', entries_page),
url(r'^media/', media_page),
url(r'^api/', include(router.urls)),
url(r'^login/$', login)
]
......@@ -57,6 +57,10 @@ class MediaForm(forms.ModelForm):
model = Media
fields = ['file', 'entry']
widgets = {
'file': forms.FileInput, #(attrs={'v-model': 'media.file'}), # FIXME
'entry': forms.Select(attrs={'v-model': 'entry.id'})
'file': forms.FileInput(attrs={
'id': 'file'
}),
'entry': forms.Select(attrs={
'v-model': 'entry.id'
})
}
from .models import Artist, Project, Entry
from .models import Artist, Project, Entry, Media
from rest_framework import serializers
class ArtistSerializer(serializers.ModelSerializer):
......@@ -43,3 +43,8 @@ class EntryReadSerializer(serializers.ModelSerializer):
class Meta:
model = Entry
fields = ('id', 'project', 'signatory', 'description', 'dimension', 'visibility', 'type')
class MediaSerializer(serializers.ModelSerializer):
class Meta:
model = Media
fields = ('file', 'entry')
from django.template.response import TemplateResponse
from django.http import HttpResponse
from .models import Artist, Project, Entry
from .models import Artist, Project, Entry, Media
from rest_framework import viewsets
from .serializers import ArtistSerializer, ArtistReadSerializer, ProjectSerializer, ProjectReadSerializer, EntrySerializer, EntryReadSerializer
from .serializers import ArtistSerializer, ArtistReadSerializer, ProjectSerializer, ProjectReadSerializer, EntrySerializer, EntryReadSerializer, MediaSerializer
from .forms import ArtistForm, ProjectForm, EntryForm, MediaForm
from django.conf import settings
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.decorators import login_required
def index_page(request):
html = TemplateResponse(request, 'index.html')
context = {}
context['authenticated'] = request.user.is_authenticated
html = TemplateResponse(request, 'index.html', context)
return HttpResponse(html.render())
@login_required
def add_page(request):
# FIXME remove this if we can upload the file with the rest framework
if request.method == 'POST':
save_path = os.path.join(settings.MEDIA_ROOT, 'uploads', request.FILES['file'])
path = default_storage.save(save_path, request.FILES['file'])
......@@ -27,6 +32,7 @@ def add_page(request):
context['project_form'] = ProjectForm
context['entry_form'] = EntryForm
context['media_form'] = MediaForm
context['authenticated'] = request.user.is_authenticated
html = TemplateResponse(request, 'add.html', context)
return HttpResponse(html.render())
......@@ -34,6 +40,7 @@ def add_page(request):
def artists_page(request):
context = {}
context['artist'] = Artist.objects.all()
context['authenticated'] = request.user.is_authenticated
html = TemplateResponse(request, 'artists.html', context)
return HttpResponse(html.render())
......@@ -41,6 +48,7 @@ def artists_page(request):
def artist_page(request, slug):
context = {}
context['artist'] = Artist.objects.get(slug=slug)
context['authenticated'] = request.user.is_authenticated
html = TemplateResponse(request, 'artist_page.html', context)
return HttpResponse(html.render())
......@@ -48,6 +56,7 @@ def artist_page(request, slug):
def projects_page(request):
context = {}
context['artist'] = Artist.objects.all()
context['authenticated'] = request.user.is_authenticated
html = TemplateResponse(request, 'projects.html', context)
return HttpResponse(html.render())
......@@ -55,10 +64,19 @@ def projects_page(request):
def entries_page(request):
context = {}
context['entry'] = Entry.objects.all()
context['authenticated'] = request.user.is_authenticated
html = TemplateResponse(request, 'entries.html', context)
return HttpResponse(html.render())
def media_page(request):
context = {}
context['media'] = Media.objects.all()
context['authenticated'] = request.user.is_authenticated
html = TemplateResponse(request, 'media.html', context)
return HttpResponse(html.render())
class ArtistViewSet(viewsets.ModelViewSet):
queryset = Artist.objects.all()
......@@ -83,7 +101,15 @@ class EntryViewSet(viewsets.ModelViewSet):
return EntryReadSerializer
return EntrySerializer
class MediaViewSet(viewsets.ModelViewSet):
queryset = Media.objects.all()
serializer_class = MediaSerializer
def login(request):
context = {}
context['authenticated'] = request.user.is_authenticated
if request.method == 'POST':
form = UserCreationForm(request.POST)
if form.is_valid():
......@@ -96,5 +122,7 @@ def login(request):
else:
form = UserCreationForm()
html = TemplateResponse(request, 'login.html', {'form': form})
context['form'] = form
html = TemplateResponse(request, 'login.html', context)
return HttpResponse(html.render())
......@@ -19,7 +19,8 @@ var demo = new Vue({
'apptitle': 'openArt',
'artist': [],
'project': [],
'entry': []
'entry': [],
'media': []
},
delimiters: ["[[","]]"],
methods: {
......@@ -28,7 +29,7 @@ var demo = new Vue({
name: this.artist.name.trim()
};
axios
.post('http://127.0.0.1:8000/api/artist/', newArtist)
.post('/api/artist/', newArtist)
.then(function(response){
print_message('.artist', 'success', 'Artist successfully added');
})
......@@ -50,7 +51,7 @@ var demo = new Vue({
dateto: this.project.dateto
};
axios
.post('http://127.0.0.1:8000/api/project/', newProject)
.post('/api/project/', newProject)
.then(function(response){
print_message('.project', 'success', 'Project successfully added');
})
......@@ -78,7 +79,7 @@ var demo = new Vue({
type: this.entry.type
};
axios
.post('http://127.0.0.1:8000/api/entry/', newEntry)
.post('/api/entry/', newEntry)
.then(function(response){
print_message('.entry', 'success', 'Entry successfully added');
})
......@@ -87,8 +88,12 @@ var demo = new Vue({
});
},
addMedia: function () {
/* const file = event.target.files[0]
axios.post('/add', file, {
newMedia = {
file: document.getElementById("file"),
entry: this.entry.id
}
console.log(newMedia);
axios.post('/api/media/', file, {
headers: {
'Content-Type': 'multipart/form-data'
}
......@@ -98,23 +103,28 @@ var demo = new Vue({
})
.catch(error => {
print_message('.media', 'error', 'Error while uploading media, please report');
}); */
});
},
removeArtist: function (index) {
axios
.delete('http://127.0.0.1:8000/api/artist/'.concat(this.artist[index].id));
.delete('/api/artist/'.concat(this.artist[index].id));
this.artist.splice(index, 1);
return true;
},
removeProject: function (index) {
axios
.delete('http://127.0.0.1:8000/api/project/'.concat(this.project[index].id));
.delete('/api/project/'.concat(this.project[index].id));
this.project.splice(index, 1);
},
removeEntry: function (index) {
axios
.delete('http://127.0.0.1:8000/api/entry/'.concat(this.entry[index].id));
.delete('/api/entry/'.concat(this.entry[index].id));
this.entry.splice(index, 1);
},
removeMedia: function (index) {
axios
.delete('/api/media/'.concat(this.media[index].id));
this.media.splice(index, 1);
}
},
mounted: function()
......@@ -122,19 +132,24 @@ var demo = new Vue({
switch (window.location.pathname) {
case "/artists/":
axios
.get('http://127.0.0.1:8000/api/artist/')
.get('/api/artist/')
.then(response => (this.artist = response.data));
break;
case "/projects/":
axios
.get('http://127.0.0.1:8000/api/project/')
.get('/api/project/')
.then(response => (this.project = response.data));
break;
case "/entries/":
axios
.get('http://127.0.0.1:8000/api/entry/')
.get('/api/entry/')
.then(response => (this.entry = response.data));
break;
case "/media/":
axios
.get('/api/media/')
.then(response => (this.media = response.data));
break;
};
}
});
......@@ -32,8 +32,11 @@
<div id="sidebar">
<a href="/artists">Artists</a><br>
<a href="/projects">Projects</a><br>
<a href="/entries">Entries</a><br><br>
<a href="/entries">Entries</a><br>
<a href="/media">Media</a><br><br>
{% if authenticated %}
<a href="/add">Add</a><br>
{% endif %}
</div>
<main>
{% block jobs %}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment