Advertisement
Guest User

Untitled

a guest
Mar 6th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. class UserProfile(models.Model):
  2. user = models.OneToOneField(User)
  3. activation_key = models.CharField(max_length=40, blank=True)
  4. key_expires = models.DateTimeField(default=datetime.date.today())
  5.  
  6. def __str__(self):
  7. return self.user.username
  8.  
  9. class Meta:
  10. verbose_name_plural='User profiles'
  11.  
  12.  
  13. class ImageDoc(models.Model):
  14. user = models.ForeignKey(UserProfile)
  15. imgfile = models.ImageField(upload_to='images/')
  16.  
  17. class RegistrationForm(UserCreationForm):
  18. email = forms.EmailField(required=True, widget=forms.TextInput(attrs={'placeholder': 'E-mail address'}))
  19. first_name = forms.CharField(required=True)
  20. last_name = forms.CharField(required=True)
  21.  
  22.  
  23. class Meta:
  24. model = User
  25. fields = ('first_name', 'last_name', 'email', 'username', 'password1', 'password2')
  26.  
  27.  
  28. class ImgDocForm(forms.Form):
  29. user_file = forms.ImageField()
  30.  
  31.  
  32. def clean_user_file(self, *args, **kwargs):
  33. cleaned_data = super(ImgDocForm,self).clean()
  34. user_file = cleaned_data.get("user_file")
  35.  
  36. if user_file:
  37. if user_file.size > 5 * 1024 * 1024:
  38. raise forms.ValidationError("Filesize is too big.")
  39.  
  40. if not os.path.splitext(user_file.name)[1].strip().lower() in ['.jpg','.png','.gif','.jpeg']:
  41. raise forms.ValidationError("File does not look like as picture.")
  42.  
  43. return user_file
  44.  
  45.  
  46. class UserForm(forms.Form):
  47. class Meta:
  48. model = User
  49. fields = ['first_name', 'last_name', 'password', 'email', 'username']
  50.  
  51. def sign_in(request):
  52. context = RequestContext(request)
  53.  
  54. if request.method == 'POST':
  55. username = request.POST['username']
  56. password = request.POST['password']
  57. user = authenticate(username=username, password=password)
  58.  
  59. if user:
  60. if user.is_active:
  61. login(request, user)
  62. return HttpResponseRedirect('/', context)
  63. else:
  64. return HttpResponse("Verify your account!")
  65. else:
  66. return HttpResponse("Invalid login details supplied.")
  67.  
  68.  
  69. def populateContext(request, context):
  70. context['authenticated'] = request.user.is_authenticated()
  71. if context['authenticated'] == True:
  72. context['username'] = request.user.username
  73.  
  74.  
  75. def index(request):
  76. context = {}
  77. populateContext(request, context)
  78. return render(request, 'index.html', context)
  79.  
  80.  
  81. def upload(request):
  82. data = {}
  83. thumb_size = (100,100)
  84. micro_thumb_size = (50,50)
  85.  
  86. if request.method == 'POST':
  87. userform = ImgDocForm(request.POST, request.FILES)
  88.  
  89. if userform.is_valid():
  90. origin_form = userform.cleaned_data["user_file"]
  91. origin_name = origin_form.name
  92. original_file = os.path.join(settings.MEDIA_ROOT, origin_name)
  93.  
  94. .
  95. .
  96. .
  97.  
  98. original_name = ImageDoc(imgfile=origin_name)
  99. original_name.save()
  100.  
  101. .
  102. .
  103. .
  104.  
  105. userform = ImgDocForm()
  106. else:
  107. return HttpResponse('Nooo!')
  108.  
  109. else:
  110. userform = ImgDocForm()
  111.  
  112. data.update(image_gallery = ImageDoc.objects.only('imgfile'))
  113. data.update(userform=userform)
  114. data.update(csrf(request))
  115. return render(request, 'upload.html', data)
  116.  
  117. <!DOCTYPE html>
  118. <html lang="en">
  119. <head>
  120. <meta charset="utf-8">
  121. </head>
  122. <body>
  123.  
  124. <div>
  125.  
  126. <form method="post" action="" enctype="multipart/form-data">
  127. {% csrf_token %}
  128. {{ userform.as_p }}
  129. <input type="submit">
  130. </form>
  131.  
  132. <br><br>
  133.  
  134. <h2>{{ origin_name }} (original)</h2>
  135.  
  136. {% if origin_name %}
  137. <img src="{{ MEDIA_URL }}{{ origin_name }}">
  138. {% endif %}
  139.  
  140. <br><br>
  141. {% if image_gallery %}
  142. {% for image in image_gallery %}
  143. <img src="/{{ image.thumbfile }}">
  144. {% endfor %}
  145. {% endif %}
  146. </div>
  147. </body>
  148. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement