示例#1
0
文件: views.py 项目: shagtv/blog
def index(request):
    form = PinForm(request.POST or None)
    if form.is_valid():
        instance = form.save(commit=False)
        instance.user = request.user
        instance.save()
        messages.success(request, "Successfully created")
        return HttpResponseRedirect(instance.get_absolute_url())
    return render(request, 'pin/index.html', {"form": form})
示例#2
0
文件: views.py 项目: carsor007/kasa
def popular(request):
    ROW_PER_PAGE = 20

    post_list = Post.objects.all().order_by('-like')
    paginator = Paginator(post_list, ROW_PER_PAGE)

    try:
        offset = int(request.GET.get('older', 1))
    except ValueError:
        offset = 1

    try:
        latest_items = paginator.page(offset)
    except PageNotAnInteger:
        latest_items = paginator.page(1)
    except EmptyPage:
        return HttpResponse(0)

    form = PinForm()

    if request.is_ajax():
        return render_to_response('pin/_items.html', {
            'latest_items': latest_items,
            'pin_form': form,
            'offset': latest_items.next_page_number
        },
                                  context_instance=RequestContext(request))

    else:
        return render_to_response('pin/home.html', {
            'latest_items': latest_items,
            'offset': latest_items.next_page_number
        },
                                  context_instance=RequestContext(request))
示例#3
0
文件: views.py 项目: carsor007/kasa
def home(request):

    try:
        timestamp = int(request.GET.get('older', 0))
    except ValueError:
        timestamp = 0

    if timestamp == 0:
        latest_items = Post.objects.all().order_by('-timestamp')[:20]
    else:
        latest_items = Post.objects.all().extra(
            where=['timestamp<%s'],
            params=[timestamp]).order_by('-timestamp')[:20]

    form = PinForm()

    if request.is_ajax():
        if latest_items.exists():
            return render_to_response('pin/_items.html', {
                'latest_items': latest_items,
                'pin_form': form
            },
                                      context_instance=RequestContext(request))
        else:
            return HttpResponse(0)
    else:
        return render_to_response('pin/home.html',
                                  {'latest_items': latest_items},
                                  context_instance=RequestContext(request))
示例#4
0
def user(request, user_id):
    user = get_object_or_404(User, pk=user_id)
    try:
        profile = Profile.objects.get(user=user)
    except Profile.DoesNotExist:
        profile = Profile.objects.none()
    try:
        timestamp = int(request.GET.get('older', 0))
    except ValueError:
        timestamp = 0
    
    if timestamp == 0:
        latest_items = Post.objects.all().filter(user=user_id).order_by('-timestamp')[:20]
    else:
        latest_items = Post.objects.all().filter(user=user_id).extra(where=['timestamp<%s'], params=[timestamp]).order_by('-timestamp')[:20]
    
    form = PinForm()
    
    if request.is_ajax():
        if latest_items.exists():
            return render_to_response('pin/_items.html', 
                              {'latest_items': latest_items,'pin_form':form},
                              context_instance=RequestContext(request))
        else:
            return HttpResponse(0)
    else:
        
        follow_status = Follow.objects.filter(follower=request.user.id, following=latest_items[0].user.id).count()
        
        return render_to_response('pin/user.html', 
                              {'latest_items': latest_items, 'follow_status':follow_status,
                               'profile':profile,
                               'cur_user':user},
                              context_instance=RequestContext(request))
示例#5
0
def following(request):
    
    try:
        timestamp = int(request.GET.get('older', 0))
    except ValueError:
        timestamp = 0
    
    if timestamp == 0:
        stream = Stream.objects.filter(user=request.user).order_by('-date')[:20]
    else:
        stream = Stream.objects.filter(user=request.user).extra(where=['date<%s'], params=[timestamp]).order_by('-date')[:20]
    
    idis = []
    for p in stream:
        idis.append(p.post_id)
    
    latest_items = Post.objects.filter(id__in=idis).all()
    
    objects = dict([(obj.id, obj) for obj in latest_items])
    sorted_objects = [objects[id] for id in idis]
    
    form = PinForm()
    
    if request.is_ajax():
        if latest_items.exists():
            return render_to_response('pin/_items.html', 
                              {'latest_items': sorted_objects,'pin_form':form},
                              context_instance=RequestContext(request))
        else:
            return HttpResponse(0)
    else:
        return render_to_response('pin/home.html', 
                              {'latest_items': sorted_objects},
                              context_instance=RequestContext(request))
