Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/Workshop.activity/WorkshopModel.py
blob: 9d2b804851dff798b1291020dc483278bcca5c16 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
# Copyright (C) 2009, Tutorius.org
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
"""
WorkshopModel

This module is the model of the Workshop Activity
"""


from sugar.tutorius.vault import *
from sugar.tutorius.store import *
from dialogs import LoginDialog,WaitingDialog,ErrorDialog
import gtk
import threading
from copy import deepcopy
import gobject
import sys,traceback
import logging

class Login(object):
    def __init__(self,proxy,login_view):
        self.proxy = proxy
        self.login_view = login_view
        
    def __call__(self,f):
        def wrapper(*args):
            self.model = self.login_view.get_model()
            self.login_view = LoginDialog()
            self.login_view.set_model(self.model)
            
            if self.proxy.get_session_id() == None:
                result = self.login_view.run()
                self.login_view.destroy()
                if result == gtk.RESPONSE_ACCEPT:
                    f(*args)
            else:
                f(*args)
        return wrapper
        

class WorkshopModel():

    store_proxy = StoreProxy("http://bobthebuilder.mine.nu/tutorius/en-US/tutorius")
    login_view = LoginDialog()
    
    def __init__(self):
        self.login_view.set_model(self)
        self.store_page_number = 1
        
    def set_workshop_view(self,view):
        self.workshop_view = view
    
    def set_store_view(self,view):
        self.store_view = view
        
    def login(self,username,password):
        """
        Log a user in the store 
        
        @param username The username of the user
        @param password The password of the user
        @return True if login succeeded False otherwise
        """
        return self.store_proxy.login(username,password)

    def register(self,username,password,email):
        """
        Register a new user to the store
        
        @param username The username of the new user
        @param password The password of the user
        @param email The email of the user
        @return True if user is registered, False otherwise
        """
        return self.store_proxy.register_new_user({'nickname':username,'password':password,'email':email})

    def query(self,keyword):
        """
        Query the vault for tutorial that are linked with the keyword
        Update the currently managed tutorials and notifies the view that the managed tutorials have changed

        @param keyword the keyword for the query
        """
        if keyword is None or keyword == []:
            vault_return = Vault.query()
        else:
            vault_return = Vault.query(keyword=keyword)
                
        tutorial_list = []
        for tuto in vault_return:
            tutorial_list.append(Tutorial(tuto))
            
        self.workshop_view.set_tutorial_list(tutorial_list)

    def delete_tutorial(self,tutorial):
        """
        Delete a tutorial and updated the currently managed tutorials
        Notifies the view that the manages tutorials have changed

        @param tutorial the tutorial to delete
        """
        Vault.deleteTutorial(tutorial.id)
        self.query(None)
        self.workshop_view.display_main_view()

    def update_tutorial_infos(self,tutorial, new_infos):
        """
        Updates the metadata on a tutorial and updates the currently managed tutorials
        Notifies the view that the managed tutorials have changed

        @param tutorial the tutorial to update
        @param new_infos a dictionnary of the new informations i.e. {"title":"tut1","author":"Max Power"}
        """
        Vault.update_metadata(tutorial.id, tutorial.updated_metadata)

    @Login(store_proxy,login_view)
    def publish_tutorial(self,tutorial):
        """
        Publishes a tutorial

        Details to come
        """
    
        archive = Vault.get_tutorial_archive(tutorial.id)
        metadata = self.tutorial_to_store_metadata(tutorial)
        if not tutorial.published_state:
            
            remote_id = self.store_proxy.publish(archive,metadata)
        else:
            remote_id = self.store_proxy.update_published_tutorial(archive,metadata,tutorial.remote_id)
            

        if remote_id == -1:
            dialog = ErrorDialog("An error occured while publishing the tutorial")
            dialog.run()
            dialog.destroy()
            return

        tutorial.remote_id = remote_id
        tutorial.published_state = True
        
        metadata = self.tutorial_to_vault_metadata(tutorial)
        Vault.update_metadata(tutorial.id,metadata)

    @Login(store_proxy,login_view)
    def unpublish_tutorial(self,tutorial):
        """
        Unpublishes a tutorial

        @param tutorial The tutorial to unpublish
        """
        logging.info(tutorial.remote_id)
        self.store_proxy.unpublish(tutorial.remote_id)
    
    def launch_tutorial(self,tutorial):
        """
        Lauches a tutorial
        
        @param tutorial The tutorial to launch
        """
        from sugar.tutorius.service  import ServiceProxy
        service = ServiceProxy()

        service.launch(tutorial.id)
    
    @Login(store_proxy,login_view)
    def rate_tutorial(self,tutorial,rating):
        """
        Rate the tutorial
        
        @param tutorial The tutorial to rate
        @param rating The new rating for the tutorial
        """
        tutorial.rating = rating
        logging.info(tutorial.updated_metadata)
        logging.info(tutorial.remote_id)
        if tutorial.remote_id is not None and int(tutorial.remote_id) > 0:
            logging.info("this is here")
            self.store_proxy.rate(rating,tutorial.remote_id)
        self.workshop_view.refresh_content()
        
    def edit_tutorial(self,tutorial):
        """
        Edit a tutorial
        
        @param tutorial The tutorial to edit
        """
        pass
    
    def save_metadata(self,tutorial):
        """
        Save the metadata of a tutorial
        
        @param tutorial The tutorial to udpate containing the new metadata
        """
        metadata = self.tutorial_to_vault_metadata(tutorial)
        Vault.update_metadata(tutorial.id,metadata)
        self.workshop_view.refresh_content()

    def get_categories_for_workshop(self):
        """
        Get all categories for selecting one in the workshop
        """
        self.categories = self.store_proxy.get_categories()
        result = []
        for category in self.categories:
            result.append(category["name"])
        
        self.workshop_view.set_categories(result)
        
        
