def set_default_language(request, lang_code, global_set=False): """ global_set has different meanings for different users. For students, it means their personal default language For teachers, it means their personal default language For django users, it means the server language. """ # Get lang packs directly, to force reloading, as they may have changed. lang_packs = get_installed_language_packs(force=True).keys() lang_code = select_best_available_language( lang_code, available_codes=lang_packs ) # Make sure to reload available languages; output is in django_lang format if lang_code != request.session.get("default_language"): logging.debug("setting session language to %s" % lang_code) request.session["default_language"] = lang_code if global_set: if request.is_django_user and lang_code != Settings.get( "default_language"): logging.debug("setting server default language to %s" % lang_code) Settings.set("default_language", lang_code) elif not request.is_django_user and request.is_logged_in and lang_code != request.session[ "facility_user"].default_language: logging.debug("setting user default language to %s" % lang_code) request.session["facility_user"].default_language = lang_code request.session["facility_user"].save() set_request_language(request, lang_code)
def start_subtitle_download(request): update_set = simplejson.loads(request.raw_post_data or "{}").get("update_set", "existing") language = simplejson.loads(request.raw_post_data or "{}").get("language", "") # Set subtitle language Settings.set("subtitle_language", language) # Get the json file with all srts request_url = "http://%s/static/data/subtitles/languages/%s_available_srts.json" % (settings.CENTRAL_SERVER_HOST, language) try: r = requests.get(request_url) r.raise_for_status() # will return none if 200, otherwise will raise HTTP error available_srts = set((r.json)["srt_files"]) except ConnectionError: return JsonResponse({"error": "The central server is currently offline."}, status=500) except HTTPError: return JsonResponse({"error": "No subtitles available on central server for language code: %s; aborting." % language}, status=500) if update_set == "existing": videofiles = VideoFile.objects.filter(subtitles_downloaded=False, subtitle_download_in_progress=False) else: videofiles = VideoFile.objects.filter(subtitle_download_in_progress=False) queue_count = 0 for chunk in break_into_chunks(available_srts): queue_count += videofiles.filter(youtube_id__in=chunk).update(flagged_for_subtitle_download=True, subtitles_downloaded=False) if queue_count == 0: return JsonResponse({"info": "There aren't any subtitles available in this language for your currently downloaded videos."}, status=200) force_job("subtitledownload", "Download Subtitles") return JsonResponse({})
def process_request(self, request): """ Process requests to set language, redirect to the same URL to continue processing without leaving the "set" in the browser history. """ # Set the set of available languages if "language_choices" not in request.session: request.session["language_choices"] = list(LanguagePack.objects.all()) request.session["language_codes"] = [convert_language_code_format(lp.code) for lp in request.session["language_choices"]] # Set the current language, and redirect (to clean browser history) if request.is_admin and request.GET.get("set_default_language"): Settings.set("default_language", request.GET["set_default_language"]) request.session["default_language"] = request.GET["set_default_language"] logging.debug("setting default language to %s" % request.GET["set_default_language"]) request.session["django_language"] = request.GET["set_default_language"] logging.debug("setting session language to %s" % request.session["django_language"]) return HttpResponseRedirect(request.path) elif request.GET.get("set_language"): request.session["django_language"] = request.GET["set_language"] logging.debug("setting session language to %s" % request.session["django_language"]) return HttpResponseRedirect(request.path) # Process the current language if "default_language" not in request.session: request.session["default_language"] = Settings.get("default_language") or settings.LANGUAGE_CODE if "django_language" not in request.session: request.session["django_language"] = request.session["default_language"] logging.debug("setting session language to %s" % request.session["django_language"]) request.language = request.session["django_language"]
def set_default_language(request, lang_code, global_set=False): """ global_set has different meanings for different users. For students, it means their personal default language For teachers, it means their personal default language For django users, it means the server language. """ # Get lang packs directly, to force reloading, as they may have changed. lang_packs = get_installed_language_packs(force=True).keys() lang_code = select_best_available_language(lang_code, available_codes=lang_packs) # Make sure to reload available languages; output is in django_lang format if lang_code != request.session.get("default_language"): logging.debug("setting session language to %s" % lang_code) request.session["default_language"] = lang_code if global_set: if request.is_django_user and lang_code != Settings.get("default_language"): logging.debug("setting server default language to %s" % lang_code) Settings.set("default_language", lang_code) elif not request.is_django_user and request.is_logged_in and lang_code != request.session["facility_user"].default_language: logging.debug("setting user default language to %s" % lang_code) request.session["facility_user"].default_language = lang_code request.session["facility_user"].save() set_request_language(request, lang_code)
def start_subtitle_download(request): new_only = simplejson.loads(request.raw_post_data or "{}").get("new_only", False) language = simplejson.loads(request.raw_post_data or "{}").get("language", "") language_list = topicdata.LANGUAGE_LIST current_language = Settings.get("subtitle_language") new_only = new_only and (current_language == language) if language in language_list: Settings.set("subtitle_language", language) else: return JsonResponse( { "error": "This language is not currently supported - please update the language list" }, status=500) if new_only: videofiles = VideoFile.objects.filter(Q(percent_complete=100) | Q(flagged_for_download=True), subtitles_downloaded=False) else: videofiles = VideoFile.objects.filter( Q(percent_complete=100) | Q(flagged_for_download=True)) for videofile in videofiles: videofile.cancel_download = False if videofile.subtitle_download_in_progress: continue videofile.flagged_for_subtitle_download = True if not new_only: videofile.subtitles_downloaded = False videofile.save() force_job("subtitledownload", "Download Subtitles") return JsonResponse({})
def setUp_fake_device(self): """ Fake the install process, to (quickly) make a key and set up the own_device() """ # Could be a fixture, but safer to simply hard-code. Settings.set("public_key", u'MIIBCgKCAQEAlbIPLnQH2dORFBK8i9x7/3E0DR571S01aP7M0TJD8vJf8OrgL8pnru3o2Jaoni1XasCZgizvM4GNImk9geqPP/sFkj0cf/MXSLr1VDKo1SoflST9yTbOi7tzVuhTeL4P3LJL6PO6iNiWkjAdqp9QX3mE/DHh/Q40G68meRw6dPDI4z8pyUcshOpxAHTSh2YQ39LJAxP7YS26yjDX/+9UNhetFxemMrBZO0UKtJYggPYRlMZmlTZLBU4ODMmK6MT26fB4DC4ChA3BD4OFiGDHI/iSy+iYJlcWaldbZtc+YfZlGhvsLnJlrp4WYykJSH5qeBuI7nZLWjYWWvMrDepXowIDAQAB') Settings.set("private_key", u'-----BEGIN RSA PRIVATE KEY-----\nMIIEqQIBAAKCAQEAlbIPLnQH2dORFBK8i9x7/3E0DR571S01aP7M0TJD8vJf8Org\nL8pnru3o2Jaoni1XasCZgizvM4GNImk9geqPP/sFkj0cf/MXSLr1VDKo1SoflST9\nyTbOi7tzVuhTeL4P3LJL6PO6iNiWkjAdqp9QX3mE/DHh/Q40G68meRw6dPDI4z8p\nyUcshOpxAHTSh2YQ39LJAxP7YS26yjDX/+9UNhetFxemMrBZO0UKtJYggPYRlMZm\nlTZLBU4ODMmK6MT26fB4DC4ChA3BD4OFiGDHI/iSy+iYJlcWaldbZtc+YfZlGhvs\nLnJlrp4WYykJSH5qeBuI7nZLWjYWWvMrDepXowIDAQABAoIBAD8S/a6XGU/BA1ov\n4t4TkvO44TO96nOSTv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F+fY9jUculveAQpVWj03nYBtBdTh2EWcvfoSjjfl\nmpzrraojpQlUhrbUf5MU1fD9+i6swrCCvfjXITG3c1bkkB5AaQW7NiPHvDRMuDpc\nHIQ+vqzdn4iUlt7KB5ChpnZMmgiOdCBM0vQsZlVCbp0ZNLqVYhFASQnWl6V9\n-----END RSA PRIVATE KEY-----\n') Device.initialize_own_device()
def load_keys(): private_key_string = Settings.get("private_key") if private_key_string: public_key_string = Settings.get("public_key") keys["private"] = rsa.PrivateKey.load_pkcs1(private_key_string) keys["public"] = deserialize_public_key(public_key_string) else: reset_keys()
def process_request(self, request): if request.user.is_authenticated() and "django_language" not in request.session: request.session["django_language"] = Settings.get("default_language") or settings.LANGUAGE_CODE if request.GET.get("set_language"): request.session["django_language"] = request.GET.get("set_language") return HttpResponseRedirect(request.path) if request.is_admin and request.GET.get("set_default_language"): Settings.set("default_language", request.GET.get("set_default_language")) return HttpResponseRedirect(request.path)
def load_keys(): private_key_string = Settings.get("private_key") public_key_string = Settings.get("public_key") if private_key_string and public_key_string: sys.modules[__name__]._own_key = Key( public_key_string=public_key_string, private_key_string=private_key_string) else: reset_keys()
def process_request(self, request): if "django_language" not in request.session: request.session["django_language"] = Settings.get("default_language") or "en" if request.GET.get("set_language"): request.session["django_language"] = request.GET.get("set_language") return HttpResponseRedirect(request.path) if request.is_admin and request.GET.get("set_default_language"): Settings.set("default_language", request.GET.get("set_default_language")) return HttpResponseRedirect(request.path)
def reset_keys(): try: (public_key, private_key) = rsa.newkeys(2048, poolsize=4) except: (public_key, private_key) = rsa.newkeys(2048) Settings.set("private_key", private_key.save_pkcs1()) Settings.set("public_key", serialize_public_key(public_key)) keys["private"] = private_key keys["public"] = public_key
def initialize_facility(facility_name=None): facility_name = facility_name or settings.INSTALL_FACILITY_NAME # Finally, install a facility--would help users get off the ground if facility_name: facility = get_object_or_None(Facility, name=facility_name) if not facility: facility = Facility(name=facility_name) facility.save() Settings.set("default_facility", facility.id)
def get_facility_from_request(request): if "facility" in request.GET: facility = get_object_or_None(Facility, pk=request.GET["facility"]) if "set_default" in request.GET and request.is_admin and facility: Settings.set("default_facility", facility.id) elif "facility_user" in request.session: facility = request.session["facility_user"].facility elif Facility.objects.count() == 1: facility = Facility.objects.all()[0] else: facility = get_object_or_None(Facility, pk=Settings.get("default_facility")) return facility
def process_request(self, request): if request.user.is_authenticated( ) and "django_language" not in request.session: request.session["django_language"] = Settings.get( "default_language") or settings.LANGUAGE_CODE if request.GET.get("set_language"): request.session["django_language"] = request.GET.get( "set_language") return HttpResponseRedirect(request.path) if request.is_admin and request.GET.get("set_default_language"): Settings.set("default_language", request.GET.get("set_default_language")) return HttpResponseRedirect(request.path)
def wrapper_fn(request, *args, **kwargs): if "facility_id" in kwargs: facility = get_object_or_None(Facility, pk=kwargs["facility_id"]) elif "facility" in request.GET: facility = get_object_or_None(Facility, pk=request.GET["facility"]) if "set_default" in request.GET and request.is_admin and facility: Settings.set("default_facility", facility.id) elif "facility_user" in request.session: facility = request.session["facility_user"].facility elif Facility.objects.count() == 1: facility = Facility.objects.all()[0] else: facility = get_object_or_None(Facility, pk=Settings.get("default_facility")) return handler(request, *args, facility=facility, **kwargs)
def setUp_fake_device(self): """ Fake the install process, to (quickly) make a key and set up the own_device() """ # Could be a fixture, but safer to simply hard-code. Settings.set( "public_key", u'MIIBCgKCAQEAlbIPLnQH2dORFBK8i9x7/3E0DR571S01aP7M0TJD8vJf8OrgL8pnru3o2Jaoni1XasCZgizvM4GNImk9geqPP/sFkj0cf/MXSLr1VDKo1SoflST9yTbOi7tzVuhTeL4P3LJL6PO6iNiWkjAdqp9QX3mE/DHh/Q40G68meRw6dPDI4z8pyUcshOpxAHTSh2YQ39LJAxP7YS26yjDX/+9UNhetFxemMrBZO0UKtJYggPYRlMZmlTZLBU4ODMmK6MT26fB4DC4ChA3BD4OFiGDHI/iSy+iYJlcWaldbZtc+YfZlGhvsLnJlrp4WYykJSH5qeBuI7nZLWjYWWvMrDepXowIDAQAB' ) Settings.set( "private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n3sjB3ptbNhk1yrkzhFbd6ZG4fsL5Mb0EurAFtQKBiFCUVc2GdQHfGsuR9DS3tnyx\n/GEI9NNIGFJKIQHzfENp4wZPQ8fwBMREmLfwJZyEtSYEi35KXi6FZugb0WuwzzhC\nZ2v+19Y+E+nmNeD4xcSEZFpuTeDtPd1pIDkmf85cBI+Mn88FfvBTHA9YrPgQXnba\nxzoaaSOUCR9Kd1kp5V2IQJtoVytBwPkCeFIDD6kkxuuqZu2Q1gkEgptHkZPjt/rP\nYnuTHNsrVowuNr/u8NkXEC+O9Zg8ub2NcsQzxCpVp4lnaDitFTf/h7Bmm4tvHNx1\n4fX3m1oU51ATXGQXVit8xK+JKU9DN4wLIGgJOwmGLwd5VZ5aIEb2v2vykgzn8l2e\nSQKBiQC7CJVToYSUWnDGwCRsF+fY9jUculveAQpVWj03nYBtBdTh2EWcvfoSjjfl\nmpzrraojpQlUhrbUf5MU1fD9+i6swrCCvfjXITG3c1bkkB5AaQW7NiPHvDRMuDpc\nHIQ+vqzdn4iUlt7KB5ChpnZMmgiOdCBM0vQsZlVCbp0ZNLqVYhFASQnWl6V9\n-----END RSA PRIVATE KEY-----\n' ) Device.initialize_own_device()
def languages(request): default_language = Settings.get("default_language") or "en" return { "DEFAULT_LANGUAGE": default_language, "language_choices": LanguagePack.objects.all(), "current_language": request.session.get("django_language", default_language), }
def setup_server_if_needed(self): """Run the setup command, if necessary.""" # Now, validate the server. try: if Settings.get("private_key") and Device.objects.count(): # The only success case pass elif not Device.objects.count(): # Nothing we can do to recover raise CommandError( "You are screwed, buddy--you went through setup but you have no devices defined! Call for help!" ) else: # Force hitting recovery code, by raising a generic error # that gets us to the "except" clause raise DatabaseError except DatabaseError: self.stdout.write( "Setting up KA Lite; this may take a few minutes; please wait!\n" ) call_command("setup", interactive=False) # show output to the user
def process_request(self, request): """ Process requests to set language, redirect to the same URL to continue processing without leaving the "set" in the browser history. """ # Set the set of available languages if "language_choices" not in request.session: request.session["language_choices"] = list( LanguagePack.objects.all()) request.session["language_codes"] = [ convert_language_code_format(lp.code) for lp in request.session["language_choices"] ] # Set the current language, and redirect (to clean browser history) if request.is_admin and request.GET.get("set_default_language"): Settings.set("default_language", request.GET["set_default_language"]) request.session["default_language"] = request.GET[ "set_default_language"] logging.debug("setting default language to %s" % request.GET["set_default_language"]) request.session["django_language"] = request.GET[ "set_default_language"] logging.debug("setting session language to %s" % request.session["django_language"]) return HttpResponseRedirect(request.path) elif request.GET.get("set_language"): request.session["django_language"] = request.GET["set_language"] logging.debug("setting session language to %s" % request.session["django_language"]) return HttpResponseRedirect(request.path) # Process the current language if "default_language" not in request.session: request.session["default_language"] = Settings.get( "default_language") or settings.LANGUAGE_CODE if "django_language" not in request.session: request.session["django_language"] = request.session[ "default_language"] logging.debug("setting session language to %s" % request.session["django_language"]) request.language = request.session["django_language"]
def handle(self, *args, **options): if Settings.get("private_key"): self.stderr.write("Error: This device already has an encryption key generated for it; aborting.\n") return self.stdout.write("Generating 2048-bit RSA encryption key (may take a few minutes; please wait)...\n") reset_keys() self.stdout.write("Done!\n")
def wrapper_fn(request, *args, **kwargs): if kwargs.get("facility_id",None): facility = get_object_or_None(pk=kwargs["facility_id"]) elif "facility" in request.GET: facility = get_object_or_None(Facility, pk=request.GET["facility"]) if "set_default" in request.GET and request.is_admin and facility: Settings.set("default_facility", facility.id) elif "facility_user" in request.session: facility = request.session["facility_user"].facility elif settings.CENTRAL_SERVER: # following options are distributed-only facility = None elif Facility.objects.count() == 1: facility = Facility.objects.all()[0] else: facility = get_object_or_None(Facility, pk=Settings.get("default_facility")) return handler(request, *args, facility=facility, **kwargs)
def wrapper_fn(request, *args, **kwargs): if kwargs.get("facility_id", None): facility = get_object_or_None(Facility, pk=kwargs["facility_id"]) elif "facility" in request.GET: facility = get_object_or_None(Facility, pk=request.GET["facility"]) if "set_default" in request.GET and request.is_admin and facility: Settings.set("default_facility", facility.id) elif "facility_user" in request.session: facility = request.session["facility_user"].facility elif settings.CENTRAL_SERVER: # following options are distributed-only facility = None elif Facility.objects.count() == 1: facility = Facility.objects.all()[0] else: facility = get_object_or_None(Facility, pk=Settings.get("default_facility")) return handler(request, *args, facility=facility, **kwargs)
def update(request): call_command("videoscan") # Could potentially be very slow, blocking request. force_job("videodownload", "Download Videos") force_job("subtitledownload", "Download Subtitles") default_language = Settings.get("subtitle_language") or "en" device = Device.get_own_device() zone = device.get_zone() context = { "default_language": default_language, "registered": Settings.get("registered"), "zone_id": zone.id if zone else None, "device_id": device.id, "video_count": VideoFile.objects.filter(percent_complete=100).count(), } return context
def eos_delete(self) -> str: config = Settings.get_solo() uri = f'{config.eosgate}/form' payload = { 'form': self.uid, } r = requests.delete(uri, json=payload) return r.content.decode()
def homepage(request): topics = filter(lambda node: node["kind"] == "Topic" and not node["hide"], topicdata.TOPICS["children"]) context = { "title": "Home", "topics": topics, "registered": Settings.get("registered"), } return context
def start_subtitle_download(request): update_set = simplejson.loads(request.raw_post_data or "{}").get("update_set", "existing") language = simplejson.loads(request.raw_post_data or "{}").get("language", "") language_list = topicdata.LANGUAGE_LIST language_lookup = topicdata.LANGUAGE_LOOKUP # Reset the language current_language = Settings.get("subtitle_language") if language in language_list: Settings.set("subtitle_language", language) else: return JsonResponse({"error": "This language is not currently supported - please update the language list"}, status=500) language_name = language_lookup.get(language) # Get the json file with all srts request_url = "http://%s/static/data/subtitles/languages/%s_available_srts.json" % (settings.CENTRAL_SERVER_HOST, language) try: r = requests.get(request_url) r.raise_for_status() # will return none if 200, otherwise will raise HTTP error available_srts = set((r.json)["srt_files"]) except ConnectionError: return JsonResponse({"error": "The central server is currently offline."}, status=500) except HTTPError: return JsonResponse({"error": "No subtitles available on central server for %s (language code: %s); aborting." % (language_name, language)}, status=500) if update_set == "existing": videofiles = VideoFile.objects.filter(Q(percent_complete=100) | Q(flagged_for_download=True), subtitles_downloaded=False, youtube_id__in=available_srts) else: videofiles = VideoFile.objects.filter(Q(percent_complete=100) | Q(flagged_for_download=True), youtube_id__in=available_srts) if not videofiles: return JsonResponse({"info": "There aren't any subtitles available in %s (language code: %s) for your current videos." % (language_name, language)}, status=200) else: for videofile in videofiles: videofile.cancel_download = False if videofile.subtitle_download_in_progress: continue videofile.flagged_for_subtitle_download = True if update_set == "all": videofile.subtitles_downloaded = False videofile.save() force_job("subtitledownload", "Download Subtitles") return JsonResponse({})
def handle(self, *args, **options): # Eliminate irrelevant settings for opt in BaseCommand.option_list: del options[opt.dest] # Parse the crappy way that runcherrypy takes args, # or the host/port for arg in args: if "=" in arg: (key, val) = arg.split("=") options[key] = val elif ":" in arg: (options["host"], options["port"]) = arg.split(":") elif isnumeric(arg): options["port"] = arg else: raise CommandError("Unexpected argument format: %s" % arg) # Now, validate the server. try: if Settings.get("private_key") and Device.objects.count(): # The only success case pass elif not Device.objects.count(): # Nothing we can do to recover raise CommandError( "You are screwed, buddy--you went through setup but you have no devices defined! Call for help!" ) else: # Force hitting recovery code, by raising a generic error # that gets us to the "except" clause raise DatabaseError except DatabaseError: self.stdout.write( "Setting up KA Lite; this may take a few minutes; please wait!\n" ) call_command("setup", interactive=False) # show output to the user #out = call_command_with_output("setup", interactive=False) #if out[1] or out[2]: # # Failed; report and exit # self.stderr.write(out[1]) # raise CommandError("Failed to setup/recover.") # Now call the proper command if options["run_in_proc"]: call_command("runserver", "%s:%s" % (options["host"], options["port"])) else: call_command( "runcherrypyserver", *["%s=%s" % (key, val) for key, val in options.iteritems()])
def wrapper_fn(request, *args, **kwargs): if kwargs.get("facility_id", None): # avoid using blank # Facility passed in directly facility = get_object_or_None(Facility, pk=kwargs["facility_id"]) elif "facility" in request.GET: # Facility from querystring facility = get_object_or_None(Facility, pk=request.GET["facility"]) if "set_default" in request.GET and request.is_admin and facility: Settings.set("default_facility", facility.id) elif settings.CENTRAL_SERVER: # following options are distributed-only facility = None elif "facility_user" in request.session: # Facility from currently logged-in facility user facility = request.session["facility_user"].facility elif request.session["facility_count"] == 1: # There's only one facility facility = Facility.objects.all()[0] elif request.session["facility_count"] > 0: if Settings.get("default_facility"): # There are multiple facilities--try to grab the default facility = get_object_or_None( Facility, pk=Settings.get("default_facility")) elif Facility.objects.filter( Q(signed_by__isnull=True) | Q(signed_by=Device.get_own_device())).count() == 1: # Default to a locally created facility (if there are multiple, and none are specified) facility = Facility.objects.filter( Q(signed_by__isnull=True) | Q(signed_by=Device.get_own_device()))[0] else: facility = None else: # There's nothing; don't bother even hitting the DB facility = None return handler(request, *args, facility=facility, **kwargs)
def languages(request): default_language = Settings.get("default_language") or "en" return { "DEFAULT_LANGUAGE": default_language, "language_choices": list(LanguagePack.objects.all()), "current_language": request.session.get("django_language", default_language), }
def update_context(request): device = Device.get_own_device() zone = device.get_zone() context = { "registered": Settings.get("registered"), "zone_id": zone.id if zone else None, "device_id": device.id, } return context
def handle(self, *args, **options): language = Settings.get("subtitle_language") failed_video_ids = [] # stored to avoid requerying failures. while True: # loop until the method is aborted if VideoFile.objects.filter( subtitle_download_in_progress=True).count() > 4: self.stderr.write( "Maximum downloads are in progress; aborting.\n") return videos = VideoFile.objects.filter( flagged_for_subtitle_download=True, subtitle_download_in_progress=False).exclude( youtube_id__in=failed_video_ids) if videos.count() == 0: self.stdout.write("Nothing to download; aborting.\n") break # Grab a video and mark it as downloading video = videos[0] video.subtitle_download_in_progress = True video.save() self.stdout.write("Downloading subtitles for video '%s'... " % video.youtube_id) self.stdout.flush() try: download_subtitles(video.youtube_id, language) video.subtitles_downloaded = True video.subtitle_download_in_progress = False video.flagged_for_subtitle_download = False video.save() self.stdout.write("Download is complete!\n") except NoSubs as e: # Not all videos have subtitles, so this error # is in most ways a success. video.flagged_for_subtitle_download = False video.subtitle_download_in_progress = False video.subtitles_downloaded = True self.stdout.write("\n") video.save() self.stdout.write("No subtitles available\n") except Exception as e: self.stderr.write("Error in downloading subtitles: %s\n" % e) video.subtitle_download_in_progress = False video.flagged_for_subtitle_download = False video.save() # Skip this video and move on. failed_video_ids.append(video.youtube_id) continue
def process_request(self, request): """ Process requests to set language, redirect to the same URL to continue processing without leaving the "set" in the browser history. """ if "language_choices" not in request.session: # Set the set of available languages request.session["language_choices"] = list(LanguagePack.objects.all()) if "set_default_language" in request.GET: # Set the current server default language, and redirect (to clean browser history) if not request.is_admin: raise PermissionDenied(_("You don't have permissions to set the server's default language.")) lang_code = lcode_to_django_lang(request.GET["set_default_language"]) self.set_language(request, lang_code) Settings.set("default_language", lang_code) logging.debug("setting default language to %s" % lang_code) redirect_url = request.get_full_path().replace("set_default_language="+request.GET["set_default_language"], "") return HttpResponseRedirect(redirect_url) elif "set_language" in request.GET: # Set the current user's session language, and redirect (to clean browser history) lang_code = lcode_to_django_lang(request.GET["set_language"]) self.set_language(request, lang_code) logging.debug("setting session language to %s" % lang_code) redirect_url = request.get_full_path().replace("set_language="+request.GET["set_language"], "") return HttpResponseRedirect(redirect_url) # Set this request's language based on the listed priority cur_lang = lcode_to_django_lang( \ request.GET.get("lang") \ or request.session.get("django_language") \ or request.session.get("default_language") \ or settings.LANGUAGE_CODE \ ) self.set_language(request, cur_lang)
def wrapper_fn(request, *args, **kwargs): if not request.is_admin and Facility.objects.count() == 0: messages.warning(request, mark_safe( "Please <a href='%s?next=%s'>login</a> with the account you created while running the installation script, \ to complete the setup." % (reverse("login"), reverse("register_public_key")))) if request.is_admin: if not Settings.get("registered") and SyncClient().test_connection() == "success": messages.warning(request, mark_safe("Please <a href='%s'>follow the directions to register your device</a>, so that it can synchronize with the central server." % reverse("register_public_key"))) elif Facility.objects.count() == 0: messages.warning(request, mark_safe("Please <a href='%s'>create a facility</a> now. Users will not be able to sign up for accounts until you have made a facility." % reverse("add_facility"))) return handler(request, *args, **kwargs)
def handle(self, *args, **options): if Settings.get("private_key"): self.stderr.write( "Error: This device already has an encryption key generated for it; aborting.\n" ) return self.stdout.write( "Generating 2048-bit RSA encryption key (may take a few minutes; please wait)...\n" ) reset_keys() self.stdout.write("Done!\n")
def set_password(self, raw_password=None, hashed_password=None): """Set a password with the raw password string, or the pre-hashed password.""" assert hashed_password is None or settings.DEBUG, "Only use hashed_password in debug mode." assert raw_password is not None or hashed_password is not None, "Must be passing in raw or hashed password" assert not (raw_password is not None and hashed_password is not None), "Must be specifying only one--not both." if hashed_password: self.password = hashed_password else: self.password = crypt(raw_password, iterations=Settings.get("password_hash_iterations", 2000 if self.is_teacher else 1000))
def status(request): """In order to promote (efficient) caching on (low-powered) distributed devices, we do not include ANY user data in our templates. Instead, an AJAX request is made to download user data, and javascript used to update the page. This view is the view providing the json blob of user information, for each page view on the distributed server. Besides basic user data, we also provide access to the Django message system through this API, again to promote caching by excluding any dynamic information from the server-generated templates. """ # Build a list of messages to pass to the user. # Iterating over the messages removes them from the # session storage, thus they only appear once. message_dicts = [] for message in get_messages(request): # Make sure to escape strings not marked as safe. # Note: this duplicates a bit of Django template logic. msg_txt = message.message if not (isinstance(msg_txt, SafeString) or isinstance(msg_txt, SafeUnicode)): msg_txt = cgi.escape(str(msg_txt)) message_dicts.append({ "tags": message.tags, "text": msg_txt, }) # Default data data = { "is_logged_in": request.is_logged_in, "registered": bool(Settings.get("registered")), "is_admin": request.is_admin, "is_django_user": request.is_django_user, "points": 0, "messages": message_dicts, } # Override properties using facility data if "facility_user" in request.session: user = request.session["facility_user"] data["is_logged_in"] = True data["username"] = user.get_name() data["points"] = VideoLog.get_points_for_user( user) + ExerciseLog.get_points_for_user(user) # Override data using django data if request.user.is_authenticated(): data["is_logged_in"] = True data["username"] = request.user.username return JsonResponse(data)
def unregister_distributed_server(): """ All local steps necessary for unregistering a server with a central server. Note that the remote steps (central-server-side) are NOT done. * Login as Django admin, go to admin page, select "devices", find your device and delete. """ if settings.CENTRAL_SERVER: raise CommandError("'Unregister' does not make sense for a central server. Aborting!") own_device = Device.get_own_device() # Delete zone info DeviceZone.objects.filter(device=own_device).delete() Zone.objects.all().delete() # Delete registered info Settings.delete("registered") # setting to False doesn't work. # Delete central server Device.objects.filter(devicemetadata__is_trusted=True).delete()
def wrapper_fn(request, *args, **kwargs): if kwargs.get("facility_id", None): # avoid using blank # Facility passed in directly facility = get_object_or_None(Facility, pk=kwargs["facility_id"]) del kwargs["facility_id"] elif "facility" in request.GET: # Facility from querystring facility = get_object_or_None(Facility, pk=request.GET["facility"]) if "set_default" in request.GET and request.is_admin and facility: Settings.set("default_facility", facility.id) elif settings.CENTRAL_SERVER: # following options are distributed-only facility = None elif "facility_user" in request.session: # Facility from currently logged-in facility user facility = request.session["facility_user"].facility elif request.session["facility_count"] == 1: # There's only one facility facility = Facility.objects.all()[0] elif request.session["facility_count"] > 0: if Settings.get("default_facility"): # There are multiple facilities--try to grab the default facility = get_object_or_None(Facility, pk=Settings.get("default_facility")) elif Facility.objects.filter(Q(signed_by__isnull=True) | Q(signed_by=Device.get_own_device())).count() == 1: # Default to a locally created facility (if there are multiple, and none are specified) facility = Facility.objects.filter(Q(signed_by__isnull=True) | Q(signed_by=Device.get_own_device()))[0] else: facility = None else: # There's nothing; don't bother even hitting the DB facility = None return handler(request, *args, facility=facility, **kwargs)
def status(request): """In order to promote (efficient) caching on (low-powered) distributed devices, we do not include ANY user data in our templates. Instead, an AJAX request is made to download user data, and javascript used to update the page. This view is the view providing the json blob of user information, for each page view on the distributed server. Besides basic user data, we also provide access to the Django message system through this API, again to promote caching by excluding any dynamic information from the server-generated templates. """ # Build a list of messages to pass to the user. # Iterating over the messages removes them from the # session storage, thus they only appear once. message_dicts = [] for message in get_messages(request): # Make sure to escape strings not marked as safe. # Note: this duplicates a bit of Django template logic. msg_txt = message.message if not (isinstance(msg_txt, SafeString) or isinstance(msg_txt, SafeUnicode)): msg_txt = cgi.escape(str(msg_txt)) message_dicts.append({ "tags": message.tags, "text": msg_txt, }) # Default data data = { "is_logged_in": request.is_logged_in, "registered": bool(Settings.get("registered")), "is_admin": request.is_admin, "is_django_user": request.is_django_user, "points": 0, "messages": message_dicts, } # Override properties using facility data if "facility_user" in request.session: user = request.session["facility_user"] data["is_logged_in"] = True data["username"] = user.get_name() data["points"] = VideoLog.get_points_for_user(user) + ExerciseLog.get_points_for_user(user) # Override data using django data if request.user.is_authenticated(): data["is_logged_in"] = True data["username"] = request.user.username return JsonResponse(data)
def set_language_data(request): """ Process requests to set language, redirect to the same URL to continue processing without leaving the "set" in the browser history. """ if "set_server_language" in request.GET: # Set the current server default language, and redirect (to clean browser history) if not request.is_admin: raise PermissionDenied( _("You don't have permissions to set the server's default language." )) set_default_language(request, lang_code=request.GET["set_server_language"], global_set=True) # Redirect to the same URL, but without the GET param, # to remove the language setting from the browser history. redirect_url = set_query_params(request.get_full_path(), {"set_server_language": None}) return HttpResponseRedirect(redirect_url) elif "set_user_language" in request.GET: # Set the current user's session language, and redirect (to clean browser history) set_default_language(request, request.GET["set_user_language"], global_set=(request.is_logged_in and not request.is_django_user)) # Redirect to the same URL, but without the GET param, # to remove the language setting from the browser history. redirect_url = set_query_params(request.get_full_path(), {"set_user_language": None}) return HttpResponseRedirect(redirect_url) if not "default_language" in request.session: # default_language has the following priority: # facility user's individual setting # config.Settings object's value # settings' value request.session["default_language"] = select_best_available_language( \ getattr(request.session.get("facility_user"), "default_language", None) \ or Settings.get("default_language") \ or settings.LANGUAGE_CODE ) # Set this request's language based on the listed priority cur_lang = request.GET.get("lang") \ or request.session.get("default_language") set_request_language(request, lang_code=cur_lang)
def handle(self, *args, **options): # Eliminate irrelevant settings for opt in BaseCommand.option_list: del options[opt.dest] # Parse the crappy way that runcherrypy takes args, # or the host/port for arg in args: if "=" in arg: (key,val) = arg.split("=") options[key] = val elif ":" in arg: (options["host"], options["port"]) = arg.split(":") elif isnumeric(arg): options["port"] = arg else: raise CommandError("Unexpected argument format: %s" % arg) # Now, validate the server. try: if Settings.get("private_key") and Device.objects.count(): # The only success case pass elif not Device.objects.count(): # Nothing we can do to recover raise CommandError("You are screwed, buddy--you went through setup but you have no devices defined! Call for help!") else: # Force hitting recovery code, by raising a generic error # that gets us to the "except" clause raise DatabaseError except DatabaseError: self.stdout.write("Setting up KA Lite; this may take a few minutes; please wait!\n") call_command("setup", interactive=False) # show output to the user #out = call_command_with_output("setup", interactive=False) #if out[1] or out[2]: # # Failed; report and exit # self.stderr.write(out[1]) # raise CommandError("Failed to setup/recover.") # Now call the proper command if options["run_in_proc"]: call_command("runserver", "%s:%s" % (options["host"], options["port"])) else: call_command("runcherrypyserver", *["%s=%s" % (key,val) for key, val in options.iteritems()])
def unregister_distributed_server(): """ All local steps necessary for unregistering a server with a central server. Note that the remote steps (central-server-side) are NOT done. * Login as Django admin, go to admin page, select "devices", find your device and delete. """ if settings.CENTRAL_SERVER: raise CommandError( "'Unregister' does not make sense for a central server. Aborting!" ) own_device = Device.get_own_device() # Delete zone info DeviceZone.objects.filter(device=own_device).delete() Zone.objects.all().delete() # Delete registered info Settings.delete("registered") # setting to False doesn't work. # Delete central server Device.objects.filter(devicemetadata__is_trusted=True).delete()
def handle(self, *args, **options): language = Settings.get("subtitle_language") failed_video_ids = [] # stored to avoid requerying failures. while True: # loop until the method is aborted if VideoFile.objects.filter(subtitle_download_in_progress=True).count() > 4: self.stderr.write("Maximum downloads are in progress; aborting.\n") return videos = VideoFile.objects.filter(flagged_for_subtitle_download=True, subtitle_download_in_progress=False).exclude(youtube_id__in=failed_video_ids) if videos.count() == 0: self.stdout.write("Nothing to download; aborting.\n") break # Grab a video and mark it as downloading video = videos[0] video.subtitle_download_in_progress = True video.save() self.stdout.write("Downloading subtitles for video '%s'... " % video.youtube_id) self.stdout.flush() try: download_subtitles(video.youtube_id, language) video.subtitles_downloaded = True video.subtitle_download_in_progress = False video.flagged_for_subtitle_download = False video.save() self.stdout.write("Download is complete!\n") except NoSubs as e: # Not all videos have subtitles, so this error # is in most ways a success. video.flagged_for_subtitle_download = False video.subtitle_download_in_progress = False video.subtitles_downloaded = True self.stdout.write("\n"); video.save() self.stdout.write("No subtitles available\n") except Exception as e: self.stderr.write("Error in downloading subtitles: %s\n" % e) video.subtitle_download_in_progress = False video.flagged_for_subtitle_download = False video.save() # Skip this video and move on. failed_video_ids.append(video.youtube_id) continue
def start_subtitle_download(request): new_only = simplejson.loads(request.raw_post_data or "{}").get("new_only", False) language = simplejson.loads(request.raw_post_data or "{}").get("language", "") language_list = topicdata.LANGUAGE_LIST current_language = Settings.get("subtitle_language") new_only = new_only and (current_language == language) if language in language_list: Settings.set("subtitle_language", language) else: return JsonResponse({"error": "This language is not currently supported - please update the language list"}, status=500) if new_only: videofiles = VideoFile.objects.filter(Q(percent_complete=100) | Q(flagged_for_download=True), subtitles_downloaded=False) else: videofiles = VideoFile.objects.filter(Q(percent_complete=100) | Q(flagged_for_download=True)) for videofile in videofiles: videofile.cancel_download = False if videofile.subtitle_download_in_progress: continue videofile.flagged_for_subtitle_download = True if not new_only: videofile.subtitles_downloaded = False videofile.save() force_job("subtitledownload", "Download Subtitles") return JsonResponse({})
def send_to_eos(self): settings = Settings.get_solo() uri = f'{settings.eosgate}/response' payload = { 'form': self._survey.uid, 'answers': list(self.cleaned_data.values()) } participation = Participation(user=self._user, survey=self._survey) r = requests.post(uri, json=payload) data = r.content.decode() if r.status_code == 200: participation.txid = data participation.save() return r.content.decode()
def update(request): call_command("videoscan") force_job("videodownload", "Download Videos") force_job("subtitledownload", "Download Subtitles") language_lookup = topicdata.LANGUAGE_LOOKUP language_list = topicdata.LANGUAGE_LIST default_language = Settings.get("subtitle_language") or "en" if default_language not in language_list: language_list.append(default_language) languages = [{"id": key, "name": language_lookup[key]} for key in language_list] languages = sorted(languages, key=lambda k: k["name"]) context = { "languages": languages, "default_language": default_language, } return context
def status(request): data = { "is_logged_in": request.is_logged_in, "registered": bool(Settings.get("registered")), "is_admin": request.is_admin, "is_django_user": request.is_django_user, "points": 0, } if "facility_user" in request.session: user = request.session["facility_user"] data["is_logged_in"] = True data["username"] = user.get_name() data["points"] = VideoLog.get_points_for_user(user) + ExerciseLog.get_points_for_user(user) if request.user.is_authenticated(): data["is_logged_in"] = True data["username"] = request.user.username return JsonResponse(data)
def get_responses_curl(self) -> str: """ cURL link to retrieve data in JSON format :return: string """ settings = Settings.get_solo() endpoint = f'{settings.eos_node_uri}/v1/chain/get_table_rows' data = json.dumps({ 'code': settings.eos_account, 'table': 'response', 'scope': self.uid, 'limit': 10000, 'json': True }) return f'curl --request "POST" ' \ f'--url {endpoint} ' \ f'--data \'{data}\''