Esempio n. 1
0
def index():
    if request.method == 'POST':
        thing = request.form.get('url')
        if thing:
            if '://' not in thing:
                thing = 'http://' + thing

            # Verify the URL
            parsed = urlparse(thing)
            if parsed.scheme not in ('http', 'https'):
                return "I only take HTTP or HTTPS URLs, dummy"

            urlhash = hashlib.sha1(thing).hexdigest()
            try:
                url = Url.get(Url.url_hash == urlhash)
            except:
                url = Url()
                url.url = thing
                url.url_hash = urlhash
                url.created = datetime.datetime.now()
                url.save()

                # hokay. got us an ID, let's make a key.
                url.key = base36_encode(url.id)
                url.save()

            return render_template('added.html', short_url="http://{0}/{1}".format(request.headers['host'], url.key))
        else:
            return "You didn't give me shit"
    else:
        return render_template('index.html')
Esempio n. 2
0
    def importIntoVersion(version):
        logging.info("comparing to version number %i" % version.number)
        topic = Topic.get_by_id("art-history", version)

        if not topic:
            parent = Topic.get_by_id("humanities---other", version)
            if not parent:
                raise Exception(
                    "Could not find the Humanities & Other topic to put art history into"
                )
            topic = Topic.insert(
                title="Art History",
                parent=parent,
                id="art-history",
                standalone_title="Art History",
                description=
                "Spontaneous conversations about works of art where the speakers are not afraid to disagree with each other or art history orthodoxy. Videos are made by Dr. Beth Harris and Dr. Steven Zucker along with other contributors."
            )

        urls = topic.get_urls(include_descendants=True)
        href_to_key_dict = dict((url.url, url.key()) for url in urls)
        hrefs = [url.url for url in urls]
        content = getSmartHistoryContent()
        if content is None:
            raise Exception(
                "Aborting import, could not read from smarthistory")

        subtopics = topic.get_child_topics()
        subtopic_dict = dict((t.title, t) for t in subtopics)
        subtopic_child_keys = {}

        new_subtopic_keys = []

        child_keys = []
        i = 0
        for link in content:
            href = link["href"]
            title = link["title"]
            parent_title = link["parent"]

            if parent_title not in subtopic_dict:
                subtopic = Topic.insert(title=parent_title,
                                        parent=topic,
                                        standalone_title="Art History: %s" %
                                        parent_title,
                                        description="")
                subtopic_dict[parent_title] = subtopic
            else:
                subtopic = subtopic_dict[parent_title]

            if subtopic.key() not in new_subtopic_keys:
                new_subtopic_keys.append(subtopic.key())

            if parent_title not in subtopic_child_keys:
                subtopic_child_keys[parent_title] = []

            if href not in hrefs:
                logging.info("adding %i %s %s to %s" %
                             (i, href, title, parent_title))
                models.VersionContentChange.add_new_content(
                    models.Url, version, {
                        "title": title,
                        "url": href
                    }, ["title", "url"])

                url = Url(url=href, title=title, id=id)
                url.put()
                subtopic_child_keys[parent_title].append(url.key())

            else:
                subtopic_child_keys[parent_title].append(
                    href_to_key_dict[href])

            i += 1

        logging.info("updating child_keys")
        change = False
        for parent_title, child_keys in subtopic_child_keys.iteritems():
            subtopic = subtopic_dict[parent_title]
            if subtopic.child_keys != subtopic_child_keys[parent_title]:
                change = True
                subtopic.update(child_keys=subtopic_child_keys[parent_title])

        if topic.child_keys != new_subtopic_keys:
            change = True
            topic.update(child_keys=new_subtopic_keys)

        if change:
            logging.info("finished updating version number %i" %
                         version.number)
        else:
            logging.info("nothing changed")

        return change
Esempio n. 3
0
    def importIntoVersion(version):
        logging.info("comparing to version number %i" % version.number)
        topic = Topic.get_by_id("art-history", version)

        if not topic:
            parent = Topic.get_by_id("humanities---other", version)
            if not parent:
                raise Exception("Could not find the Humanities & Other topic to put art history into")
            topic = Topic.insert(title="Art History",
                                 parent=parent,
                                 id="art-history",
                                 standalone_title="Art History",
                                 description="Spontaneous conversations about works of art where the speakers are not afraid to disagree with each other or art history orthodoxy. Videos are made by Dr. Beth Harris and Dr. Steven Zucker along with other contributors.")
        
        urls = topic.get_urls(include_descendants=True)
        href_to_key_dict = dict((url.url, url.key()) for url in urls)
        hrefs = [url.url for url in urls]
        content = getSmartHistoryContent()
        if content is None:
            raise Exception("Aborting import, could not read from smarthistory")

        subtopics = topic.get_child_topics()
        subtopic_dict = dict((t.title, t) for t in subtopics)
        subtopic_child_keys = {}
        
        new_subtopic_keys = []

        child_keys = []
        i = 0
        for link in content:
            href = link["href"]
            title = link["title"]
            parent_title = link["parent"]

            if parent_title not in subtopic_dict:
                subtopic = Topic.insert(title=parent_title,
                                 parent=topic,
                                 standalone_title="Art History: %s" % parent_title,
                                 description="")
                subtopic_dict[parent_title] = subtopic
            else:
                subtopic = subtopic_dict[parent_title]
           
            if subtopic.key() not in new_subtopic_keys:
                new_subtopic_keys.append(subtopic.key())

            if parent_title not in subtopic_child_keys:
                 subtopic_child_keys[parent_title] = []

            if href not in hrefs:
                logging.info("adding %i %s %s to %s" % (i, href, title, parent_title))
                models.VersionContentChange.add_new_content(
                    models.Url, 
                    version,
                    {"title": title,
                     "url": href
                    },
                    ["title", "url"])

                url = Url(url=href,
                          title=title,
                          id=id)
                url.put()
                subtopic_child_keys[parent_title].append(url.key())

            else:
                subtopic_child_keys[parent_title].append(href_to_key_dict[href])

            i += 1

        logging.info("updating child_keys")
        change = False
        for parent_title, child_keys in subtopic_child_keys.iteritems():
            subtopic = subtopic_dict[parent_title]
            if subtopic.child_keys != subtopic_child_keys[parent_title]:
                change = True
                subtopic.update(child_keys=subtopic_child_keys[parent_title])
        
        if topic.child_keys != new_subtopic_keys:    
            change = True
            topic.update(child_keys=new_subtopic_keys)
        
        if change:
            logging.info("finished updating version number %i" % version.number)
        else:
            logging.info("nothing changed")

        return change