import urllib
from io import StringIO

from django.contrib.auth.decorators import login_required
from django.contrib.sites.shortcuts import get_current_site
from django.http import HttpResponse
from django.shortcuts import redirect
from django.template import loader, RequestContext
from flask import url_for

from topics.helpers import view_helpers
from topics.helpers.view_helpers import normalize_url
from topics.models import Link
import lxml.html
import requests

view_helpers.setup()


def get_topic(request, topic_name):
    if request.POST:
        return save_link(request, topic_name)

    template = loader.get_template('topics/show_topic.html')
    links = Link.get_all_links(get_current_site(request))
    context = RequestContext(request, {
        'links': links,
    })
    return HttpResponse(template.render(context))


@login_required
def save_link(request, topic_name):
    # Fix url
    url_raw = request.POST.get('url')
    if not url_raw.startswith('http'):
        url_raw = 'http://' + url_raw
    url = urllib.parse.urlparse(url_raw)
    url_full = normalize_url(url)

    # Parse title & icon
    page = requests.get(url_full)
    parsed = lxml.html.parse(StringIO(page.text))
    title = parsed.find(".//title").text
    icon = None
    icon_node = parsed.xpath('.//link[contains(@rel, "icon")]')
    if icon_node:
        icon = icon_node[0].attrib.get("href")
        icon = normalize_url(urllib.parse.urlparse(icon), url.netloc)
    if not icon:
        icon = "{}://{}/favicon.ico".format(url.scheme, url.netloc)

    link = Link(
            link=url_full,
            title=title,
            icon=icon,
            user=request.user,
            site=get_current_site(request)
    )
    link.save()
    return redirect("topics:get_topic", topic_name)

@@ -7,16 +7,36 @@
  urllib%0A
+import urllib.parse%0A
 from io 
@@ -52,16 +52,52 @@
 ringIO%0A%0A
+from django.contrib import messages%0A
 from dja
@@ -206,16 +206,53 @@
 nt_site%0A
+from django.db import IntegrityError%0A
 from dja
@@ -318,16 +318,16 @@
 edirect%0A
+
 from dja
@@ -372,34 +372,8 @@
 text
-%0Afrom flask import url_for
 %0A%0Afr
@@ -1786,18 +1786,114 @@
     
-link.save(
+try:%0A        link.save()%0A    except IntegrityError:%0A        messages.error(request, 'Link already posted.'
 )%0A  

Add error message to topics