#Function related to the store

    def get_categories(self):
        """
        Get all categories of tutorial from the store
        """
        self.categories = self.store_proxy.get_categories()
        result = ['all']
        for category in self.categories:
            result.append(category["name"])
        
        self.store_view.set_categories(result)
        
    def search_store(self,keyword,category,page=1):
        """
        Search the store for specific tutorial
        
        @param keyword The keyword to search for
        @param category The category to search tutorial for
        @param page The page result to return 
        """
        tut_list = self.store_proxy.search(keyword,category,page)
        show_next = False
        show_prev = True
        if page == 1:
            show_prev = False
        if len(tut_list) == 10:
            next_list = self.store_proxy.search(keyword,category,page+1)
            if len(next_list) > 0:
                show_next = True
        tutorials = []
        for tut in tut_list:
            tutorials.append(Tutorial(tut))
        self.store_view.set_tutorial_list(tutorials)
        self.store_view.set_button_sensitive(show_prev,show_next)
        
    def get_tutorials_by_category(self,category,page=1):
        """
        Get all the tutorial in a specified category
        
        @param category The category to search
        @param The page result to return
        """
        for cat in self.categories:
            if cat["name"] == category:
                category = cat["id"]
                break
        tut_list = self.store_proxy.get_tutorials(category,page)
        show_next = False
        show_prev = True
        if page == 1:
            show_prev = False
        if len(tut_list) == 10:
            next_list = self.store_proxy.get_tutorials(category,page+1)
            if len(next_list) > 0:
                show_next = True
        tutorials = []
        for tut in tut_list:
            tutorials.append(Tutorial(tut))
        self.store_view.set_tutorial_list(tutorials)
        self.store_view.set_button_sensitive(show_prev,show_next)
        
    def download_tutorial(self,tutorial):
        """
        Download a tutorial from the store
        
        @param tutorial The tutorial to download
        """
        thread = threading.Thread(target = self.background_download,kwargs={"tutorial":tutorial})
        self.dialog = WaitingDialog(thread.start,{})
        thread.start()
        self.dialog.run()
        self.dialog.destroy()
        
    
    def background_download(self,tutorial):
        try:
            downloaded = self.store_proxy.download_tutorial(int(tutorial.remote_id))
            temp_file = open('temp.zip','w')
            temp_file.write(downloaded.read())
            temp_file.close()
            Vault.installTutorials(os.getcwd(),'temp.zip')
            temp_list = Vault.query(str(tutorial.name))
            logging.info(temp_list)
            for tut in temp_list:
                tuto = Tutorial(tut)
                if tutorial.name == tuto.name:
                    tuto.remote_id = tutorial.remote_id
                    Vault.update_metadata(tuto.id,tuto.updated_metadata)
        except:
            traceback.print_exc()
        finally:
            gobject.idle_add(self.dialog.response,0)
        
    def get_popular(self):
        tutorials = self.store_proxy.list('Popular')
        result = []
        for tutorial in tutorials:
            result.append(Tutorial(tutorial))
        self.store_view.set_popular(result)
        
    def get_also_like(self):
        tutorials = self.store_proxy.list('Recommended')
        result = []
        for tutorial in tutorials:
            result.append(Tutorial(tutorial))
        self.store_view.set_also_like(result)
        
    
        
    def tutorial_to_store_metadata(self,tutorial):
        metadata = {}
        metadata['guid'] = tutorial.id
        metadata['name'] = tutorial.name
        metadata['version'] = str(tutorial.version)
        metadata['summary'] = tutorial.description
        metadata['homepage'] = "http://www.tutorius.org"
        metadata['description'] = tutorial.description
        metadata['filename'] = tutorial.name +".zip"
        for category in self.categories:
            if category["name"] == tutorial.category:
                metadata['cat1'] = category["id"]
        logging.info(metadata)
        return metadata
    
    def tutorial_to_vault_metadata(self,tutorial):
        metadata = {}
        for key in tutorial.updated_metadata.keys():
            if key == 'activities':
                metadata[key] = tutorial.updated_metadata[key]
            else:
                metadata[key] = str(tutorial.updated_metadata[key])
        return metadata

