diff --git a/pastebin/forms.py b/pastebin/forms.py index ab5a48b..b984c0a 100644 --- a/pastebin/forms.py +++ b/pastebin/forms.py @@ -1,4 +1,5 @@ from django.forms import ModelForm +from django import forms from models import Geometry @@ -6,4 +7,12 @@ from models import Geometry class GeometryForm(ModelForm): class Meta: model = Geometry - fields = ['name', 'description', 'public', 'expiration', 'file', 'sourcefile'] \ No newline at end of file + fields = ['name', 'description', 'public', 'expiration', 'file', 'sourcefile'] + + +class AnonymousGeometryForm(ModelForm): + expiration = forms.ChoiceField(choices=Geometry.EXPIRATION_CHOICES[:-1]) + class Meta: + model = Geometry + fields = ['name', 'description', 'public', 'expiration', 'file'] + diff --git a/pastebin/views.py b/pastebin/views.py index d526034..5239b6e 100644 --- a/pastebin/views.py +++ b/pastebin/views.py @@ -3,7 +3,7 @@ from django.views.generic.detail import DetailView from django.core.urlresolvers import reverse from django.views.generic.base import ContextMixin -from forms import GeometryForm +from forms import GeometryForm, AnonymousGeometryForm from models import Geometry @@ -23,9 +23,22 @@ class GeometryView(DetailView): class GeometryCreate(CreateView, LastesGeometriesMixin): model = Geometry - form_class = GeometryForm template_name = 'pastebin/geometry_create.html' def get_success_url(self): return reverse('geometry_details', kwargs={'id' :self.object.id}) + def get_form_class(self): + if self.request.user.is_authenticated() : + return GeometryForm + else: + return AnonymousGeometryForm + + def form_valid(self, form): + res = super(GeometryCreate, self).form_valid(form) + + if self.request.user.is_authenticated() : + self.object.user = self.request.user + self.object.save() + + return res \ No newline at end of file diff --git a/templates/base.html b/templates/base.html index f54b358..955ac58 100644 --- a/templates/base.html +++ b/templates/base.html @@ -22,9 +22,12 @@