From 1c0b5d7c47dc0ebfe8d445cfe81ff35dfbaf560f Mon Sep 17 00:00:00 2001 From: Charlie Date: Thu, 05 Nov 2009 15:55:20 +0000 Subject: Workshop activity My tutorials --- (limited to 'Workshop.activity/WorkshopModel.py') diff --git a/Workshop.activity/WorkshopModel.py b/Workshop.activity/WorkshopModel.py new file mode 100644 index 0000000..b085584 --- /dev/null +++ b/Workshop.activity/WorkshopModel.py @@ -0,0 +1,245 @@ +""" +WorkshopModel + +This module is the model of the Workshop Activity +""" + +from sugar.tutorius.vault import * +from sugar.tutorius.store import * +from dialogs import LoginDialog + +class Login(object): + def __init__(self,proxy,login_view): + self.proxy = proxy + self.login_view = login_view + + def __call__(self,f): + def wrapper(*args): + if self.proxy.get_session_id() == None: + self.login_view.run() + self.login_view.destroy() + f(*args) + return wrapper + + +class WorkshopModel(): + + store_proxy = StoreProxy("http://bobthebuilder.mine.nu/tutorius/en-US/tutorius") + login_view = LoginDialog() + + def __init__(self,view): + self.view = view + self.login_view.set_model(self) + + 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 + """ + vault_return = Vault.query() + tutorial_list = [] + for tuto in vault_return: + tutorial_list.append(Tutorial(tuto)) + + self.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) + + 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"} + """ + pass + + @Login(store_proxy,login_view) + def publish_tutorial(self,tutorial): + """ + Publishes a tutorial + + Details to come + """ + if not tutorial.published_state: + binary = vault.get_tutorial_binary(tutorial.id) + + + @Login(store_proxy,login_view) + def unpublish_tutorial(self,tutorial): + """ + Unpublishes a tutorial + + Details to come + """ + pass + + def launch_tutorial(self,tutorial): + """ + Lauches a tutorial + + @param tutorial The tutorial to launch + """ + pass + + @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 + self.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 + """ + self.view.refresh_content() + + +class Tutorial(): + """ + Wrapper for tutorial metadata + """ + def __init__(self,metadata_dict): + self.__original_dict = metadata_dict + self.__update_dict = metadata_dict + + if 'name' in self.__original_dict: + self.__name = self.__original_dict['name'] + else: + self.__description = name + + 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: + self.__rating = float(self.__original_dict['rating']) + else: + self.__rating = 0 + + 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 = "" + + def get_name(self): + return self.__name + + def set_name(self,name): + self.__name = name + + 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_published_state(self): + return self.__published_state + + def set_published_state(self,published_state): + self.__published_state = published_state + self.__update_dict['PublishedState'] = published_state + + def get_id(self): + return self.__id + + def set_id(self,id): + self.__id = id + self.__update_dict['TutorialId'] = id + + def get_updated_metadata(self): + return self.__update_dict + + name = property(get_name,set_name) + description = property(get_description,set_description) + author = property(get_author,set_author) + rating = property(get_rating,set_rating) + published_state = property(get_published_state,set_published_state) + id = property(get_id,set_id) + updated_metadata = property(get_updated_metadata) + + + + + -- cgit v0.9.1