Exemple #1
0
 def add(self):
     listlink = self
     alist = listlink.alist
     link = listlink.links
     md_link = "1. [%s](%s)" % (
         link.title, link.url
     )
     alist.md_content = "\n".join([alist.md_content, md_link])
     alist.html_content = listmarkdown(alist.md_content, alist)
     alist.save()
Exemple #2
0
def new_list(request):
    if request.method == 'POST':
        form = NewListForm(request.POST)
        if form.is_valid():
            is_private = False

            if 'private' in form.data:
                is_private = form.data['private']

            if form.data['url']:
                opener = urllib2.build_opener()
                opener.addheaders = [('Accept-Charset', 'utf-8')]
                url_response = opener.open(form.data['url'])
                mdcontent = url_response.read().decode('utf-8')
            else:
                mdcontent = form.data['mdcontent']

            from resrc.language.models import Language
            try:
                lang = Language.objects.get(language=form.data['language'])
            except Language.DoesNotExist:
                lang = Language.objects.create(language=form.data['language'])

            from resrc.utils.templatetags.emarkdown import listmarkdown
            alist = List.objects.create(
                title=form.data['title'],
                description=form.data['description'],
                url=form.data['url'],
                md_content=mdcontent,
                html_content='',
                owner=request.user,
                is_public=not is_private,
                language=lang,
            )
            alist.html_content = listmarkdown(mdcontent, alist)
            alist.save()

            return redirect(alist.get_absolute_url())
    else:
        form = NewListForm()

    links = list(Link.objects.all())

    return render_template('lists/new_list.html', {
        'form': form,
        'links': links
    })
Exemple #3
0
def auto_pull(request, list_pk):
    alist = get_object_or_404(List, pk=list_pk)

    opener = urllib2.build_opener()
    opener.addheaders = [('Accept-Charset', 'utf-8')]
    url_response = opener.open(alist.url)
    mdcontent = url_response.read().decode('utf-8')


    alist.md_content = mdcontent
    alist.html_content = ''

    from resrc.utils.templatetags.emarkdown import listmarkdown
    alist.html_content = listmarkdown(mdcontent, alist)
    alist.save()

    return redirect(alist.get_absolute_url())
Exemple #4
0
def new_list(request):
    if request.method == "POST":
        form = NewListForm(request.POST)
        if form.is_valid():
            is_private = False

            if "private" in form.data:
                is_private = form.data["private"]

            if form.data["url"]:
                opener = urllib2.build_opener()
                opener.addheaders = [("Accept-Charset", "utf-8")]
                url_response = opener.open(form.data["url"])
                mdcontent = url_response.read().decode("utf-8")
            else:
                mdcontent = form.data["mdcontent"]

            from resrc.language.models import Language

            try:
                lang = Language.objects.get(language=form.data["language"])
            except Language.DoesNotExist:
                lang = Language.objects.create(language=form.data["language"])

            from resrc.utils.templatetags.emarkdown import listmarkdown

            alist = List.objects.create(
                title=form.data["title"],
                description=form.data["description"],
                url=form.data["url"],
                md_content=mdcontent,
                html_content="",
                owner=request.user,
                is_public=not is_private,
                language=lang,
            )
            alist.html_content = listmarkdown(mdcontent.strip(u"\ufeff"), alist)
            alist.save()

            return redirect(alist.get_absolute_url())
    else:
        form = NewListForm()

    links = list(Link.objects.all())

    return render_template("lists/new_list.html", {"form": form, "links": links})
Exemple #5
0
    def remove(self):
        listlink = self
        alist = listlink.alist
        link = listlink.links
        md_link = "[%s](%s)" % (
            link.title, link.url
        )

        alist.md_content = alist.md_content.replace(md_link, '')

        import re
        SEARCH = re.compile("^(\d+\.|-)(\s)$", re.MULTILINE)
        REPLACE = r' '
        alist.md_content = SEARCH.sub(REPLACE, alist.md_content)
        from resrc.utils.templatetags.emarkdown import listmarkdown
        alist.html_content = listmarkdown(alist.md_content, alist)
        alist.save()
