Exemple #1
0
def add_collaborator_(request, pk):
    data = {'valid': False}
    tl = Timeline.objects.get(pk=pk)
    if tl.created_by != request.user:
        return render_json_response(data)
    if request.method == "POST":
        username = request.POST.get('username', '')
        try:
            user = User.objects.get(username=username)
            if user.has_perm('collaborator', tl):
                data['info'] = u'用户 "%s" 已经添加过' % username
                return render_json_response(data)
        except:
            data['info'] = u'用户 "%s" 不存在' % username
            return render_json_response(data)
        assign('collaborator', user, tl)
        return render_json_response({
            'valid':
            True,
            'obj': {
                'pk': user.pk,
                'username': user.username
            },
            'html':
            render_string(COLLABORATOR_ROW_TMPL, {'o': user})
        })
    return render_json_response(data)
Exemple #2
0
def remove_collaborator_(request, pk):
    tl = Timeline.objects.get(pk=pk)
    if tl.created_by != request.user:
        return render_json_response({'valid': False})
    upk = request.POST.get('upk', '')
    user = User.objects.get(pk=upk)
    remove_perm('collaborator', user, tl)
    return render_json_response({'valid': True})
Exemple #3
0
def remove_collaborator_(request, pk):
    tl = Timeline.objects.get(pk=pk)
    if tl.created_by != request.user:
        return render_json_response({'valid': False})
    upk = request.POST.get('upk', '')
    user = User.objects.get(pk=upk)
    remove_perm('collaborator', user, tl)
    return render_json_response({'valid': True})
Exemple #4
0
def delete_(request):
    pk = request.GET.get('pk', '')
    event = get_object_or_404(TlEvent, pk=pk)
    if not event.timeline.can_edit(request.user):
        return render_json_response({'valid': False})
    event.delete()
    timeline = event.timeline
    timeline.update_num_events()
    timeline.update_updated_on()
    return render_json_response({'valid': True})
Exemple #5
0
def addevent_(request, pk):
    timeline = get_object_or_404(Timeline, pk=pk)
    if not timeline.can_edit(request.user):
        return render_json_response({'valid': False})
    form, validate = validate_form(request, form_class=TlEventForm)
    if validate['valid']:
        event = form.save(timeline=timeline)
        timeline.update_num_events()
        timeline.update_updated_on()
        validate['data'] = event_to_sdict(event)
    return render_json_response(validate)
Exemple #6
0
def addevent_(request, pk):
    timeline = get_object_or_404(Timeline, pk=pk)
    if not timeline.can_edit(request.user):
        return render_json_response({'valid': False})
    form, validate = validate_form(request, form_class=TlEventForm)
    if validate['valid']:
        event = form.save(timeline=timeline)
        timeline.update_num_events()
        timeline.update_updated_on()
        validate['data'] = event_to_sdict(event)
    return render_json_response(validate)
Exemple #7
0
def edit_(request):
    pk = request.GET.get('pk', '')
    event = TlEvent.objects.get(pk=pk)
    if not event.timeline.can_edit(request.user):
        return render_json_response({'valid': False})
    form, validate = validate_form(request, form_class=TlEventForm, instance=event)
    if validate['valid']:
        event = form.save()
        event.timeline.update_updated_on()
        validate['data'] = event_to_sdict(event)
    return render_json_response(validate)
Exemple #8
0
def postcomment_(request, pk):
    timeline = get_object_or_404(Timeline, pk=pk)
    form, validate = validate_form(request, form_class=CommentForm)
    if not request.user.is_authenticated():
        return render_json_response({'valid': False})
    if validate['valid']:
        c = form.save(commit=False)
        c.timeline = timeline
        c.created_by = request.user
        c.save()
        timeline.update_num_replies()
        validate['html'] = render_to_string('timeline/inc_comment.html', { 'c': c })
    return render_json_response(validate)
Exemple #9
0
def postcomment_(request, pk):
    bookmark = get_object_or_404(Bookmark, pk=pk)
    form, validate = validate_form(request, form_class=BKCommentForm)
    if not request.user.is_authenticated():
        return render_json_response({'valid': False})
    if validate['valid']:
        c = form.save(commit=False)
        c.bookmark = bookmark
        c.created_by = request.user
        c.save()
        bookmark.update_num_replies()
        validate['html'] = render_to_string('bookmark/inc_comment.html', { 'c': c })
    return render_json_response(validate)
Exemple #10
0
def postcomment_(request, pk):
    timeline = get_object_or_404(Timeline, pk=pk)
    form, validate = validate_form(request, form_class=CommentForm)
    if not request.user.is_authenticated():
        return render_json_response({'valid': False})
    if validate['valid']:
        c = form.save(commit=False)
        c.timeline = timeline
        c.created_by = request.user
        c.save()
        timeline.update_num_replies()
        validate['html'] = render_to_string('timeline/inc_comment.html',
                                            {'c': c})
    return render_json_response(validate)
Exemple #11
0
def events_sjson_(request, pk):
    tl = Timeline.objects.get(pk=pk)
    events = tl.tlevent_set.order_by('startdate')
    date = []
    for e in events:
        date.append(event_to_sdict(e))
    return render_json_response(date)
Exemple #12
0
def events_sjson_(request, pk):
    tl = Timeline.objects.get(pk=pk)
    events = tl.tlevent_set.order_by('startdate')
    date = []
    for e in events:
        date.append(event_to_sdict(e))
    return render_json_response(date)