示例#6
0
def send(request):
    if request.method == "POST":
        post_values = request.POST.copy()
        tags = post_values['tags']
        post_values['tags'] = tags[tags.find("[") + 1:tags.find("]")]
        form = PinForm(post_values)
        if form.is_valid():
            model = form.save(commit=False)
            filename = model.image
            image_o = "%s/pin/temp/o/%s" % (MEDIA_ROOT, filename)
            image_on = "%s/pin/images/o/%s" % (MEDIA_ROOT, filename)

            copyfile(image_o, image_on)

            model.image = "pin/images/o/%s" % (filename)
            model.timestamp = time()
            model.user = request.user
            model.save()

            form.save_m2m()

            if model.status == 1:
                msg = 'مطلب شما با موفقیت ارسال شد. <a href="%s">مشاهده</a>' %\
                    reverse('pin-item', args=[model.id])
                messages.add_message(request, messages.SUCCESS, msg)
            elif model.status == 0:
                msg = 'مطلب شما با موفقیت ارسال شد و بعد از تایید در سایت نمایش داده می شود '
                messages.add_message(request, messages.SUCCESS, msg)

            return HttpResponseRedirect('/pin/')
    else:
        form = PinForm()

    category = Category.objects.all()

    if request.is_ajax():
        return render(request, 'pin/_send.html', {
            'form': form,
            'category': category
        })
    else:
        return render(request, 'pin/send.html', {
            'form': form,
            'category': category
        })
示例#7
0
文件: views.py 项目: carsor007/kasa
def tag(request, keyword):
    ROW_PER_PAGE = 20

    tag = get_object_or_404(Tag, slug=keyword)
    content_type = ContentType.objects.get_for_model(Post)
    tag_items = TaggedItem.objects.filter(tag_id=tag.id,
                                          content_type=content_type)

    paginator = Paginator(tag_items, ROW_PER_PAGE)

    try:
        offset = int(request.GET.get('older', 1))
    except ValueError:
        offset = 1

    try:
        tag_items = paginator.page(offset)
    except PageNotAnInteger:
        tag_items = paginator.page(1)
    except EmptyPage:
        return HttpResponse(0)

    s = []
    for t in tag_items:
        s.append(t.object_id)

    latest_items = Post.objects.filter(id__in=s).all()

    form = PinForm()

    if request.is_ajax():
        if latest_items.exists():
            return render_to_response('pin/_items.html', {
                'latest_items': latest_items,
                'pin_form': form,
                'offset': tag_items.next_page_number
            },
                                      context_instance=RequestContext(request))
        else:
            return HttpResponse(0)
    else:
        return render_to_response('pin/tag.html', {
            'latest_items': latest_items,
            'tag': tag,
            'offset': tag_items.next_page_number
        },
                                  context_instance=RequestContext(request))
示例#8
0
文件: views.py 项目: carsor007/kasa
def send(request):
    if request.method == "POST":
        post_values = request.POST.copy()
        tags = post_values['tags']
        post_values['tags'] = tags[tags.find("[") + 1:tags.find("]")]
        form = PinForm(post_values)
        if form.is_valid():
            model = form.save(commit=False)

            filename = model.image

            image_o = "%s/pin/temp/o/%s" % (MEDIA_ROOT, filename)

            image_on = "%s/pin/images/o/%s" % (MEDIA_ROOT, filename)

            copyfile(image_o, image_on)

            model.image = "pin/images/o/%s" % (filename)
            model.timestamp = time.time()
            model.user = request.user
            model.save()

            form.save_m2m()

            return HttpResponseRedirect('/pin/')
    else:
        form = PinForm()

    user_category = Category.objects.filter(user_id=request.user.id).all()

    if request.is_ajax():
        return render_to_response('pin/_send.html', {
            'form': form,
            'user_category': user_category
        },
                                  context_instance=RequestContext(request))
    else:
        return render_to_response('pin/send.html', {
            'form': form,
            'user_category': user_category
        },
                                  context_instance=RequestContext(request))
