Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/activity_review/src/Model.py
blob: 2176d4406a53fdc1397a57525a7417b846775d41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
'''
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]