class Tutorial(object):
    """
    Wrapper for tutorial metadata
    """
    def __init__(self,metadata_dict):
        object.__init__(self)
        self.__original_dict = metadata_dict
        self.__update_dict = {}
        for key in self.__original_dict.keys():
            self.__update_dict[key] = self.__original_dict[key]

        if 'name' in self.__original_dict:
            self.__name = self.__original_dict['name']
        else:
            self.__name = ""
            
        if 'version' in self.__original_dict:
            if self.__original_dict['version'] == '':
                self.__version = 0
            else:
                self.__version = int(self.__original_dict['version'])
        else:
            self.__version = 0

        if 'description' in self.__original_dict:
            self.__description = self.__original_dict['description']
        else:
            self.__description = ""

        if 'author' in self.__original_dict:
            self.__author = self.__original_dict['author']
        else:
            self.__author = ""

        if 'rating' in self.__original_dict:
            if self.__original_dict['rating'] == '':
                self.__rating = 0
            else:
                self.__rating = float(self.__original_dict['rating'])
        else:
            self.__rating = 0 
            
        if 'category' in self.__original_dict:
            self.__category =  self.__original_dict['category']
        else:
            self.__category = ""

        if 'publish_state' in self.__original_dict:
            #I'm sorry for this
            temp = self.__original_dict['publish_state']
            temp = temp.lower()
            if temp == 'false':
                self.__published_state = False
            elif temp == 'true':
                self.__published_state = True
            else:
                self.__published_state = None
        else:
            self.__published_state = None

        if 'guid' in self.__original_dict:
            self.__id = self.__original_dict['guid']
        else:
            self.__id = ""
            
        if 'id' in self.__original_dict:
            self.__remote_id = self.__original_dict['id']
        else:
            self.__remote_id = ""
        

    def get_name(self):
        return self.__name
    
    def set_name(self,name):
        self.__name = name
        self.__update_dict['name'] = name
        
    def get_version(self):
        return self.__version
    
    def set_version(self,version):
        self.__version = version
        self.__update_dict['version'] = version

    def get_description(self):
        return self.__description

    def set_description(self,description):
        self.__description = description
        self.__update_dict['description'] = description

    def get_author(self):
        return self.__author

    def set_author(self,author):
        self.__author = author
        self.__update_dict['author'] = author

    def get_rating(self):
        return self.__rating

    def set_rating(self,rating):
        self.__rating = rating
        self.__update_dict['rating'] = rating
        
    def get_category(self):
        return self.__category
    
    def set_category(self,category):
        self.__category = category
        self.__update_dict['category'] = category

    def get_published_state(self):
        return self.__published_state

    def set_published_state(self,published_state):
        self.__published_state = published_state
        self.__update_dict['published_state'] = published_state

    def get_id(self):
        return self.__id

    def set_id(self,id):
        self.__id = id
        self.__update_dict['guid'] = id
        
    def get_remote_id(self):
        return self.__remote_id
    
    def set_remote_id(self,id):
        self.__remote_id = id
        self.__update_dict['id'] = id

    def get_updated_metadata(self):
        return self.__update_dict

    name = property(get_name,set_name)
    version = property(get_version, set_version)
    description = property(get_description,set_description)
    author = property(get_author,set_author)
    rating = property(get_rating,set_rating)
    category = property(get_category,set_category)
    published_state = property(get_published_state,set_published_state)
    id = property(get_id,set_id)
    remote_id = property(get_remote_id, set_remote_id)
    updated_metadata = property(get_updated_metadata)