''' Created on 22 Oct 2012 @author: cgueret ''' from semanticxo import graphstore, util, addressbook import time from semanticxo.graphstore import GraphStore class ReviewModel(object): def __init__(self): ''' Constructor ''' pass def get_talks_list(self): ''' Return a list of the title for the different talks stored ''' gstore = graphstore.get_instance() talks_list = gstore.get_resources_list(restrict='Talk') return talks_list def add_new_talk(self, title): ''' Store a new talk ''' # Get the graph store instance gstore = graphstore.get_instance() # Create a graph, add a new talk in it graph = gstore.create_graph() resource = graph.create_resource(uid=None, category='Talk') resource.add("title", title) graph.add_share(util.public_uri()) # Save the graph gstore.persist_graph(graph) # Push it to the other devices contacts = addressbook.get_neighbours() for host in contacts.itervalues(): remote_graph_store = GraphStore(hostname=host) remote_graph_store.persist_graph(graph) return resource.get_resource_uri() def get_comments_for_talk(self, talk): ''' Get the list of comments associated to a talk ''' gstore = graphstore.get_instance() comment_id_list = gstore.get_resources_list(restrict='Comment') results = [] for comment_id in comment_id_list: comment = gstore.get_resource(comment_id) if comment.get("talk")[0] == talk: message = comment.get("comment")[0] date = comment.get("date")[0] author = comment.get("author")[0] results.append((comment_id, message, date, author)) return results def add_new_comment_for_talk(self, talk, comment): ''' Add a new comment for a talk ''' gstore = graphstore.get_instance() # First, try to see if there is already a graph for our comments graph = gstore.get_graph_by_name(name="Comments") # If the graph is not found, create it if graph == None: graph = gstore.create_graph(name="Comments") # Record the comment comment_resource = graph.create_resource(uid=None, category='Comment') comment_resource.set("comment", comment) comment_resource.set("date", int(time.time())) comment_resource.set("author", util.device_uri()) comment_resource.set("talk", talk) # Persist the graph gstore.persist_graph(graph) def set_mood_for_talk(self, talk, mood): ''' Set the mood for a talk ''' gstore = graphstore.get_instance() # Try to find the mood instance graph = gstore.get_graph_by_name(name="Moods") # If the graph is not found, create it if graph == None: graph = gstore.create_graph(name="Moods") # Try to find the associated mood object mood_resources = graph.find_resources(key='talk', value=talk) if len(mood_resources) == 0: mood_resource = graph.create_resource(uid=None, category='Mood') mood_resource.set("mood", mood) mood_resource.set("talk", talk) else: mood_resource = graph.get_resource(mood_resources[0]) mood_resource.set("mood", mood) # Persist the graph gstore.persist_graph(graph) def get_mood_for_talk(self, talk): ''' Get the mood associated to a talk, if any ''' gstore = graphstore.get_instance() # Try to find the mood instance graph = gstore.get_graph_by_name(name="Moods") # If the graph is not found, create it if graph == None: graph = gstore.create_graph(name="Moods") # Try to find the associated mood object mood_resources = graph.find_resources(key='talk', value=talk) if len(mood_resources) == 0: return None # Return the value return graph.get_resource(mood_resources[0]).get("mood")[0]