From 6fe6b906c847137f84e898e102c6385e2b05fa99 Mon Sep 17 00:00:00 2001 From: Sebastian Silva (icarito) Date: Wed, 11 Jul 2012 20:46:38 +0000 Subject: Generalize access to mounts and avoid false online mode (with 0 contexts) --- diff --git a/app/app.py b/app/app.py index 9f6514c..5d4bc0f 100644 --- a/app/app.py +++ b/app/app.py @@ -44,7 +44,7 @@ app.config['BABEL_DEFAULT_LOCALE'] = 'en' app.config['BABEL_DEFAULT_TIMEZONE'] = 'America/Lima' from cursors import * -mounts = client.mounts() +mounts = network_mount.client.mounts() @babel.localeselector def get_locale(): @@ -101,22 +101,40 @@ def inject_vars(): @app.before_request def before_request(): if 'connected' not in session: - session['connected'] = client.connected + session['connected'] = network_mount.client.connected + if session['connected']==True and network_mount.Contexts.total==0: + session['connected']=False if session['connected']==False: - g.client = offline_client - g.Contexts = offline_Contexts - g.Activities= offline_Activities - g.Projects = offline_Projects - g.autocomplete_Contexts = autocomplete_offline_Contexts + g.client = home_mount.client + g.Contexts = home_mount.Contexts + g.Activities= home_mount.Activities + g.Projects = home_mount.Projects + g.autocomplete_Contexts = home_mount.autocomplete_Contexts + g.Questions = home_mount.Questions + g.Ideas = home_mount.Ideas + g.Problems = home_mount.Problems + g.Solutions = home_mount.Solutions + g.Comments = home_mount.Comments + g.Reviews = home_mount.Reviews + g.Resources = home_mount.Resources elif session['connected']==True: - g.client = client - g.Contexts = Contexts - g.Activities= Activities - g.Projects = Projects - g.autocomplete_Contexts = autocomplete_Contexts + g.client = network_mount.client + g.Contexts = network_mount.Contexts + g.Activities= network_mount.Activities + g.Projects = network_mount.Projects + g.autocomplete_Contexts = network_mount.autocomplete_Contexts + g.Questions = network_mount.Questions + g.Ideas = network_mount.Ideas + g.Problems = network_mount.Problems + g.Solutions = network_mount.Solutions + g.Comments = network_mount.Comments + g.Reviews = network_mount.Reviews + g.Resources = network_mount.Resources else: try: cm = g.custom_mount + if cm.mountpoint!=session['connected']: + g.custom_mount = Custom_Mount(session['connected']) except AttributeError: g.custom_mount = Custom_Mount(session['connected']) cm = g.custom_mount @@ -125,6 +143,13 @@ def before_request(): g.Activities= cm.Activities g.Projects = cm.Projects g.autocomplete_Contexts = cm.autocomplete_Contexts + g.Questions = cm.Questions + g.Ideas = cm.Ideas + g.Problems = cm.Problems + g.Solutions = cm.Solutions + g.Comments = cm.Comments + g.Reviews = cm.Reviews + g.Resources = cm.Resources @app.route('/') @@ -178,13 +203,7 @@ def gen_icon(context_guid): @app.route('/launch/') def launch(context_guid): - #workaround in 0.3 : non-activity contexts - if context_guid=='sugar-network' or context_guid=='83b8e18cb6b611e1aa6b0016360ee2af': - return redirect('/context/reviews/%s' % context_guid) - try: - client.launch(context_guid) - except ServerError,RuntimeError: - pass + g.client.launch(context_guid) return redirect('/context/reviews/%s' % context_guid) @app.route('/new/resource') @@ -344,19 +363,19 @@ def resource_list(query=None): resource = session.get('last_resource') or 'all' if resource=='questions': - resource_object=Questions + resource_object=g.Questions resource_label=_("questions") elif resource=='problems': - resource_object=Problems + resource_object=g.Problems resource_label=_("problems") elif resource=='ideas': - resource_object=Ideas + resource_object=g.Ideas resource_label=_("ideas") elif resource=='reviews': - resource_object=Reviews + resource_object=g.Reviews resource_label=_("reviews") elif resource=='all': - resource_object=Resources + resource_object=g.Resources resource_label=_("resources") resource='all_' # avoid chop resource_type=resource[:-1] @@ -467,13 +486,13 @@ def users_grid(query=None): """ Users Grid """ - result = client.User.cursor(query) + result = g.client.User.cursor(query) return render_template('users-grid.html',query=query, result=result, type='user') @app.route('/_comments/', methods=['DELETE']) def del_comment(resource_guid): - client.Comment.delete(resource_guid) + g.client.Comment.delete(resource_guid) return "true" @app.route('/_comments/') @@ -492,10 +511,10 @@ def comments_browser(resource_guid=None): @app.route('/review/') def reviews_browser(resource_guid=None, review_guid=None): if review_guid: - r = client.Review(guid=review_guid, reply=['context']) + r = g.client.Review(guid=review_guid, reply=['context']) resource_guid = r['context'] return redirect ('/context/reviews/'+resource_guid) - Reviews.filter(context=resource_guid, type='review') + g.Reviews.filter(context=resource_guid, type='review') inner_template='_context-review-list.html' if '_pjax' in request.args: @@ -514,7 +533,7 @@ def reviews_browser(resource_guid=None, review_guid=None): kwargs = {'reviews':'button_selected'} return render_template(template, context=context, - result=Reviews, inner_template=inner_template, + result=g.Reviews, inner_template=inner_template, resource_label=_('reviews'),resource_type="review", **kwargs ) @@ -524,14 +543,14 @@ def reviews_browser(resource_guid=None, review_guid=None): def solution_browser(resource_guid=None): resource_type=split(request.path, "/")[1] if resource_type=='question': - resource_cursor=Questions - resource_class=client.Question + resource_cursor=g.Questions + resource_class=g.client.Question elif resource_type=='problem': - resource_cursor=Problems - resource_class=client.Problem + resource_cursor=g.Problems + resource_class=g.client.Problem elif resource_type=='idea': - resource_cursor=Ideas - resource_class=client.Idea + resource_cursor=g.Ideas + resource_class=g.client.Idea offset=int(request.args.get('cursor_offset') or 0) if offset: @@ -540,7 +559,7 @@ def solution_browser(resource_guid=None): resource=resource_class(resource_guid, reply=['guid', 'title', 'content', 'author', 'user', 'context', 'tags', 'mtime']) - Solutions.filter(parent=resource['guid']) + g.Solutions.filter(parent=resource['guid']) inner_template='_context-solution-list.html' if '_pjax' in request.args: @@ -552,7 +571,7 @@ def solution_browser(resource_guid=None): reply=['guid', 'title', 'description', 'author', 'keep', 'keep_impl', 'type']) return render_template(template, context=context, - result=Solutions, inner_template=inner_template, + result=g.Solutions, inner_template=inner_template, resource=resource, resource_type=resource_type, cursor_offset=offset) @@ -574,16 +593,16 @@ def resource_browser(context_guid=None, query=None): resource_type=split(request.path, "/")[2][:-1] if resource_type=='question': - resource_object = Questions + resource_object = g.Questions resource_label = _("questions") elif resource_type=='problem': - resource_object = Problems + resource_object = g.Problems resource_label = _("problems") elif resource_type=='idea': - resource_object = Ideas + resource_object = g.Ideas resource_label = _("ideas") elif resource_type=='al': - resource_object = Resources + resource_object = g.Resources resource_label = _("resources") resource_type='all' @@ -628,20 +647,20 @@ def edit_resource(): resource_type = request.form['resource_type'] resource_guid = request.form['edit_guid'] if resource_type=='question': - resource = client.Question(resource_guid) - resource_cursor = Questions + resource = g.client.Question(resource_guid) + resource_cursor = g.Questions elif resource_type=='idea': - resource = client.Idea(resource_guid) - resource_cursor = Ideas + resource = g.client.Idea(resource_guid) + resource_cursor = g.Ideas elif resource_type=='problem': - resource = client.Problem(resource_guid) - resource_cursor = Problems + resource = g.client.Problem(resource_guid) + resource_cursor = g.Problems elif resource_type=='review': - resource = client.Review(resource_guid) - resource_cursor = Reviews + resource = g.client.Review(resource_guid) + resource_cursor = g.Reviews elif resource_type=='solution': - resource = client.Solution(resource_guid) - resource_cursor = Solutions + resource = g.client.Solution(resource_guid) + resource_cursor = g.Solutions if request.form.get('title'): resource['title']=request.form['title'] @@ -657,14 +676,14 @@ def edit_resource(): @app.route('/submit_resource/', methods=['POST']) def new_resource(resource_type): if resource_type=='question': - resource = client.Question() - resource_cursor = Questions + resource = g.client.Question() + resource_cursor = g.Questions elif resource_type=='idea': - resource = client.Idea() - resource_cursor = Ideas + resource = g.client.Idea() + resource_cursor = g.Ideas elif resource_type=='problem': - resource = client.Problem() - resource_cursor = Problems + resource = g.client.Problem() + resource_cursor = g.Problems resource['content'] = request.form['content'] resource['title'] = request.form['title'] resource['context'] = request.form['guid'] @@ -676,45 +695,42 @@ def new_resource(resource_type): @app.route('/submit_context', methods=['POST']) def new_context(): - context=client.Context() + context=g.client.Context() context['type'] = ['project'] context['title'] = request.form['title'] context['summary'] = request.form['summary'] context['description'] = request.form['content'] if context['title'] and context['description'] and context['summary']: context.post() - Contexts._reset() + g.Contexts._reset() return redirect(url_for('context_grid', page=1, type='project')) @app.route('/submit_solution', methods=['POST']) def new_solution(): - solution=client.Solution() + solution=g.client.Solution() solution['content'] = request.form['solution'] solution['parent'] = request.form['resource_guid'] solution['parent_resource'] = request.form['resource_type'] if solution['content']: solution.post() - Solutions._reset() + g.Solutions._reset() return redirect('/%s/%s' % (solution['parent_resource'], solution['parent'])) @app.route('/submit_review', methods=['POST']) def new_review(): - review=client.Review() + review=g.client.Review() review['content'] = request.form['review'] review['title'] = '' review['type'] = 'review' context = review['context'] = request.form['resource_guid'] if review['content']: review.post() - Reviews._reset() + g.Reviews._reset() return redirect('/context/reviews/%s' % context) @app.route('/submit_report', methods=['POST']) def new_report(): - if client.connected: - report = client.Report() - else: - report = offline_client.Report() + report = g.client.Report() report['context'] = request.form['context'] report['implementation'] = request.form['implementation'] report['description'] = request.form['content'] @@ -728,13 +744,13 @@ def new_report(): @app.route('/submit_comment', methods=['POST']) def new_comment(): - comment=client.Comment() + comment=g.client.Comment() comment['message'] = request.form['comment'] comment['parent'] = request.form['resource_guid'] comment['parent_resource'] = request.form['resource_type'] if comment['message']: comment.post() - Comments._reset() + g.Comments._reset() return redirect('_comments/%s?resource_type=%s' % (comment['parent'], comment['parent_resource']) ) @@ -743,9 +759,9 @@ def new_comment(): def event_comment(): resource_type=split(request.path, "/")[2] if resource_type=='comment': - cursor = Comments + cursor = g.Comments elif resource_type=='solution': - cursor = Solutions + cursor = g.Solutions def feed(): for i in cursor.read_events(): yield 'data: %s\n\n' % i @@ -757,7 +773,7 @@ def event_network(): def feed(): while True: gevent.sleep(1) - for i in Solutions.read_events(): + for i in g.Solutions.read_events(): yield 'data: %s\n\n' % i return Response(feed(), mimetype='text/event-stream', direct_passthrough=True) diff --git a/app/cursors.py b/app/cursors.py index 991ac7e..ea18eb0 100644 --- a/app/cursors.py +++ b/app/cursors.py @@ -16,50 +16,45 @@ from sugar_network import Client -client = Client('/') -offline_client = Client('~') - -Contexts = client.Context.cursor( - reply=['guid', 'type', 'title', 'author', 'description', 'keep', 'keep_impl', 'mtime'], order_by='-mtime') -Activities = client.Context.cursor(type='activity', - reply=['guid', 'type', 'title', 'author', 'description', 'keep', 'keep_impl', 'mtime'], order_by='-mtime') -Projects = client.Context.cursor(type='project', - reply=['guid', 'type', 'title', 'author', 'description', 'keep', 'keep_impl', 'mtime'], order_by='-mtime') -autocomplete_Contexts = client.Context.cursor( - reply=['guid', 'title'], order_by='-mtime') -Questions = client.Feedback.cursor(type="question", - reply=['guid', 'type', 'title', 'content', 'context', 'author', 'user', 'tags', 'mtime'], order_by='-mtime') -Problems = client.Problem.cursor(type="problem", - reply=['guid', 'type', 'title', 'content', 'context', 'author', 'user', 'tags', 'mtime'], order_by='-mtime') -Ideas = client.Idea.cursor(type="idea", - reply=['guid', 'type', 'title', 'content', 'context', 'author', 'user', 'tags', 'mtime'], order_by='-mtime') -Solutions = client.Solution.cursor( - reply=['guid', 'content', 'parent', 'author', 'user', 'tags', 'mtime'], order_by='-mtime') -Comments = client.Comment.cursor( - reply=['guid', 'message', 'tags', 'author', 'user' ,'parent', 'parent_resource', 'mtime']) -Reviews = client.Review.cursor(type="review", - reply=['guid', 'type', 'content', 'context', 'author', 'user', 'tags', 'mtime'], order_by='-mtime') -Resources = client.Feedback.cursor( - reply=['guid', 'type', 'title', 'content', 'context', 'author', 'user', 'tags', 'mtime'], order_by='-mtime') - -offline_Contexts = offline_client.Context.cursor(keep_impl=2, - reply=['guid', 'type', 'title', 'author', 'description', 'keep', 'keep_impl', 'mtime'], order_by='-mtime') -autocomplete_offline_Contexts = offline_client.Context.cursor(keep_impl=2, - reply=['guid', 'title'], order_by='-mtime') -offline_Projects = offline_client.Context.cursor(type='project', keep_impl=2, - reply=['guid', 'type', 'title', 'author', 'description', 'keep', 'keep_impl', 'mtime'], order_by='-mtime') -offline_Activities = offline_client.Context.cursor(type='activity', keep_impl=2, - reply=['guid', 'type', 'title', 'author', 'description', 'keep', 'keep_impl', 'mtime'], order_by='-mtime') - -class Custom_Mount: - def __init__(self, mountpoint): +class Mount: + def __init__(self, mountpoint, keep_impl=None): + self.mountpoint = mountpoint self.client = Client(mountpoint) - self.Contexts = self.client.Context.cursor( + if not keep_impl: + self.Contexts = self.client.Context.cursor( + reply=['guid', 'type', 'title', 'author', 'description', 'keep', 'keep_impl', 'mtime'], order_by='-mtime') + self.autocomplete_Contexts = self.client.Context.cursor( + reply=['guid', 'title'], order_by='-mtime') + self.Projects = self.client.Context.cursor(type='project', + reply=['guid', 'type', 'title', 'author', 'description', 'keep', 'keep_impl', 'mtime'], order_by='-mtime') + self.Activities = self.client.Context.cursor(type='activity', reply=['guid', 'type', 'title', 'author', 'description', 'keep', 'keep_impl', 'mtime'], order_by='-mtime') - self.autocomplete_Contexts = self.client.Context.cursor( - reply=['guid', 'title'], order_by='-mtime') - self.Projects = self.client.Context.cursor(type='project', + else: + self.Contexts = self.client.Context.cursor(keep_impl=keep_impl, + reply=['guid', 'type', 'title', 'author', 'description', 'keep', 'keep_impl', 'mtime'], order_by='-mtime') + self.autocomplete_Contexts = self.client.Context.cursor(keep_impl=keep_impl, + reply=['guid', 'title'], order_by='-mtime') + self.Projects = self.client.Context.cursor(type='project', keep_impl=keep_impl, + reply=['guid', 'type', 'title', 'author', 'description', 'keep', 'keep_impl', 'mtime'], order_by='-mtime') + self.Activities = self.client.Context.cursor(type='activity', keep_impl=keep_impl, reply=['guid', 'type', 'title', 'author', 'description', 'keep', 'keep_impl', 'mtime'], order_by='-mtime') - self.Activities = self.client.Context.cursor(type='activity', - reply=['guid', 'type', 'title', 'author', 'description', 'keep', 'keep_impl', 'mtime'], order_by='-mtime') + + self.Questions = self.client.Feedback.cursor(type="question", + reply=['guid', 'type', 'title', 'content', 'context', 'author', 'user', 'tags', 'mtime'], order_by='-mtime') + self.Problems = self.client.Problem.cursor(type="problem", + reply=['guid', 'type', 'title', 'content', 'context', 'author', 'user', 'tags', 'mtime'], order_by='-mtime') + self.Ideas = self.client.Idea.cursor(type="idea", + reply=['guid', 'type', 'title', 'content', 'context', 'author', 'user', 'tags', 'mtime'], order_by='-mtime') + self.Solutions = self.client.Solution.cursor( + reply=['guid', 'content', 'parent', 'author', 'user', 'tags', 'mtime'], order_by='-mtime') + self.Comments = self.client.Comment.cursor( + reply=['guid', 'message', 'tags', 'author', 'user' ,'parent', 'parent_resource', 'mtime']) + self.Reviews = self.client.Review.cursor(type="review", + reply=['guid', 'type', 'content', 'context', 'author', 'user', 'tags', 'mtime'], order_by='-mtime') + self.Resources = self.client.Feedback.cursor( + reply=['guid', 'type', 'title', 'content', 'context', 'author', 'user', 'tags', 'mtime'], order_by='-mtime') + +network_mount = Mount('/') +home_mount = Mount('~', keep_impl=2) + -- cgit v0.9.1