Beispiel #1
0
def new(request, slug):
    """Create new note for reading."""
    reading = get_object_or_404(Reading, slug=slug)
    if request.method == 'POST':
        form = NoteForm(request.POST)
        if form.is_valid():
            note = form.save(commit=False)
            note.reading = reading
            note.user = request.user
            note.save()
            # facebook open graph add note
            facebook_graph_add_note(request.user, reading)
            # add rep
            add_rep(request, n=note)
            request.user.vote_set.create(note=note, value=1)
            notify(note=note)
            if request.user.is_staff:
                # auto create votes for reading and reading.notes
                auto_vote(request, reading)
            d = {
                    'comment_form': CommentForm(),
                    'note': note,
                    'note_form': NoteForm(),
                    'reading': reading,
                    'static': settings.STATIC_URL,
            }
            bullet_notes = loader.get_template('notes/bullet_notes.html')
            note_form = loader.get_template('notes/note_form.html')
            note_temp = loader.get_template('notes/note.html')
            context = RequestContext(request, add_csrf(request, d))
            data = {
                        'bullet_notes': bullet_notes.render(context),
                        'note': note_temp.render(context),
                        'note_form': note_form.render(context),
                        'note_pk': note.pk,
            }
            return HttpResponse(json.dumps(data), mimetype='application/json')
    return HttpResponseRedirect(reverse('readings.views.detail', 
        args=[reading.slug]))
Beispiel #2
0
def reading(request, slug):
    """Detail reading."""
    reading = get_object_or_404(Reading, slug=slug)
    if request.method == 'POST':
        if request.user.pk == 2:
            form = DavidNoteForm(request.POST)
        else:
            form = AdminNoteForm(request.POST)
        if form.is_valid():
            # save note
            note = form.save(commit=False)
            note.reading = reading
            note.save()
            # facebook open graph add note
            if note.user.pk == request.user.pk:
                facebook_graph_add_note(request.user, reading)
            # add rep
            add_rep(request, n=note)
            # create notification
            notify(note=note)
            note.user.vote_set.create(note=note, value=1)
            # auto create votes for reading and reading.notes
            auto_vote(request, reading)
            messages.success(request, 'Note created')
            return HttpResponseRedirect(reverse('admins.views.reading', 
                args=[reading.slug]))
    else:
        if request.user.pk == 2:
            form = DavidNoteForm()
        else:
            form = AdminNoteForm()
    d = {
        'form': form,
        'reading': reading,
        'title': 'Reading: Add Note',
    }
    return render_to_response('admins/detail_reading.html', 
        add_csrf(request, d), context_instance=RequestContext(request))
Beispiel #3
0
def new_bookmarklet(request):
    """Create new reading from bookmarklet."""
    if request.method == "POST":
        content = request.POST.get("content", "")
        image = request.POST.get("image", "")
        link = request.POST.get("link", "")
        titl = request.POST.get("title", "")[:80]
        if request.user.is_staff:
            user_pk = request.POST.get("user")
            if user_pk:
                try:
                    user = User.objects.get(pk=int(user_pk))
                except ObjectDoesNotExist:
                    user = request.user
            else:
                user = request.user
        else:
            user = request.user
        # if there is a link and title, create reading
        if link and titl:
            try:
                # if reading with link exists, add notes to that reading
                reading = Reading.objects.get(link=link)
                reading_exists = True
            except ObjectDoesNotExist:
                reading_exists = False
                titles = Reading.objects.filter(title=titl)
                # if there is a reading with the title
                if titles:
                    titl = "%s-%s" % (titl, str(titles.count()))
                reading = Reading(image=image, link=link, title=titl, user=user)
                reading.save()
                # create vote for reading
                reading.vote_set.create(user=user, value=1)
            # add tag
            name = request.POST.get("tag_name")
            # if user added tag
            if name:
                try:
                    # check to see if user already tied a tag to this reading
                    existing_tie = reading.tie_set.get(user=user)
                # if user has not added a tag to this reading
                except ObjectDoesNotExist:
                    name = name.lower()
                    pattern = only_letters()
                    # If name contains only letters
                    if re.search(pattern, name):
                        # If name does not contain any banned words
                        blacklist = banned_words()
                        if not re.search(blacklist, name):
                            try:
                                # If tag exists, get tag
                                tag = Tag.objects.get(name=name)
                            except ObjectDoesNotExist:
                                # If tag does not exist, create tag
                                tag = Tag(name=name, user=user)
                                tag.slug = slugify(tag.name)
                                tag.save()
                            tie = user.tie_set.create(reading=reading, tag=tag)
                            # add rep
                            add_rep(request, t=tie)
            # if user did not add a tag, auto tag
            else:
                auto_tag(request, reading)
            # add rep
            add_rep(request, rd=reading)
            # if there is content, create note
            if content.strip():
                note = Note(content=content, reading=reading, user=user)
                note.save()
                # create first vote for note
                user.vote_set.create(note=note, value=1)
                if reading_exists:
                    facebook_graph_add_note(note.user, note.reading)
            if not reading_exists:
                facebook_graph_add_reading(reading.user, reading)
            if request.user.is_staff:
                # auto create votes for reading and reading.notes
                auto_vote(request, reading)
            data = {"success": 1}
            return HttpResponse(json.dumps(data), mimetype="application/json")
    content = request.GET.get("note", "").lstrip(" ")
    link = request.GET.get("link", "")
    # split title
    html_title = split_title(request.GET.get("title", ""))
    if request.user.is_staff:
        if request.user.pk == 2:
            users = admin_david_list()
        else:
            users = admin_user_list()
    else:
        users = []
    d = {"content": content, "link": link, "titl": html_title, "title": "Add Reading", "users": users}
    return render_to_response(
        "readings/new_bookmarklet.html", add_csrf(request, d), context_instance=RequestContext(request)
    )
