Пример #1
0
def new(request, slug):
    """Create new tie for reading."""
    reading = get_object_or_404(Reading, slug=slug)
    if request.method == "POST":
        # If reading has 5 tags, do not add or create
        if len(reading.tie_set.all()) >= 5:
            messages.warning(request, "Each read can only have 5 tags")
        else:
            try:
                # If user already added a tag
                user = reading.tie_set.get(user=request.user)
                messages.error(request, "You can only add 1 tag per read")
            except ObjectDoesNotExist:
                name = request.POST.get("tag_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)
                            d = {"reading": reading}
                            tags = loader.get_template("tags/tags.html")
                            context = RequestContext(request, d)
                            data = {"ties": tags.render(context)}
                            return HttpResponse(json.dumps(data), mimetype="application/json")
    return HttpResponseRedirect(reverse("readings.views.detail", args=[reading.slug]))
Пример #2
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))
Пример #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)
    )
Пример #4
0
def new_reading(request):
    """Create a new reading."""
    NoteFormset = formset_factory(NoteForm, extra=3, formset=RequiredFormSet)
    if request.method == 'POST':
        if request.user.pk == 2:
            form = DavidReadingForm(request.POST)
        else:
            form = AdminReadingForm(request.POST)
        formset = NoteFormset(request.POST)
        if form.is_valid() and formset.is_valid():
            # save reading
            reading = form.save()
            # 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()
                        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
            if reading.user.pk == request.user.pk:
                facebook_graph_add_reading(request.user, reading)
            # add rep
            add_rep(request, rd=reading)
            first = True
            # save each note
            for note_form in formset:
                note = note_form.save(commit=False)
                if note.content.strip():
                    note.reading = reading
                    # if this is the first note
                    # set note.user to reading.user
                    if first:
                        note.user = reading.user
                        first = False
                    else:
                        note.user = random_user()
                    # save note
                    note.save()
                    # create first vote for note
                    note.user.vote_set.create(note=note, value=1)
                    # add rep
                    add_rep(request, n=note)
            # auto create votes for reading and reading.notes
            auto_vote(request, reading)
            messages.success(request, 'Reading created')
            return HttpResponseRedirect(reverse('admins.views.reading', 
                args=[reading.slug]))
    else:
        if request.user.pk == 2:
            form = DavidReadingForm()
        else:
            form = AdminReadingForm()
        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))