示例#9
0
文件: views.py 项目: carsor007/kasa
def sendurl(request):
    if request.method == "POST":
        post_values = request.POST.copy()
        tags = post_values['tags']
        post_values['tags'] = tags[tags.find("[") + 1:tags.find("]")]
        form = PinForm(post_values)
        if form.is_valid():
            model = form.save(commit=False)

            image_url = model.image

            filename = image_url.split('/')[-1]

            #str = "%f" % time.time()
            #str = str.replace('.', '')

            #filename = "%s%s" % (str, os.path.splitext(filename)[1])
            filename = create_filename(filename)
            #filename = "%s%s" % (str, filename)

            image_on = "%s/pin/images/o/%s" % (MEDIA_ROOT, filename)

            urllib.urlretrieve(image_url, image_on)

            model.image = "pin/images/o/%s" % (filename)
            model.timestamp = time.time()
            model.user = request.user
            model.save()

            form.save_m2m()

            return HttpResponseRedirect('/pin/')
    else:
        form = PinForm()

    return render_to_response('pin/sendurl.html', {'form': form},
                              context_instance=RequestContext(request))
示例#10
0
def sendurl(request):
    if request.method == "POST":
        post_values = request.POST.copy()
        tags = post_values['tags']
        post_values['tags'] = tags[tags.find("[") + 1:tags.find("]")]
        form = PinForm(post_values)
        if form.is_valid():
            model = form.save(commit=False)

            image_url = model.image
            filename = image_url.split('/')[-1]
            filename = create_filename(filename)
            image_on = "%s/pin/images/o/%s" % (MEDIA_ROOT, filename)

            urllib.urlretrieve(image_url, image_on)

            model.image = "pin/images/o/%s" % (filename)
            model.timestamp = time()
            model.user = request.user
            model.save()

            form.save_m2m()

            if model.status == 1:
                msg = 'مطلب شما با موفقیت ارسال شد. <a href="%s">مشاهده</a>' %\
                    reverse('pin-item', args=[model.id])
                messages.add_message(request, messages.SUCCESS, msg)
            elif model.status == 0:
                msg = 'مطلب شما با موفقیت ارسال شد و بعد از تایید در سایت نمایش داده می شود '
                messages.add_message(request, messages.SUCCESS, msg)

            return HttpResponseRedirect('/pin/')
    else:
        form = PinForm()

    return render(request, 'pin/sendurl.html', {'form': form})
示例#11
0
def send(request):
    if request.method == "POST":
        post_values = request.POST.copy()
        tags = post_values['tags']
        post_values['tags']=tags[tags.find("[")+1:tags.find("]")]
        form = PinForm(post_values)
        if form.is_valid():
            model = form.save(commit=False)
            
            filename= model.image
            
            image_o = "%s/pin/temp/o/%s" % ( MEDIA_ROOT,filename)
            
            image_on = "%s/pin/images/o/%s" % ( MEDIA_ROOT, filename)
            
            copyfile(image_o, image_on)
            
            model.image = "pin/images/o/%s" % (filename)
            model.timestamp = time.time()
            model.user = request.user
            model.save()
            
            form.save_m2m()
            
            return HttpResponseRedirect('/pin/')
    else:
        form = PinForm()
    
    user_category = Category.objects.filter(user_id=request.user.id).all()
        
    if request.is_ajax():
        return render_to_response('pin/_send.html',
                                  {'form': form,
                                   'user_category': user_category}, 
                                  context_instance=RequestContext(request))
    else:
        return render_to_response('pin/send.html',
                                  {'form': form,
                                   'user_category': user_category}, 
                                  context_instance=RequestContext(request))
示例#12
0
def sendurl(request):
    if request.method == "POST":
        post_values = request.POST.copy()
        tags = post_values['tags']
        post_values['tags']=tags[tags.find("[")+1:tags.find("]")]
        form = PinForm(post_values)
        print post_values, form
        if form.is_valid():
            model = form.save(commit=False)
            
            image_url= model.image
            print image_url
            #image_url="http://image.sinajs.cn/newchart/daily/n/%s.gif" % image_url
            
            filename = image_url.split('/')[-1]
            
            #str = "%f" % time.time()
            #str = str.replace('.', '')
        
            #filename = "%s%s" % (str, os.path.splitext(filename)[1])
            filename = create_filename(filename)
            #filename = "%s%s" % (str, filename)
                        
            image_on = "%s/pin/images/o/%s" % ( MEDIA_ROOT, filename)
                                 
            urllib.urlretrieve(image_url, image_on)
            
            model.image = "pin/images/o/%s" % (filename)
            model.timestamp = time.time()
            model.user = request.user
            model.save()
            
            form.save_m2m()
            
            return HttpResponseRedirect('/pin/')
    else:
        form = PinForm()
            
    return render_to_response('pin/sendurl.html',{'form':form}, 
                              context_instance=RequestContext(request)) 
