def delete(request, id): photo=get_object_or_404(Photo, pk=id) u=request.user if (photo.user!=u) and (not is_superuser(u)): return HttpResponseRedirect('/photos/check/%s/' % photo.id) user_id=photo.user.id try: if photo.show_type=='s': for t in photo.tags.all(): t.obj_count=t.obj_count-1 if len(t.photo_set.all())>1:#=1为当前图片 t.save() else: t.delete() for t in photo.usertag_set.all(): t.obj_count=t.obj_count-1 if len(t.photos.all())>1: t.save() else: t.delete() import os os.remove(settings.PHOTO_ROOT+photo.photo_name()) os.remove(settings.PHOTO_ROOT+photo.thumb_big_photo_name()) os.remove(settings.PHOTO_ROOT+photo.thumb_small_photo_name()) except Exception, e: pass
def delete(request, id): photo = get_object_or_404(Photo, pk=id) u = request.user if (photo.user != u) and (not is_superuser(u)): return HttpResponseRedirect('/photos/check/%s/' % photo.id) user_id = photo.user.id try: if photo.show_type == 's': for t in photo.tags.all(): t.obj_count = t.obj_count - 1 if len(t.photo_set.all()) > 1: #=1为当前图片 t.save() else: t.delete() for t in photo.usertag_set.all(): t.obj_count = t.obj_count - 1 if len(t.photos.all()) > 1: t.save() else: t.delete() import os os.remove(settings.PHOTO_ROOT + photo.photo_name()) os.remove(settings.PHOTO_ROOT + photo.thumb_big_photo_name()) os.remove(settings.PHOTO_ROOT + photo.thumb_small_photo_name()) except Exception, e: pass
def view_photo(request, id): """ 查看图片 """ photo = get_object_or_404(Photo, pk=id) user = request.user if photo.show_type == 'h' and not is_superuser(user): raise Http404 context_map = get_base_context_map(request) #TODO 评论翻页 comments = photo.photocomment_set.all() if not is_superuser(user): comments = comments.filter(show_type='s') user_s_other_photo = photo.user.photo_set.all() if not is_superuser(user): user_s_other_photo = user_s_other_photo.filter(show_type='s') user_s_other_photo = user_s_other_photo.order_by("-create_date")[0:8] form = PhotoCommentForm() if request.method == 'POST': """ 添加评论 """ #TODO 登录判断 if user.is_anonymous(): return HttpResponseRedirect('/photos/%s/#beginComment' % id) form = PhotoCommentForm(request.POST, photo=photo, author=user) if form.is_valid(): form.save() return HttpResponseRedirect('/photos/%s/#beginComment' % id) else: photo.view_count = photo.view_count + 1 photo.save() context_map['form'] = form context_map['photo'] = photo context_map['user_s_other_photo'] = user_s_other_photo context_map['comments'] = comments context_map['user'] = user #TODO 查看图片是否已经收藏过了 context_map['has_collection'] = get_count( """ select count(*) from userpanel_collectionphoto uc where uc.photo_id=%s and uc.user_id=%s """, [photo.id, user.id]) if user == photo.user or is_superuser(user): context_map['can_edit'] = True return render_to_response('photos/view_photo.html', context_map)
def view_photo(request, id): """ 查看图片 """ photo=get_object_or_404(Photo, pk=id) user = request.user if photo.show_type=='h' and not is_superuser(user): raise Http404 context_map=get_base_context_map(request) #TODO 评论翻页 comments=photo.photocomment_set.all() if not is_superuser(user): comments=comments.filter(show_type='s') user_s_other_photo = photo.user.photo_set.all() if not is_superuser(user): user_s_other_photo = user_s_other_photo.filter(show_type='s') user_s_other_photo = user_s_other_photo.order_by("-create_date")[0:8] form = PhotoCommentForm() if request.method == 'POST': """ 添加评论 """ #TODO 登录判断 if user.is_anonymous(): return HttpResponseRedirect('/photos/%s/#beginComment' % id) form = PhotoCommentForm(request.POST,photo=photo,author=user) if form.is_valid(): form.save() return HttpResponseRedirect('/photos/%s/#beginComment' % id) else: photo.view_count=photo.view_count+1 photo.save() context_map['form']=form context_map['photo']=photo context_map['user_s_other_photo']=user_s_other_photo context_map['comments']=comments context_map['user']=user #TODO 查看图片是否已经收藏过了 context_map['has_collection']=get_count(""" select count(*) from userpanel_collectionphoto uc where uc.photo_id=%s and uc.user_id=%s """, [photo.id, user.id]) if user==photo.user or is_superuser(user): context_map['can_edit']=True return render_to_response('photos/view_photo.html', context_map)
def check(request, id): """ 确认图片(添加/编辑后) """ photo = get_object_or_404(Photo, pk=id) user = request.user if photo.show_type == 'h' and not is_superuser(user): raise Http404 context_map = get_base_context_map(request) context_map['photo'] = photo return render_to_response('photos/check.html', context_map)
def check(request, id): """ 确认图片(添加/编辑后) """ photo=get_object_or_404(Photo, pk=id) user = request.user if photo.show_type=='h' and not is_superuser(user): raise Http404 context_map=get_base_context_map(request) context_map['photo']=photo return render_to_response('photos/check.html', context_map)
def photos(request, user_id=0): context_map,user,view_user=\ init_context(request, user_id=user_id, context_map={}) self_photos = view_user.photo_set.all() if not is_superuser(user): self_photos = self_photos.filter(show_type='s') self_photos = self_photos.order_by("-create_date") #context_map['self_photos']=self_photos return object_list(request, self_photos, paginate_by=20, template_name='space/photos.html', extra_context=context_map, allow_empty=True)
def tags(request, user_id=0, tag_id=0): context_map,user,view_user=\ init_context(request, user_id=user_id, context_map={}) usertags=view_user.usertag_set.all()[:100] context_map['usertags']=usertags if tag_id: usertag=UserTag.objects.get(pk=tag_id) context_map['tag']=usertag photos=usertag.photos.all() else: photos = view_user.photo_set.all() if not is_superuser(user): photos = photos.filter(show_type='s') photos = photos.order_by("-create_date") #context_map['self_photos']=self_photos return object_list(request, photos, paginate_by=20, template_name='space/tags.html', extra_context=context_map, allow_empty=True)
def tags(request, user_id=0, tag_id=0): context_map,user,view_user=\ init_context(request, user_id=user_id, context_map={}) usertags = view_user.usertag_set.all()[:100] context_map['usertags'] = usertags if tag_id: usertag = UserTag.objects.get(pk=tag_id) context_map['tag'] = usertag photos = usertag.photos.all() else: photos = view_user.photo_set.all() if not is_superuser(user): photos = photos.filter(show_type='s') photos = photos.order_by("-create_date") #context_map['self_photos']=self_photos return object_list(request, photos, paginate_by=20, template_name='space/tags.html', extra_context=context_map, allow_empty=True)