Exemple #1
0
def store_user_data(user, tag_history, tag_graph, start, end, append_to):
    user_entity = User.get_by_id(user, namespace=DS_VERSION)
    if user_entity is None:
        return False

    if append_to is not None:
        # merge histories
        hist_frag = TagHistory.get_by_id(append_to,
                                         parent=user_entity.key,
                                         namespace=DS_VERSION)

        hist_frag.tag_history['weeks'] += tag_history['weeks']
        hist_frag.end = end
        hist_frag.put()

        # merge graphs
        graph_frag = TagGraph.get_by_id(append_to,
                                        parent=user_entity.key,
                                        namespace=DS_VERSION)

        old_graph = graph_frag.tag_graph

        for tag in tag_graph:
            if tag in old_graph:
                old_graph[tag]['plays'] += tag_graph[tag]['plays']
                old_graph[tag]['adj'] = old_graph[tag]['adj'].union(
                    tag_graph[tag]['adj'])
            else:
                old_graph[tag] = tag_graph[tag]
        graph_frag.end = end
        graph_frag.put()

        # update end timestamp in user fragments chart
        frag_info = next(frag_info for frag_info in user_entity.fragments
                         if frag_info['start'] == hist_frag.start)
        frag_info['end'] = end

    else:
        user_entity.fragments.append({'start': start, 'end': end})

        if tag_history['weeks']:
            TagHistory(id=user + str(start),
                       tag_history=tag_history,
                       start=start,
                       end=end,
                       parent=user_entity.key,
                       namespace=DS_VERSION).put_async()
            TagGraph(id=user + str(start),
                     tag_graph=tag_graph,
                     start=start,
                     end=end,
                     parent=user_entity.key,
                     namespace=DS_VERSION).put_async()

    user_entity.put()

    return True