Exemple #13
0
def attach_upload_(request, pk):
    if not request.user.is_authenticated():
        return render_json_response({'valid': False})
    timeline = get_object_or_404(Timeline, pk=pk)
    data = _do_ajax_upload(request)
    ret = {}
    if data['valid']:
        attach = Attachment.objects.get(pk=data['attachment']['id'])
        timeline.attachments.add(attach)
        ret['name'] = attach.org_filename
        ret['size'] = attach.file.size
        ret['url'] = attach.file.url
        ret['id'] = attach.pk
        ret['delete_url'] = "%s?id=%s" % (reverse('timeline_attach_delete_', args=[timeline.pk]), attach.pk)
    else:
        ret['error'] = u'上传失败'
    return render_json_response([ret, ]) 
Exemple #14
0
def attachs_(request, pk):
    timeline = get_object_or_404(Timeline, pk=pk)
    attachs = timeline.attachments.order_by('-date_uploaded')
    data = []
    for a in attachs:
        data.append({'id': a.id, 'fn': a.org_filename, 
            'url': a.file.url, 'descn': a.description})
    return render_json_response(data) 
Exemple #15
0
def attach_upload_(request, pk):
    if not request.user.is_authenticated():
        return render_json_response({'valid': False})
    timeline = get_object_or_404(Timeline, pk=pk)
    data = _do_ajax_upload(request)
    ret = {}
    if data['valid']:
        attach = Attachment.objects.get(pk=data['attachment']['id'])
        timeline.attachments.add(attach)
        ret['name'] = attach.org_filename
        ret['size'] = attach.file.size
        ret['url'] = attach.file.url
        ret['id'] = attach.pk
        ret['delete_url'] = "%s?id=%s" % (reverse(
            'timeline_attach_delete_', args=[timeline.pk]), attach.pk)
    else:
        ret['error'] = u'上传失败'
    return render_json_response([
        ret,
    ])
Exemple #16
0
def attachs_(request, pk):
    timeline = get_object_or_404(Timeline, pk=pk)
    attachs = timeline.attachments.order_by('-date_uploaded')
    data = []
    for a in attachs:
        data.append({
            'id': a.id,
            'fn': a.org_filename,
            'url': a.file.url,
            'descn': a.description
        })
    return render_json_response(data)
Exemple #17
0
def add_collaborator_(request, pk):
    data = {'valid': False}
    tl = Timeline.objects.get(pk=pk)
    if tl.created_by != request.user:
        return render_json_response(data)
    if request.method == "POST":
        username = request.POST.get('username', '')
        try:
            user = User.objects.get(username=username)
            if user.has_perm('collaborator', tl):
                data['info'] = u'用户 "%s" 已经添加过' % username
                return render_json_response(data)
        except:
            data['info'] = u'用户 "%s" 不存在' % username
            return render_json_response(data)
        assign('collaborator', user, tl)
        return render_json_response({'valid': True, 
            'obj': {'pk': user.pk, 'username': user.username},
            'html': render_string(COLLABORATOR_ROW_TMPL, {'o': user})
            })
    return render_json_response(data)
Exemple #18
0
def events_json_(request, pk):
    tl = Timeline.objects.get(pk=pk)
    t = {}
    timeline = {"type": "default"}
    t['timeline'] = timeline
    date = []
    timeline['date'] = date
    # cover
    events = tl.tlevent_set.filter(cover=True).order_by('startdate')
    if events.count():
        timeline.update(event_to_dict(events[0]))
    # date
    events = tl.tlevent_set.filter(cover=False).order_by('startdate')
    for e in events:
        date.append(event_to_dict(e))
    if not events.count():  #if no events will get a js error.
        date.append({"startDate": "2012", 'headline': u'现在还没有任何事件,请先添加事件'})
    return render_json_response(t)
Exemple #19
0
def events_json_(request, pk):
    tl = Timeline.objects.get(pk=pk)
    t = {}
    timeline = { "type":"default" }
    t['timeline'] = timeline
    date = []
    timeline['date'] = date
    # cover
    events = tl.tlevent_set.filter(cover=True).order_by('startdate')
    if events.count():
        timeline.update(event_to_dict(events[0]))
    # date
    events = tl.tlevent_set.filter(cover=False).order_by('startdate')
    for e in events:
        date.append(event_to_dict(e))
    if not events.count():#if no events will get a js error.
        date.append({"startDate": "2012", 'headline': u'现在还没有任何事件,请先添加事件'})
    return render_json_response(t)
Exemple #20
0
def attach_change_descn_(request, pk):
    attach_id = request.POST.get('id', 0) or request.GET.get('id', 0)
    attach = Attachment.objects.get(pk=attach_id)
    if attach.user != request.user:
        return render_json_response({'valid': False})
    return ajax_change_descn(request)
Exemple #21
0
def attach_change_descn_(request, pk):
    attach_id = request.POST.get('id', 0) or request.GET.get('id', 0)
    attach = Attachment.objects.get(pk=attach_id)
    if attach.user != request.user:
        return render_json_response({'valid': False})
    return ajax_change_descn(request)
Exemple #22
0
def json_(request):
    pk = request.GET.get('pk', '')
    event = TlEvent.objects.get(pk=pk)
    data = {'valid': True}
    data['data'] = event_to_sdict(event)
    return render_json_response(data)
Exemple #23
0
def validate(request, *args, **kwargs):
    form, data = validate_form(request, *args, **kwargs)
    return render_json_response(data)