Exemple #6
0
def new_link(request, title=None, url=None):
    if title is not None and url is not None:
        url = url.replace('http:/', 'http://')
        url = url.replace('https:/', 'https://')
        form = NewLinkForm(initial={
            'title': title,
            'url': url,
        })

        tags = cache.get('tags_csv')
        if tags is None:
            from taggit.models import Tag
            tags = '","'.join(Tag.objects.all().values_list('name', flat=True))
            tags = '"%s"' % tags
            cache.set('tags_csv', tags, 60*15)

        return render_template('links/new_link_button.html', {
            'form': form,
            'tags': tags
        })

    if request.method == 'POST':
        form = NewLinkForm(request.POST)
        if form.is_valid():
            data = form.data

            link = Link()
            link.title = data['title']
            link.url = data['url']
            from resrc.language.models import Language
            try:
                lang = Language.objects.get(language=data['language'])
            except Language.DoesNotExist:
                lang = Language.objects.create(language=data['language'])
            link.language = lang

            link.level = data['level']
            link.author = request.user

            if url[-1] == '/' and Link.objects.filter(url=data['url'][:-1]).exists():
                return redirect(Link.objects.get(url=data['url'][:-1]).get_absolute_url())

            elif Link.objects.filter(url=data['url']+'/').exists():
                return redirect(Link.objects.get(url=data['url']+'/').get_absolute_url())

            elif Link.objects.filter(url=data['url']).exists():
                return redirect(Link.objects.get(url=data['url']).get_absolute_url())

            link.save()
            list_tags = data['tags'].split(',')
            for tag in list_tags:
                link.tags.add(tag)
                cache.delete('tags_all')
                cache.delete('tags_csv')
            link.save()
            cache.set('link_%s' % link.pk, link, 60*5)

            if not 'ajax' in data:
                return redirect(link.get_absolute_url())

            alist = get_object_or_404(List, pk=data['id'])
            # if alist.owner != request.user:
            #    raise Http404
            from resrc.list.models import ListLinks
            if not ListLinks.objects.filter(alist=alist, links=link).exists():
                ListLinks.objects.create(
                    alist=alist,
                    links=link
                )
            from resrc.utils.templatetags.emarkdown import listmarkdown
            alist.html_content = listmarkdown(alist.md_content.strip(u'\ufeff'), alist)
            alist.save()

            data = json.dumps({'result': 'added'})
            return HttpResponse(data, mimetype="application/javascript")
        else:
            if not 'ajax' in form.data:
                form = NewLinkForm()

                tags = cache.get('tags_csv')
                if tags is None:
                    from taggit.models import Tag
                    tags = '","'.join(Tag.objects.all().values_list('name', flat=True))
                    tags = '"%s"' % tags
                    cache.set('tags_csv', tags, 60*15)

                return render_template('links/new_link.html', {
                    'form': form,
                    'tags': tags
                })
            else:
                data = json.dumps({'result': 'fail'})
                return HttpResponse(data, mimetype="application/javascript")

    else:
        form = NewLinkForm()

    tags = cache.get('tags_csv')
    if tags is None:
        from taggit.models import Tag
        tags = '","'.join(Tag.objects.all().values_list('name', flat=True))
        tags = '"%s"' % tags
        cache.set('tags_csv', tags, 60*15)

    return render_template('links/new_link.html', {
        'form': form,
        'tags': tags
    })
Exemple #7
0
def edit(request, list_pk):
    if not request.user.is_authenticated():
        raise Http404
    alist = get_object_or_404(List, pk=list_pk)

    if request.user.pk != alist.owner.pk:
        raise Http404

    if alist.is_public:
        private_checkbox = ''
    else:
        private_checkbox = 'checked="checked"'
    if alist.url is not None and len(alist.url) > 0:
        from_url = True
    else:
        from_url = False

    if request.method == 'POST':
        form = EditListForm(
            private_checkbox, alist, from_url, request.POST)
        if form.is_valid():
            is_private = False

            if 'private' in form.data:
                is_private = form.data['private']

            if form.data['url']:
                opener = urllib2.build_opener()
                opener.addheaders = [('Accept-Charset', 'utf-8')]
                url_response = opener.open(form.data['url'])
                mdcontent = url_response.read().decode('utf-8')
            else:
                mdcontent = form.data['mdcontent']

            from resrc.utils.templatetags.emarkdown import listmarkdown

            from resrc.language.models import Language
            try:
                lang = Language.objects.get(language=form.data['language'])
            except Language.DoesNotExist:
                lang = Language.objects.create(language=form.data['language'])

            alist.title = form.data['title']
            alist.description = form.data['description']
            alist.url = form.data['url']
            alist.md_content = mdcontent
            alist.html_content = ''
            alist.is_public = not is_private
            alist.language = lang
            alist.save()
            # once saved, we parse the markdown to add links found to list
            alist.html_content = listmarkdown(mdcontent, alist)
            alist.save()

            return redirect(alist.get_absolute_url())

        print form.errors.items()
    form = EditListForm(private_checkbox, alist, from_url, initial={
        'title': alist.title,
        'description': alist.description,
        'private': not alist.is_public,
        'url': alist.url,
        'mdcontent': alist.md_content,
        'language': alist.language.language,
    })

    links = list(Link.objects.all())

    return render_template('lists/edit_list.html', {
        'list': alist,
        'form': form,
        'links': links
    })
