def bundle_update(language, target): if language == 'en_ALL': # Don't allow to translate texts in English return jsonify(result="error") app_url = request.values.get('app_url') try: request_data = request.get_json(force=True, silent=True) or {} except ValueError: request_data = {} key = request_data.get("key") value = request_data.get("value") if key is None or value is None: return jsonify(**{"result": "error"}) user = current_golab_user() translation_url, original_messages, metadata = extract_local_translations_url(app_url, force_local_cache = True) translated_messages = { key : value } add_full_translation_to_app(user.email, app_url, translation_url, metadata, language, target, translated_messages, original_messages, from_developer = False) from appcomposer.translator.tasks import task_synchronize_single_app task_synchronize_single_app.delay("update", app_url) return jsonify(**{"result": "success"})
def translation_upload(): default_language = _guess_default_language() if default_language: form = UploadForm(language = default_language) else: form = UploadForm() if form.validate_on_submit(): errors = False app_url = form.url.data try: translation_url, original_messages, metadata = extract_local_translations_url(app_url) except Exception as e: traceback.print_exc() form.url.errors = [unicode(e)] errors = True xml_contents = form.opensocial_xml.data.read() if isinstance(xml_contents, str): xml_contents = unicode(xml_contents, 'utf8') try: translated_messages, metadata = extract_messages_from_translation(xml_contents) except Exception as e: traceback.print_exc() form.opensocial_xml.errors = [unicode(e)] errors = True if not errors: language = form.language.data target = form.target.data add_full_translation_to_app(current_golab_user(), app_url, translation_url, metadata, language, target, translated_messages, original_messages, from_developer = False) flash("Contents successfully added") return render_template('translator/translations_upload.html', form=form)
def wrapper(*args, **kwargs): user = current_golab_user() lock_key = 'locks:{}'.format(user.email) db.session.remove() counter = 10 while True: # Maximum: lock for 15 seconds lock_key = rlock.lock(lock_key, 15 * 1000) if lock_key: try: return func(*args, **kwargs) finally: try: rlock.unlock(lock_key) except: return make_response( json.dumps({ 'result': 'error', 'message': 'Unable to unlock!!!' }), 500) counter = counter - 1 if counter < 0: return make_response( json.dumps({ 'result': 'error', 'message': 'Unable to lock' }), 500)
def bundle_update(language, target): if language == 'en_ALL': # Don't allow to translate texts in English return jsonify(result="error") app_url = request.values.get('app_url') try: request_data = request.get_json(force=True, silent=True) or {} except ValueError: request_data = {} key = request_data.get("key") value = request_data.get("value") if key is None or value is None: return jsonify(**{"result": "error"}) user = current_golab_user() translation_url, original_messages, metadata = extract_local_translations_url( app_url, force_local_cache=True) translated_messages = {key: value} add_full_translation_to_app(user.email, app_url, translation_url, metadata, language, target, translated_messages, original_messages, from_developer=False) from appcomposer.translator.tasks import task_synchronize_single_app task_synchronize_single_app.delay("update", app_url) return jsonify(**{"result": "success"})
def create(): form = ApplicationForm() if form.validate_on_submit(): form_scale = _get_scale_value(form) application = EmbedApplication(url=form.url.data, name=form.name.data, owner=current_golab_user(), height=form.height.data, scale=form_scale) db.session.add(application) try: db.session.commit() except Exception as e: traceback.print_exc() return render_template( "embed/error.html", message=gettext( "There was an error creating an application")), 500 else: return redirect(url_for('.edit', identifier=application.identifier)) return render_template("embed/create.html", form=form, header_message=gettext("Add a web"))
def check_authn(): cur_url = request.values.get("cur_url") golab_user = current_golab_user() if golab_user: return jsonify(**{ "result" : "ok", "display_name" : golab_user.display_name }) else: return jsonify(**{ "result" : "fail", "redirect" : url_for('graasp_oauth_login', next = cur_url, _external = True) })
def bundle_update(language, target): app_url = request.values.get('app_url') key = request.values.get("key") value = request.values.get("value") if key is None or value is None: return jsonify(**{"result": "error"}) user = current_golab_user() translation_url, original_messages, metadata = extract_local_translations_url(app_url, force_local_cache = True) translated_messages = { key : value } add_full_translation_to_app(user, app_url, translation_url, metadata, language, target, translated_messages, original_messages, from_developer = False) return jsonify(**{"result": "success"})
def check_authn(): cur_url = request.values.get("cur_url") golab_user = current_golab_user() if golab_user: return jsonify(**{ "result": "ok", "display_name": golab_user.display_name }) else: return jsonify( **{ "result": "fail", "redirect": url_for('graasp_oauth_login', next=cur_url, _external=True) })
def filter(self, record): try: record.environ = pprint.pformat(request.environ) except RuntimeError: # This on production will raise an "out of request context" error. We ignore it. # TODO: Check if there is some way to detect that we are outside, rather than rely on an exception. return False try: from appcomposer.login import current_golab_user user = current_golab_user() if user is None: record.user = '******' else: record.user = user.email except: record.user = '******' return True
def wrapper(*args, **kwargs): user = current_golab_user() lock_key = 'locks:{}'.format(user.email) db.session.remove() counter = 10 while True: # Maximum: lock for 15 seconds lock_key = rlock.lock(lock_key, 15 * 1000) if lock_key: try: return func(*args, **kwargs) finally: try: rlock.unlock(lock_key) except: return make_response(json.dumps({ 'result': 'error', 'message': 'Unable to unlock!!!'}), 500) counter = counter - 1 if counter < 0: return make_response(json.dumps({ 'result': 'error', 'message': 'Unable to lock'}), 500)
def translation_upload(): default_language = guess_default_language() if default_language: form = UploadForm(language = default_language) else: form = UploadForm() if form.validate_on_submit(): errors = False app_url = form.url.data try: translation_url, original_messages, metadata = extract_local_translations_url(app_url) except Exception as e: traceback.print_exc() form.url.errors = [unicode(e)] errors = True xml_contents = form.opensocial_xml.data.read() if isinstance(xml_contents, str): xml_contents = unicode(xml_contents, 'utf8') try: translated_messages, metadata = extract_messages_from_translation(translation_url, xml_contents) except Exception as e: traceback.print_exc() form.opensocial_xml.errors = [unicode(e)] errors = True if not errors: language = form.language.data target = form.target.data add_full_translation_to_app(current_golab_user().email, app_url, translation_url, metadata, language, target, translated_messages, original_messages, from_developer = False) from appcomposer.translator.tasks import synchronize_apps_cache_wrapper synchronize_apps_cache_wrapper.delay("upload") flash("Contents successfully added") return render_template('translator/translations_upload.html', form=form)
def api_translate(language, target): if language == 'en_ALL': return "error: select other language (not English)" app_url = request.args.get('app_url') errors = [] if not app_url: errors.append("'app_url' argument missing") if not language: errors.append("'lang' argument missing") if not target: errors.append("'target' argument missing") if errors: return '; '.join(errors), 400 translation_url, original_messages, metadata = extract_local_translations_url(app_url) translation = {} stored_translations, from_developer, automatic = retrieve_stored(translation_url, language, target) suggestions = retrieve_suggestions(original_messages, language, target, stored_translations) for key, original_message_pack in original_messages.iteritems(): # We still store the message itself (useful for other things, such as storing and maintaining it # in MongoDB contacted by Shindig). However, we do not display these messages to the final user if not original_message_pack['same_tool']: continue value = original_message_pack['text'] stored = stored_translations.get(key, {}) current_suggestions = list(suggestions.get(key, [])) current_target = stored.get('value') if from_developer: can_edit = not stored.get('from_developer', True) else: can_edit = True if value: translation[key] = { 'source' : value, 'target' : current_target, 'from_default' : stored.get('from_default', False), 'suggestions' : current_suggestions, 'can_edit' : can_edit, 'format': original_message_pack.get('format', 'plain'), } app_thumb = None name = None for repo_app in db.session.query(RepositoryApp).filter_by(url = app_url).all(): if repo_app.name is not None: name = repo_app.name if repo_app.app_thumb is not None: app_thumb = repo_app.app_thumb if name and app_thumb: break update_user_status(language, target, app_url, current_golab_user()) users_status = get_user_status(language, target, app_url, current_golab_user()) response = { 'url' : app_url, 'app_thumb' : app_thumb, 'name' : name, 'translation' : translation, 'modificationDate': users_status['modificationDate'], 'modificationDateByOther': users_status['modificationDateByOther'], 'automatic': automatic and not from_developer, 'preview': automatic, } if False: response = json.dumps(response, indent = 4) return "<html><body>%s</body></html>" % response return jsonify(**response)
def index(): applications = db.session.query(EmbedApplication).filter_by(owner = current_golab_user()).order_by(EmbedApplication.last_update).all() return render_template("embed/index.html", applications = applications)
def api_translate(language, target): app_url = request.args.get('app_url') errors = [] if not app_url: errors.append("'app_url' argument missing") if not language: errors.append("'lang' argument missing") if not target: errors.append("'target' argument missing") if errors: return '; '.join(errors), 400 translation_url, original_messages, metadata = extract_local_translations_url(app_url) translation = {} stored_translations, from_developer, automatic = retrieve_stored(translation_url, language, target) suggestions = retrieve_suggestions(original_messages, language, target, stored_translations) for key, original_message_pack in original_messages.iteritems(): value = original_message_pack['text'] stored = stored_translations.get(key, {}) current_suggestions = list(suggestions.get(key, [])) current_target = stored.get('value') if from_developer: can_edit = not stored.get('from_developer', True) else: can_edit = True translation[key] = { 'source' : value, 'target' : current_target, 'from_default' : stored.get('from_default', False), 'suggestions' : current_suggestions, 'can_edit' : can_edit } app_thumb = None name = None for repo_app in db.session.query(RepositoryApp).filter_by(url = app_url).all(): if repo_app.name is not None: name = repo_app.name if repo_app.app_thumb is not None: app_thumb = repo_app.app_thumb if name and app_thumb: break update_user_status(language, target, app_url, current_golab_user()) users_status = get_user_status(language, target, app_url, current_golab_user()) response = { 'url' : app_url, 'app_thumb' : app_thumb, 'name' : name, 'translation' : translation, 'modificationDate': users_status['modificationDate'], 'modificationDateByOther': users_status['modificationDateByOther'], 'automatic': automatic and not from_developer } if False: response = json.dumps(response, indent = 4) return "<html><body>%s</body></html>" % response return jsonify(**response)
def api_translate(language, target): if language == 'en_ALL': return "error: select other language (not English)" app_url = request.args.get('app_url') errors = [] if not app_url: errors.append("'app_url' argument missing") if not language: errors.append("'lang' argument missing") if not target: errors.append("'target' argument missing") if errors: return '; '.join(errors), 400 translation_url, original_messages, metadata = extract_local_translations_url( app_url) translation = {} stored_translations, from_developer, automatic = retrieve_stored( translation_url, language, target) suggestions = retrieve_suggestions(original_messages, language, target, stored_translations) for key, original_message_pack in original_messages.iteritems(): # We still store the message itself (useful for other things, such as storing and maintaining it # in MongoDB contacted by Shindig). However, we do not display these messages to the final user if not original_message_pack['same_tool']: continue value = original_message_pack['text'] stored = stored_translations.get(key, {}) current_suggestions = list(suggestions.get(key, [])) current_target = stored.get('value') if from_developer: can_edit = not stored.get('from_developer', True) else: can_edit = True if value: translation[key] = { 'source': value, 'target': current_target, 'from_default': stored.get('from_default', False), 'suggestions': current_suggestions, 'can_edit': can_edit, 'format': original_message_pack.get('format', 'plain'), } app_thumb = None name = None for repo_app in db.session.query(RepositoryApp).filter_by( url=app_url).all(): if repo_app.name is not None: name = repo_app.name if repo_app.app_thumb is not None: app_thumb = repo_app.app_thumb if name and app_thumb: break update_user_status(language, target, app_url, current_golab_user()) users_status = get_user_status(language, target, app_url, current_golab_user()) response = { 'url': app_url, 'app_thumb': app_thumb, 'name': name, 'translation': translation, 'modificationDate': users_status['modificationDate'], 'modificationDateByOther': users_status['modificationDateByOther'], 'automatic': automatic and not from_developer, 'preview': automatic, } if False: response = json.dumps(response, indent=4) return "<html><body>%s</body></html>" % response return jsonify(**response)
def check_modifications(language, target): """ Retrieves the last modification date and the active users. """ app_url = request.values.get('app_url') update_user_status(language = language, target = target, app_url = app_url, user = current_golab_user()) data = get_user_status(language = language, target = target, app_url = app_url, user = current_golab_user()) # data = { # "modificationDate": "2015-07-07T23:20:08Z", # "modificationDateByOther": "2015-07-07T23:20:08Z", # "time_now": "2015/12/01T20:83:23Z", # 'collaborators': [ # { # 'name': 'Whoever', # 'md5': 'thisisafakemd5' # } # ] # } # return jsonify(**data)
def create(): form = ApplicationForm() if form.validate_on_submit(): form_scale = _get_scale_value(form) application = EmbedApplication(url = form.url.data, name = form.name.data, owner = current_golab_user(), height=form.height.data, scale=form_scale) db.session.add(application) try: db.session.commit() except Exception as e: traceback.print_exc() return render_template("embed/error.html", message = gettext("There was an error creating an application")), 500 else: return redirect(url_for('.edit', identifier=application.identifier)) return render_template("embed/create.html", form=form, header_message=gettext("Add a web"))
def index(): applications = db.session.query(EmbedApplication).filter_by( owner=current_golab_user()).order_by( EmbedApplication.last_update).all() return render_template("embed/index.html", applications=applications)