Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class UserProfile(models.Model):
- user = models.OneToOneField(User)
- activation_key = models.CharField(max_length=40, blank=True)
- key_expires = models.DateTimeField(default=datetime.date.today())
- def __str__(self):
- return self.user.username
- class Meta:
- verbose_name_plural='User profiles'
- class ImageDoc(models.Model):
- user = models.ForeignKey(UserProfile)
- imgfile = models.ImageField(upload_to='images/')
- class RegistrationForm(UserCreationForm):
- email = forms.EmailField(required=True, widget=forms.TextInput(attrs={'placeholder': 'E-mail address'}))
- first_name = forms.CharField(required=True)
- last_name = forms.CharField(required=True)
- class Meta:
- model = User
- fields = ('first_name', 'last_name', 'email', 'username', 'password1', 'password2')
- class ImgDocForm(forms.Form):
- user_file = forms.ImageField()
- def clean_user_file(self, *args, **kwargs):
- cleaned_data = super(ImgDocForm,self).clean()
- user_file = cleaned_data.get("user_file")
- if user_file:
- if user_file.size > 5 * 1024 * 1024:
- raise forms.ValidationError("Filesize is too big.")
- if not os.path.splitext(user_file.name)[1].strip().lower() in ['.jpg','.png','.gif','.jpeg']:
- raise forms.ValidationError("File does not look like as picture.")
- return user_file
- class UserForm(forms.Form):
- class Meta:
- model = User
- fields = ['first_name', 'last_name', 'password', 'email', 'username']
- def sign_in(request):
- context = RequestContext(request)
- if request.method == 'POST':
- username = request.POST['username']
- password = request.POST['password']
- user = authenticate(username=username, password=password)
- if user:
- if user.is_active:
- login(request, user)
- return HttpResponseRedirect('/', context)
- else:
- return HttpResponse("Verify your account!")
- else:
- return HttpResponse("Invalid login details supplied.")
- def populateContext(request, context):
- context['authenticated'] = request.user.is_authenticated()
- if context['authenticated'] == True:
- context['username'] = request.user.username
- def index(request):
- context = {}
- populateContext(request, context)
- return render(request, 'index.html', context)
- def upload(request):
- data = {}
- thumb_size = (100,100)
- micro_thumb_size = (50,50)
- if request.method == 'POST':
- userform = ImgDocForm(request.POST, request.FILES)
- if userform.is_valid():
- origin_form = userform.cleaned_data["user_file"]
- origin_name = origin_form.name
- original_file = os.path.join(settings.MEDIA_ROOT, origin_name)
- .
- .
- .
- original_name = ImageDoc(imgfile=origin_name)
- original_name.save()
- .
- .
- .
- userform = ImgDocForm()
- else:
- return HttpResponse('Nooo!')
- else:
- userform = ImgDocForm()
- data.update(image_gallery = ImageDoc.objects.only('imgfile'))
- data.update(userform=userform)
- data.update(csrf(request))
- return render(request, 'upload.html', data)
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- </head>
- <body>
- <div>
- <form method="post" action="" enctype="multipart/form-data">
- {% csrf_token %}
- {{ userform.as_p }}
- <input type="submit">
- </form>
- <br><br>
- <h2>{{ origin_name }} (original)</h2>
- {% if origin_name %}
- <img src="{{ MEDIA_URL }}{{ origin_name }}">
- {% endif %}
- <br><br>
- {% if image_gallery %}
- {% for image in image_gallery %}
- <img src="/{{ image.thumbfile }}">
- {% endfor %}
- {% endif %}
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement