Example #1
0
    def tag(self, message):
        components = message.split(" ")
        subcommand = components[0]

        if subcommand == "add":
            if len(components[1:]) != 1:
                return f"invalid syntax: '{message}' had the wrong # of arguments ({len(components[1:])})"
            t = db.Tag(name=components[1])
            self.db.store_tag(t)
            return f"added tag {t.name}"
        elif subcommand == "apply":
            if len(components[1:]) != 2:
                return f"invalid syntax: '{message}' had the wrong # of arguments ({len(components[1:])})"
            review_id = components[1]
            tag_name = components[2]
            # is this a real tag?
            tag = self.db.retrieve_tag(tag_name)
            if not tag:
                return f"I can't find a tag named {tag_name}!"

            # OK, it's real, is this review real?
            review = self.db.retrieve_review(review_id)
            if not review:
                return f"I can't find a review with the ID {review_id}!"

            self.db.tag_review(review=review, tag=tag)
            return f"tagged review #{review.key} with {tag.name}"
        elif subcommand == "list":
            tags = [str(t) for t in self.db.retrieve_tags()]
            if not tags:
                return "I don't have any tags"
            return "\n".join(tags)
Example #2
0
def tag_added(title, user_id, artwork):
    if not title:
        return None

    title = title.strip()
    url_name = tag_url_name(title)
    if len(title) <= 1 or len(title) > 64:
        return None

    if hide_bad_language(title) != title:
        return None

    global_tag = db.Tag.all().filter('url_name =', url_name).get()
    if global_tag:
        if getattr(global_tag, 'count') is not None:
            global_tag.count = global_tag.count + 1
            global_tag.last_date = datetime.now()
            if not global_tag.cover:
                global_tag.cover = artwork
            global_tag.put()
    else:
        tag = db.Tag.all().filter('url_name =', url_name).get()
        if not tag:
            tag = db.Tag()
            tag.title = title
            tag.title_lower = title.lower()
            tag.url_name = url_name
            tag.date = datetime.now()
            tag.last_date = datetime.now()
            tag.cover = artwork
            tag.count = 1
            tag.put()

    user_tag = db.UserTag.all().filter('user_id =',
                                       user_id).filter('url_name',
                                                       url_name).get()
    if user_tag:
        user_tag.count = user_tag.count + 1
        user_tag.last_date = datetime.now()
        if not user_tag.cover:
            user_tag.cover = artwork
        user_tag.put()
    else:
        user_tag = db.UserTag()
        user_tag.user_id = user_id
        user_tag.url_name = url_name
        user_tag.cover = artwork
        user_tag.title = title
        user_tag.title_lower = title.lower()
        user_tag.date = datetime.now()
        user_tag.last_date = datetime.now()
        user_tag.count = 1
        user_tag.put()

    cache.delete(cache.MC_TAG + url_name)
    return url_name
Example #3
0
def tag_by_url_name(url_name):
    cache_tag = cache.get(cache.MC_TAG + url_name)
    if cache_tag:
        return cache_tag
    else:
        tag = db.Tag.all().filter('url_name =', url_name).get()
        if tag:
            cache.add(cache.MC_TAG + url_name, tag)
        else:
            tag = db.Tag()
            tag.url_name = url_name
            tag.title = url_name
            tag.title_lower = url_name

        return tag
Example #4
0
def add_edit_tags(id_article, tags):

    try:
        article_res = db.session.query(
            db.Article.id).filter(db.Article.id == id_article).one_or_none()
        if not article_res or len(article_res) == 0:
            raise ArticleDoesntExist

    except sqlalchemy.exc.IntegrityError:
        db.session.rollback()
        raise ArticleAlreadyExistsException

    try:
        tag_res = db.session.query(
            db.Tag).filter(db.Tag.id_article == id_article).all()
    except sqlalchemy.exc.IntegrityError:
        db.session.rollback()
        raise TagsDoesentExist

    if not check_tags(tags):
        raise TagsNotValidException

    if tag_res or len(tag_res) != 0:
        for i in tag_res:

            if i.tag in tags:
                tags.remove(i.tag)
    try:
        for tag in tags:
            aid = randomword(10, 'id')
            tag = db.Tag(aid, id_article, tag)
            db.session.add(tag)
            db.session.commit()

    except sqlalchemy.exc.IntegrityError:
        db.session.rollback()
        raise TagsInsertErrorException

    return True
Example #5
0
                new_db.session.add(o)
                new_db.session.commit()
            except Exception, e:
                log.error(e)
                continue
            medium_mapper[i[0]] = o.medium_id

    # tags
    tag_mapper = {}
    old_cursor.execute("SELECT id, name FROM tags;")
    for i in old_cursor.fetchall():
        o = new_db.session.query(db.Tag).filter_by(name=i[1]).first()
        if o is not None:
            tag_mapper[i[0]] = o.tag_id
        else:
            o = db.Tag(name=i[1])
            try:
                new_db.session.add(o)
                new_db.session.commit()
            except Exception, e:
                log.error(e)
                continue
            tag_mapper[i[0]] = o.tag_id

    # movies
    movie_mapper = {}
    old_cursor.execute("""
        SELECT id, volume_id, collection_id, original_title, title, director,
            number, image, plot, country, year, runtime, classification,
            genre, studio, site, imdb, actors, trailer, rating, loaned,
            media, num_media, obs, seen, region, condition, color, layers