示例#13
0
def send(request):
    if request.method == "POST":
        post_values = request.POST.copy()
        tags = post_values['tags']
        post_values['tags'] = tags[tags.find("[") + 1:tags.find("]")]
        form = PinForm(post_values)
        if form.is_valid():
            model = form.save(commit=False)
            filename = model.image
            image_o = "%s/pin/temp/o/%s" % (MEDIA_ROOT, filename)
            image_on = "%s/pin/images/o/%s" % (MEDIA_ROOT, filename)

            copyfile(image_o, image_on)

            model.image = "pin/images/o/%s" % (filename)
            model.timestamp = time()
            model.user = request.user
            model.save()

            form.save_m2m()

            if model.status == 1:
                msg = 'مطلب شما با موفقیت ارسال شد. <a href="%s">مشاهده</a>' %\
                    reverse('pin-item', args=[model.id])
                messages.add_message(request, messages.SUCCESS, msg)
            elif model.status == 0:
                msg = 'مطلب شما با موفقیت ارسال شد و بعد از تایید در سایت نمایش داده می شود '
                messages.add_message(request, messages.SUCCESS, msg)

            return HttpResponseRedirect('/pin/')
    else:
        form = PinForm()

    category = Category.objects.all()

    if request.is_ajax():
        return render(request, 'pin/_send.html', {'form': form, 'category': category})
    else:
        return render(request, 'pin/send.html', {'form': form, 'category': category})
示例#14
0
def sendurl(request):
    if request.method == "POST":
        post_values = request.POST.copy()
        tags = post_values["tags"]
        post_values["tags"] = tags[tags.find("[") + 1 : tags.find("]")]
        form = PinForm(post_values)
        if form.is_valid():
            model = form.save(commit=False)

            image_url = model.image

            filename = image_url.split("/")[-1]

            # str = "%f" % time.time()
            # str = str.replace('.', '')

            # filename = "%s%s" % (str, os.path.splitext(filename)[1])
            filename = create_filename(filename)
            # filename = "%s%s" % (str, filename)

            image_on = "%s/pin/images/o/%s" % (MEDIA_ROOT, filename)

            urllib.urlretrieve(image_url, image_on)

            model.image = "pin/images/o/%s" % (filename)
            model.timestamp = time.time()
            model.user = request.user
            model.save()

            form.save_m2m()

            return HttpResponseRedirect("/pin/")
    else:
        form = PinForm()

    return render_to_response("pin/sendurl.html", {"form": form}, context_instance=RequestContext(request))
示例#15
0
def sendurl(request):
    if request.method == "POST":
        post_values = request.POST.copy()
        tags = post_values['tags']
        post_values['tags'] = tags[tags.find("[")+1:tags.find("]")]
        form = PinForm(post_values)
        if form.is_valid():
            model = form.save(commit=False)

            image_url = model.image
            filename = image_url.split('/')[-1]
            filename = create_filename(filename)
            image_on = "%s/pin/images/o/%s" % (MEDIA_ROOT, filename)

            urllib.urlretrieve(image_url, image_on)

            model.image = "pin/images/o/%s" % (filename)
            model.timestamp = time()
            model.user = request.user
            model.save()

            form.save_m2m()

            if model.status == 1:
                msg = 'مطلب شما با موفقیت ارسال شد. <a href="%s">مشاهده</a>' %\
                    reverse('pin-item', args=[model.id])
                messages.add_message(request, messages.SUCCESS, msg)
            elif model.status == 0:
                msg = 'مطلب شما با موفقیت ارسال شد و بعد از تایید در سایت نمایش داده می شود '
                messages.add_message(request, messages.SUCCESS, msg)

            return HttpResponseRedirect('/pin/')
    else:
        form = PinForm()

    return render(request, 'pin/sendurl.html', {'form': form})