def delete(self): #only the author can delete if self.author == threadlocals.get_current_user(): filename=settings.MEDIA_ROOT+self.image if os.path.exists(filename): os.remove(filename) super(Photo, self).delete()
def save(self): #only the author can edit if self.id and self.author == threadlocals.get_current_user(): super(Photo, self).save() #the photo is new if not self.id: super(Photo, self).save()
def index(request, template, user=None, tag=None, home=False): context = RequestContext(request) ## the logged user logged_as = threadlocals.get_current_user() photo_list = Photo.objects.all() title = "Last uploaded" ## Uploading a new image if request.POST: newform = newPhotoForm(request.POST, request.FILES) if newform.is_valid(): newform.save() return HttpResponseRedirect("/yourphotos/") try: page = request.__getitem__("page") except KeyError: page = 1 ## Filtering by user if user: try: id = User.objects.get(username=user) photo_list = Photo.objects.filter(author=id) title = user + " pictures" except User.DoesNotExist: title = "No photoes by " + user ## Filtering by tag if tag: photo_list = TaggedItem.objects.get_by_model(Photo, tag) title = 'Photoes tagged<br />"' + tag + '"' ## The user photoes if home: photo_list = Photo.objects.filter(author=logged_as) title = "Your photoes" ## Pagination page number page_num = page newform = newPhotoForm() return render_to_response(template, locals(), context_instance=context)