Beispiel #4
0
def new(request):
    """Add new reading."""
    NoteFormset = formset_factory(NoteForm, extra=3)
    if request.method == "POST":
        link = request.POST.get("link")
        form = ReadingForm(request.POST)
        formset = NoteFormset(request.POST)
        # if reading with link already exists, add notes to it
        try:
            reading = Reading.objects.get(link=link)
            if formset.is_valid():
                # add tag
                name = request.POST.get("tag_name")
                # if user added a tag
                if name:
                    name = name.lower()
                    pattern = only_letters()
                    # If name contains only letters
                    if re.search(pattern, name):
                        # If name does not contain any banned words
                        blacklist = banned_words()
                        if not re.search(blacklist, name):
                            try:
                                # If tag exists, get tag
                                tag = Tag.objects.get(name=name)
                            except ObjectDoesNotExist:
                                # If tag does not exist, create tag
                                tag = Tag(name=name, user=request.user)
                                tag.slug = slugify(tag.name)
                                tag.save()
                            try:
                                # Check to see if tie exists
                                tie = reading.tie_set.get(tag=tag)
                            except ObjectDoesNotExist:
                                # If tie does not exist, create it
                                tie = request.user.tie_set.create(reading=reading, tag=tag)
                                # add rep
                                add_rep(request, t=tie)
                                # create notification
                                notify(tie=tie)
                    # if user did not add a tag, auto tag
                    else:
                        auto_tag(request, reading)
                # create notes and add it to the existing reading
                for index, note_form in enumerate(formset):
                    note = note_form.save(commit=False)
                    note.reading = reading
                    note.user = request.user
                    if note.content.strip():
                        note.save()
                        # create first vote for note
                        note.user.vote_set.create(note=note, value=1)
                        # if this is the first note
                        if index == 0:
                            # facebook open graph add note
                            facebook_graph_add_note(note.user, note.reading)
                if request.user.is_staff:
                    # auto create votes for reading and reading.notes
                    auto_vote(request, reading)
                messages.success(request, "This reading exists, your content has been added to it")
                return HttpResponseRedirect(reverse("readings.views.detail", args=[reading.slug]))
        # if reading with link does not exist, create the reading
        except ObjectDoesNotExist:
            if form.is_valid() and formset.is_valid():
                reading = form.save(commit=False)
                reading.user = request.user
                reading.save()
                # create vote for reading
                reading.vote_set.create(user=reading.user, value=1)
                # add tag
                name = request.POST.get("tag_name")
                # if user added tag
                if name:
                    name = name.lower()
                    pattern = only_letters()
                    # If name contains only letters
                    if re.search(pattern, name):
                        # If name does not contain any banned words
                        blacklist = banned_words()
                        if not re.search(blacklist, name):
                            try:
                                # If tag exists, get tag
                                tag = Tag.objects.get(name=name)
                            except ObjectDoesNotExist:
                                # If tag does not exist, create tag
                                tag = Tag(name=name, user=request.user)
                                tag.slug = slugify(tag.name)
                                tag.save()
                            tie = request.user.tie_set.create(reading=reading, tag=tag)
                            # add rep
                            add_rep(request, t=tie)
                # if user did not add a tag, auto tag
                else:
                    auto_tag(request, reading)
                # facebook open graph add reading
                facebook_graph_add_reading(request.user, reading)
                # add rep
                add_rep(request, rd=reading)
                # create notes for this reading
                for note_form in formset:
                    note = note_form.save(commit=False)
                    note.reading = reading
                    note.user = request.user
                    if note.content.strip():
                        note.save()
                        # create first vote for note
                        request.user.vote_set.create(note=note, value=1)
                if request.user.is_staff:
                    # auto create votes for reading and reading.notes
                    auto_vote(request, reading)
                messages.success(request, "Reading created")
                return HttpResponseRedirect(reverse("readings.views.detail", args=[reading.slug]))
    else:
        form = ReadingForm()
        formset = NoteFormset()
    di = {"static": settings.STATIC_URL}
    t = loader.get_template("javascript/bookmarklet.js")
    context = RequestContext(request, di)
    d = {
        "bookmarklet": re.sub(r"\s", "%20", str(t.render(context))),
        "form": form,
        "formset": formset,
        "title": "New Reading",
    }
    return render_to_response("readings/new.html", add_csrf(request, d), context_instance=RequestContext(request))