Ejemplo n.º 1
0
def _save_user_data_dir(u, user_mc):
    # make a dir first (prefix with user_id for extra security)
    temp_dir = tempfile.mkdtemp(prefix='user{}'.format(u.profile['auth_users_id']))
    # user profile
    with open(os.path.join(temp_dir, 'profile.json'), 'w') as outfile:
        profile = u.profile
        json.dump(profile, outfile)
    # topic-level permissions
    with open(os.path.join(temp_dir, 'topic-permissions.csv'), 'w') as outfile:
        topics = user_mc.topicList(limit=1000)['topics']
        user_owned_topics = topics_user_owns(topics, u.profile['email'])
        topic_permission_list = [{
            'topics_id': t['topics_id'],
            'topic_name': t['name'],
            'permission': t['user_permission'],
        } for t in user_owned_topics]
        writer = csv.DictWriter(outfile, ['topics_id', 'topic_name', 'permission'])
        writer.writeheader()
        writer.writerows(topic_permission_list)
    # saved searches
    with open(os.path.join(temp_dir, 'saved-searches.json'), 'w') as outfile:
        search_list = user_db.get_users_lists(u.name, 'searches')
        json.dump(search_list, outfile)
    # starred sources
    with open(os.path.join(temp_dir, 'starred-sources.csv'), 'w') as outfile:
        user_favorited = user_db.get_users_lists(user_name(), 'favoriteSources')
        media_sources = [user_mc.media(media_id) for media_id in user_favorited]
        media_sources = [{
            'media_id': m['media_id'],
            'name': m['name'],
            'url': m['url']
        } for m in media_sources]
        writer = csv.DictWriter(outfile, ['media_id', 'name', 'url'])
        writer.writeheader()
        writer.writerows(media_sources)
    # starred collections
    with open(os.path.join(temp_dir, 'starred-collections.csv'), 'w') as outfile:
        user_favorited = user_db.get_users_lists(user_name(), 'favoriteCollections')
        collections = [user_mc.tag(tags_id) for tags_id in user_favorited]
        collections = [{
            'tags_id': c['tags_id'],
            'label': c['label'],
            'description': c['description']
        } for c in collections]
        writer = csv.DictWriter(outfile, ['tags_id', 'label', 'description'])
        writer.writeheader()
        writer.writerows(collections)
    # starred topics
    with open(os.path.join(temp_dir, 'starred-topics.csv'), 'w') as outfile:
        user_favorited = user_db.get_users_lists(user_name(), 'favoriteTopics')
        topics = [user_mc.topic(topics_id) for topics_id in user_favorited]
        topics = [{
            'topics_id': t['topics_id'],
            'name': t['name'],
            'description': t['description']
        } for t in topics]
        writer = csv.DictWriter(outfile, ['topics_id', 'name', 'description'])
        writer.writeheader()
        writer.writerows(topics)
    return temp_dir
Ejemplo n.º 2
0
def favorite_sources():
    user_mc = user_mediacloud_client()
    user_favorited = user_db.get_users_lists(user_name(), 'favoriteSources')
    favorited_s = [user_mc.media(media_id) for media_id in user_favorited]
    for s in favorited_s:
        s['isFavorite'] = True
    return jsonify({'list': favorited_s})
Ejemplo n.º 3
0
def topic_favorites():
    user_mc = user_mediacloud_client()
    favorite_topic_ids = user_db.get_users_lists(user_name(), 'favoriteTopics')
    favorited_topics = [user_mc.topic(tid) for tid in favorite_topic_ids]
    for t in favorited_topics:
        t['isFavorite'] = True
    return jsonify({'topics': favorited_topics})
Ejemplo n.º 4
0
def favorite_collections():
    user_mc = user_mediacloud_client()
    user_favorited = user_db.get_users_lists(user_name(),
                                             'favoriteCollections')
    favorited_collections = [user_mc.tag(tag_id) for tag_id in user_favorited]
    for s in favorited_collections:
        s['isFavorite'] = True
    return jsonify({'list': favorited_collections})
Ejemplo n.º 5
0
def topic_favorites():
    user_mc = user_mediacloud_client()
    favorite_topic_ids = user_db.get_users_lists(user_name(), 'favoriteTopics')
    favorited_topics = [user_mc.topic(tid) for tid in favorite_topic_ids]
    for t in favorited_topics:
        t['isFavorite'] = True
        # t['detailInfo'] = get_topic_info_per_snapshot_timespan(t['topics_id'])
    return jsonify({'topics': favorited_topics})
Ejemplo n.º 6
0
def add_user_favorite_flag_to_sources(sources):
    user_favorited = user_db.get_users_lists(user_name(), 'favoriteSources')
    for s in sources:
        s['isFavorite'] = int(s['media_id']) in user_favorited
    return sources
Ejemplo n.º 7
0
def add_user_favorite_flag_to_collections(collections):
    user_favorited = user_db.get_users_lists(user_name(),
                                             'favoriteCollections')
    for c in collections:
        c['isFavorite'] = int(c['tags_id']) in user_favorited
    return collections
Ejemplo n.º 8
0
def add_user_favorite_flag_to_topics(topics):
    user_favorited = user_db.get_users_lists(user_name(), 'favoriteTopics')
    for t in topics:
        t['isFavorite'] = t['topics_id'] in user_favorited
    return topics
Ejemplo n.º 9
0
def _add_user_favorite_flag_to_topics(topics):
    user_favorited = user_db.get_users_lists(user_name(), 'favoriteTopics')
    for t in topics:
        t['isFavorite'] = t['topics_id'] in user_favorited
    return topics
Ejemplo n.º 10
0
def load_user_searches():
    username = user_name()
    search_list = user_db.get_users_lists(username, 'searches')
    return jsonify({'list': search_list})