Exemple #8
0
def edit(request, list_pk):
    alist = get_object_or_404(List, pk=list_pk)

    if request.user.pk != alist.owner.pk:
        raise PermissionDenied

    if alist.is_public:
        private_checkbox = ""
    else:
        private_checkbox = 'checked="checked"'
    if alist.url is not None and len(alist.url) > 0:
        from_url = True
    else:
        from_url = False

    if request.method == "POST":
        form = EditListForm(private_checkbox, alist, from_url, request.POST)
        if form.is_valid():
            is_private = False

            if "private" in form.data:
                is_private = form.data["private"]

            if form.data["url"]:
                opener = urllib2.build_opener()
                opener.addheaders = [("Accept-Charset", "utf-8")]
                url_response = opener.open(form.data["url"])
                mdcontent = url_response.read().decode("utf-8")
            else:
                mdcontent = form.data["mdcontent"]

            from resrc.utils.templatetags.emarkdown import listmarkdown

            from resrc.language.models import Language

            try:
                lang = Language.objects.get(language=form.data["language"])
            except Language.DoesNotExist:
                lang = Language.objects.create(language=form.data["language"])

            alist.title = form.data["title"]
            alist.description = form.data["description"]
            alist.url = form.data["url"]
            alist.md_content = mdcontent
            alist.html_content = ""
            alist.is_public = not is_private
            alist.language = lang
            alist.save()
            # once saved, we parse the markdown to add links found to list
            alist.html_content = listmarkdown(mdcontent.strip(u"\ufeff"), alist)
            alist.save()

            return redirect(alist.get_absolute_url())

    form = EditListForm(
        private_checkbox,
        alist,
        from_url,
        initial={
            "title": alist.title,
            "description": alist.description,
            "private": not alist.is_public,
            "url": alist.url,
            "mdcontent": alist.md_content,
            "language": alist.language.language,
        },
    )

    links = list(Link.objects.all())

    return render_template("lists/edit_list.html", {"list": alist, "form": form, "links": links})
Exemple #9
0
def new_link(request, title=None, url=None):
    if title is not None and url is not None:
        url = url.replace("http:/", "http://")
        url = url.replace("https:/", "https://")
        form = NewLinkForm(initial={"title": title, "url": url})

        tags = cache.get("tags_csv")
        if tags is None:
            from taggit.models import Tag

            tags = '","'.join(Tag.objects.all().values_list("name", flat=True))
            tags = '"%s"' % tags
            cache.set("tags_csv", tags, 60 * 15)

        return render_template("links/new_link_button.html", {"form": form, "tags": tags})

    if request.method == "POST":
        form = NewLinkForm(request.POST)
        if form.is_valid():
            data = form.data

            link = Link()
            link.title = data["title"]
            link.url = data["url"]
            from resrc.language.models import Language

            try:
                lang = Language.objects.get(language=data["language"])
            except Language.DoesNotExist:
                lang = Language.objects.create(language=data["language"])
            link.language = lang

            link.level = data["level"]
            link.author = request.user

            if Link.objects.filter(url=data["url"]).exists():
                return redirect(Link.objects.get(url=data["url"]).get_absolute_url())

            link.save()
            list_tags = data["tags"].split(",")
            for tag in list_tags:
                link.tags.add(tag)
                cache.delete("tags_all")
                cache.delete("tags_csv")
            link.save()
            cache.set("link_%s" % link.pk, link, 60 * 5)

            if not "ajax" in data:
                return redirect(link.get_absolute_url())

            alist = get_object_or_404(List, pk=data["id"])
            # if alist.owner != request.user:
            #    raise Http404
            from resrc.list.models import ListLinks

            if not ListLinks.objects.filter(alist=alist, links=link).exists():
                ListLinks.objects.create(alist=alist, links=link)
            from resrc.utils.templatetags.emarkdown import listmarkdown

            alist.html_content = listmarkdown(alist.md_content.strip(u"\ufeff"), alist)
            alist.save()

            data = json.dumps({"result": "added"})
            return HttpResponse(data, mimetype="application/javascript")
        else:
            if not "ajax" in form.data:
                form = NewLinkForm()

                tags = cache.get("tags_csv")
                if tags is None:
                    from taggit.models import Tag

                    tags = '","'.join(Tag.objects.all().values_list("name", flat=True))
                    tags = '"%s"' % tags
                    cache.set("tags_csv", tags, 60 * 15)

                return render_template("links/new_link.html", {"form": form, "tags": tags})
            else:
                data = json.dumps({"result": "fail"})
                return HttpResponse(data, mimetype="application/javascript")

    else:
        form = NewLinkForm()

    tags = cache.get("tags_csv")
    if tags is None:
        from taggit.models import Tag

        tags = '","'.join(Tag.objects.all().values_list("name", flat=True))
        tags = '"%s"' % tags
        cache.set("tags_csv", tags, 60 * 15)

    return render_template("links/new_link.html", {"form": form, "tags": tags})