def test_summary_cron_locked_cron_locked(self): # set locked SettingProperties.set_int('oppia_cron_lock', 1) lock = SettingProperties.get_int('oppia_cron_lock', 0) self.assertEqual(lock, 1) SettingProperties.set_int('oppia_summary_cron_lock', 1) lock = SettingProperties.get_int('oppia_summary_cron_lock', 0) self.assertEqual(lock, 1) call_command('update_summaries', stdout=StringIO()) # check new details on pks # cron is locked so nothing should happen tracker_id = SettingProperties.get_int('last_tracker_pk', 0) self.assertEqual(tracker_id, 0) # unlock SettingProperties.delete_key('oppia_summary_cron_lock') SettingProperties.delete_key('oppia_cron_lock') # check unlocked again lock = SettingProperties.get_int('oppia_summary_cron_lock', 999) self.assertEqual(lock, 999) lock = SettingProperties.get_int('oppia_cron_lock', 999) self.assertEqual(lock, 999)
def test_summary_cron(self): # check lock not set lock = SettingProperties.get_int('oppia_summary_cron_lock', 999) self.assertEqual(lock, 999) update_summaries() # check new details on pks tracker_id = SettingProperties.get_int('last_tracker_pk', 0) self.assertEqual(tracker_id, 1472216) # this id is from the test_tracker data #check unlocked again lock = SettingProperties.get_int('oppia_summary_cron_lock', 999)
def insert_maxuploadsize(apps, schema_editor): current = SettingProperties.get_int(constants.MAX_UPLOAD_SIZE, None) if current is None and hasattr(settings, 'OPPIA_MAX_UPLOAD_SIZE'): settings_prop = SettingProperties() settings_prop.key = constants.MAX_UPLOAD_SIZE settings_prop.int_value = settings.OPPIA_MAX_UPLOAD_SIZE settings_prop.save()
def insert_self_registration(apps, schema_editor): current = SettingProperties.get_int(constants.OPPIA_ALLOW_SELF_REGISTRATION, None) if current is None and hasattr(settings, 'OPPIA_ALLOW_SELF_REGISTRATION'): settings_prop = SettingProperties() settings_prop.key = constants.OPPIA_ALLOW_SELF_REGISTRATION settings_prop.int_value = settings.OPPIA_ALLOW_SELF_REGISTRATION settings_prop.save()
def register(request): self_register = SettingProperties.get_int(constants.OPPIA_ALLOW_SELF_REGISTRATION, settings.OPPIA_ALLOW_SELF_REGISTRATION) if not self_register: raise Http404 if request.method == 'POST': # if form submitted... form = RegisterForm(request.POST) if form.is_valid(): # All validation rules pass # Create new user username = form.cleaned_data.get("username") email = form.cleaned_data.get("email") password = form.cleaned_data.get("password") first_name = form.cleaned_data.get("first_name") last_name = form.cleaned_data.get("last_name") user = User.objects.create_user(username, email, password) user.first_name = first_name user.last_name = last_name user.save() user_profile = UserProfile() user_profile.user = user user_profile.job_title = form.cleaned_data.get("job_title") user_profile.organisation = form.cleaned_data.get("organisation") user_profile.save() u = authenticate(username=username, password=password) if u is not None and u.is_active: login(request, u) return HttpResponseRedirect('thanks/') return HttpResponseRedirect('thanks/') # Redirect after POST else: form = RegisterForm(initial={'next': filter_redirect(request.GET), }) return render(request, 'oppia/form.html', {'form': form, 'title': _(u'Register'), })
def test_delete_int(self): key = "testkey1" value = 123 SettingProperties.set_int(key, value) SettingProperties.delete_key(key) retreived_value = SettingProperties.get_int(key, 0) self.assertEqual(0, retreived_value)
def insert_self_registration(apps, schema_editor): current = SettingProperties.get_int( constants.OPPIA_ALLOW_SELF_REGISTRATION, None) if current is None and hasattr(settings, 'OPPIA_ALLOW_SELF_REGISTRATION'): settings_prop = SettingProperties() settings_prop.key = constants.OPPIA_ALLOW_SELF_REGISTRATION settings_prop.int_value = settings.OPPIA_ALLOW_SELF_REGISTRATION settings_prop.save()
def test_summary_cron_open_cron_open(self): # check lock not set lock = SettingProperties.get_int('oppia_summary_cron_lock', 999) self.assertEqual(lock, 999) lock = SettingProperties.get_int('oppia_cron_lock', 999) self.assertEqual(lock, 999) call_command('update_summaries', stdout=StringIO()) # check new details on pks tracker_id = SettingProperties.get_int('last_tracker_pk', 0) self.assertEqual(tracker_id, 1484256) # this id is from the test_tracker data # check unlocked again lock = SettingProperties.get_int('oppia_summary_cron_lock', 999) self.assertEqual(lock, 999)
def test_summary_invalid_latest_tracker(self): SettingProperties.objects.update_or_create( key='last_tracker_pk', defaults={"int_value": 2000000}) call_command('update_summaries', stdout=StringIO()) self.assertRaises(Tracker.DoesNotExist) # check new details on pks tracker_id = SettingProperties.get_int('last_tracker_pk', 0) self.assertEqual(tracker_id, 2000000)
def get_settings(request): self_register = SettingProperties.get_int(constants.OPPIA_ALLOW_SELF_REGISTRATION, settings.OPPIA_ALLOW_SELF_REGISTRATION) return {'OPPIA_ALLOW_SELF_REGISTRATION': self_register, 'OPPIA_GOOGLE_ANALYTICS_ENABLED': settings.OPPIA_GOOGLE_ANALYTICS_ENABLED, 'OPPIA_GOOGLE_ANALYTICS_CODE': settings.OPPIA_GOOGLE_ANALYTICS_CODE, 'OPPIA_GOOGLE_ANALYTICS_DOMAIN': settings.OPPIA_GOOGLE_ANALYTICS_DOMAIN, 'OPPIA_SHOW_GRAVATARS': settings.OPPIA_SHOW_GRAVATARS, 'OPPIA_DEVICEADMIN_ENABLED': settings.DEVICE_ADMIN_ENABLED, 'OPPIA_REPORTS': menu_reports(request), 'DEBUG': settings.DEBUG, }
def test_summary_cron_locked(self): # set lock not SettingProperties.set_int('oppia_summary_cron_lock', 1) lock = SettingProperties.get_int('oppia_summary_cron_lock', 0) self.assertEqual(lock, 1) update_summaries() # check new details on pks # cron is locked so nothing should happen tracker_id = SettingProperties.get_int('last_tracker_pk', 0) self.assertEqual(tracker_id, 0) #unlock SettingProperties.delete_key('oppia_summary_cron_lock') #check unlocked again lock = SettingProperties.get_int('oppia_summary_cron_lock', 999) self.assertEqual(lock, 999)
def test_summary_exclude_from_reporting(self): call_command('update_summaries', '--fromstart', stdout=StringIO()) # check new details on pks tracker_id = SettingProperties.get_int('last_tracker_pk', 0) self.assertEqual(tracker_id, 1484256) num_daily_stats = CourseDailyStats.objects.count() self.assertEqual(num_daily_stats, 196) excluded_user = UserProfile.objects.get(user__username='******') excluded_user.exclude_from_reporting = True excluded_user.save() call_command('update_summaries', '--fromstart', stdout=StringIO()) # check new details on pks tracker_id = SettingProperties.get_int('last_tracker_pk', 0) self.assertEqual(tracker_id, 1484256) num_daily_stats = CourseDailyStats.objects.count() # New daily stats are less than the previous ones, as some got ignored self.assertEqual(num_daily_stats, 122)
def check_upload_file_size(file, validation_errors): max_upload = SettingProperties.get_int(constants.MAX_UPLOAD_SIZE, settings.OPPIA_MAX_UPLOAD_SIZE) if file is not None and file.size > max_upload: size = int(math.floor(max_upload / 1024 / 1024)) validation_errors.append((_(u"Your file is larger than the maximum \ allowed (%(size)d Mb). You may want to \ check your course for large includes, \ such as images etc.") % {'size': size, })) msg_text = _(u"Maximum course file upload size exceeded") CoursePublishingLog(action="over_max_upload", data=msg_text).save() return validation_errors
def clean(self): cleaned_data = super(UploadCourseStep1Form, self).clean() file = cleaned_data.get("course_file") max_upload = SettingProperties.get_int(constants.MAX_UPLOAD_SIZE, settings.OPPIA_MAX_UPLOAD_SIZE) if file is not None and file.size > max_upload: size = int(math.floor(max_upload / 1024 / 1024)) raise forms.ValidationError(_("Your file is larger than the maximum allowed (%(size)d Mb). You may want to check your course for large includes, such as images etc.") % {'size': size, }) if file is not None and file.content_type != 'application/zip' and file.content_type != 'application/x-zip-compressed': raise forms.ValidationError(_("You may only upload a zip file")) return cleaned_data
def clean(self): cleaned_data = super(UploadCourseStep1Form, self).clean() file = cleaned_data.get("course_file") max_upload = SettingProperties.get_int(constants.MAX_UPLOAD_SIZE, settings.OPPIA_MAX_UPLOAD_SIZE) if file is not None and file._size > max_upload: size = int(math.floor(max_upload / 1024 / 1024)) raise forms.ValidationError(_("Your file is larger than the maximum allowed (%(size)d Mb). You may want to check your course for large includes, such as images etc.") % {'size': size, }) if file is not None and file.content_type != 'application/zip' and file.content_type != 'application/x-zip-compressed': raise forms.ValidationError(_("You may only upload a zip file")) return cleaned_data
def check_upload_file_size_type(file, validation_errors): max_upload = SettingProperties.get_int(constants.MAX_UPLOAD_SIZE, settings.OPPIA_MAX_UPLOAD_SIZE) if file is not None and file.size > max_upload: size = int(math.floor(max_upload / 1024 / 1024)) validation_errors.append((_( u"Your file is larger than the maximum allowed (%(size)d Mb). You may want to check your course for large includes, such as images etc." ) % { 'size': size, })) if file is not None and file.content_type != 'application/zip' and file.content_type != 'application/x-zip-compressed': validation_errors.append(_(u"You may only upload a zip file")) return validation_errors
def get_settings(request): self_register = SettingProperties.get_int( constants.OPPIA_ALLOW_SELF_REGISTRATION, settings.OPPIA_ALLOW_SELF_REGISTRATION) return { 'OPPIA_ALLOW_SELF_REGISTRATION': self_register, 'OPPIA_GOOGLE_ANALYTICS_ENABLED': settings.OPPIA_GOOGLE_ANALYTICS_ENABLED, 'OPPIA_GOOGLE_ANALYTICS_CODE': settings.OPPIA_GOOGLE_ANALYTICS_CODE, 'OPPIA_GOOGLE_ANALYTICS_DOMAIN': settings.OPPIA_GOOGLE_ANALYTICS_DOMAIN, 'OPPIA_SHOW_GRAVATARS': settings.OPPIA_SHOW_GRAVATARS, 'OPPIA_DEVICEADMIN_ENABLED': settings.DEVICE_ADMIN_ENABLED, 'OPPIA_REPORTS': menu_reports(request), 'DEBUG': settings.DEBUG, }
def __init__(self, *args, **kwargs): super(UploadCourseStep1Form, self).__init__( * args, ** kwargs) max_upload = SettingProperties.get_int(constants.MAX_UPLOAD_SIZE, settings.OPPIA_MAX_UPLOAD_SIZE) self.fields['course_file'].help_text = _('Max size %(size)d Mb') % {'size': int(math.floor(max_upload / 1024 / 1024))} self.helper = FormHelper() self.helper.form_action = reverse('oppia_upload') self.helper.form_class = 'form-horizontal' self.helper.label_class = 'col-lg-2' self.helper.field_class = 'col-lg-4' self.helper.layout = Layout( 'course_file', Div( Submit('submit', _(u'Upload'), css_class='btn btn-default'), css_class='col-lg-offset-2 col-lg-4', ), )
def obj_create(self, bundle, **kwargs): self_register = SettingProperties.get_int(constants.OPPIA_ALLOW_SELF_REGISTRATION, settings.OPPIA_ALLOW_SELF_REGISTRATION) if not self_register: raise BadRequest(_(u'Registration is disabled on this server.')) required = ['username', 'password', 'passwordagain', 'email', 'firstname', 'lastname'] check_required_params(bundle, required) data = {'username': bundle.data['username'], 'password': bundle.data['password'], 'password_again': bundle.data['passwordagain'], 'email': bundle.data['email'] if 'email' in bundle.data else '', 'first_name': bundle.data['firstname'], 'last_name': bundle.data['lastname'], } rf = RegisterForm(data) if not rf.is_valid(): str = "" for key, value in rf.errors.items(): for error in value: str += error + "\n" raise BadRequest(str) else: username = bundle.data['username'] password = bundle.data['password'] email = bundle.data['email'] first_name = bundle.data['firstname'] last_name = bundle.data['lastname'] try: bundle.obj = User.objects.create_user(username, email, password) bundle.obj.first_name = first_name bundle.obj.last_name = last_name bundle.obj.save() user_profile = UserProfile() user_profile.user = bundle.obj if 'jobtitle' in bundle.data: user_profile.job_title = bundle.data['jobtitle'] if 'organisation' in bundle.data: user_profile.organisation = bundle.data['organisation'] if 'phoneno' in bundle.data: user_profile.phone_number = bundle.data['phoneno'] user_profile.save() u = authenticate(username=username, password=password) if u is not None and u.is_active: login(bundle.request, u) # Add to tracker tracker = Tracker() tracker.user = u tracker.type = 'register' tracker.ip = bundle.request.META.get('REMOTE_ADDR', api.DEFAULT_IP_ADDRESS) tracker.agent = bundle.request.META.get('HTTP_USER_AGENT', 'unknown') tracker.save() key = ApiKey.objects.get(user=u) bundle.data['api_key'] = key.key except IntegrityError: raise BadRequest(_(u'Username "%s" already in use, please select another' % username)) del bundle.data['passwordagain'] del bundle.data['password'] del bundle.data['firstname'] del bundle.data['lastname'] return bundle
def test_int_is_null(self): key = "testkey" value = "testval" SettingProperties.set_string(key, value) retreived_value = SettingProperties.get_int(key, 123) self.assertEqual(123, retreived_value)
def test_set_int_default(self): key = "testkey1" retreived_value = SettingProperties.get_int(key, 0) self.assertEqual(0, retreived_value)
def test_set_int(self): key = "intkey" value = 123 SettingProperties.set_int(key, value) retreived_value = SettingProperties.get_int(key, 0) self.assertEqual(value, retreived_value)
def obj_create(self, bundle, **kwargs): self_register = SettingProperties.get_int(constants.OPPIA_ALLOW_SELF_REGISTRATION, settings.OPPIA_ALLOW_SELF_REGISTRATION) if not self_register: raise BadRequest(_(u'Registration is disabled on this server.')) required = ['username', 'password', 'passwordagain', 'email', 'firstname', 'lastname'] check_required_params(bundle, required) data = {'username': bundle.data['username'], 'password': bundle.data['password'], 'password_again': bundle.data['passwordagain'], 'email': bundle.data['email'], 'first_name': bundle.data['firstname'], 'last_name': bundle.data['lastname'], } rf = RegisterForm(data) if not rf.is_valid(): str = "" for key, value in rf.errors.items(): for error in value: str += error + "\n" raise BadRequest(str) else: username = bundle.data['username'] password = bundle.data['password'] email = bundle.data['email'] first_name = bundle.data['firstname'] last_name = bundle.data['lastname'] try: bundle.obj = User.objects.create_user(username, email, password) bundle.obj.first_name = first_name bundle.obj.last_name = last_name bundle.obj.save() user_profile = UserProfile() user_profile.user = bundle.obj if 'jobtitle' in bundle.data: user_profile.job_title = bundle.data['jobtitle'] if 'organisation' in bundle.data: user_profile.organisation = bundle.data['organisation'] if 'phoneno' in bundle.data: user_profile.phone_number = bundle.data['phoneno'] user_profile.save() u = authenticate(username=username, password=password) if u is not None and u.is_active: login(bundle.request, u) # Add to tracker tracker = Tracker() tracker.user = u tracker.type = 'register' tracker.ip = bundle.request.META.get('REMOTE_ADDR', api.DEFAULT_IP_ADDRESS) tracker.agent = bundle.request.META.get('HTTP_USER_AGENT', 'unknown') tracker.save() key = ApiKey.objects.get(user=u) bundle.data['api_key'] = key.key except IntegrityError: raise BadRequest(_(u'Username "%s" already in use, please select another' % username)) del bundle.data['passwordagain'] del bundle.data['password'] del bundle.data['firstname'] del bundle.data['lastname'] return bundle
def get_settings(request): self_register = SettingProperties.get_bool( constants.OPPIA_ALLOW_SELF_REGISTRATION, settings.OPPIA_ALLOW_SELF_REGISTRATION) show_gravatars = SettingProperties.get_bool(constants.OPPIA_SHOW_GRAVATARS, settings.OPPIA_SHOW_GRAVATARS) ga_enabled = SettingProperties.get_bool( constants.OPPIA_GOOGLE_ANALYTICS_ENABLED, settings.OPPIA_GOOGLE_ANALYTICS_ENABLED) ga_code = SettingProperties.get_string( constants.OPPIA_GOOGLE_ANALYTICS_CODE, settings.OPPIA_GOOGLE_ANALYTICS_CODE) ga_domain = SettingProperties.get_string( constants.OPPIA_GOOGLE_ANALYTICS_DOMAIN, settings.OPPIA_GOOGLE_ANALYTICS_DOMAIN) try: badge_award_method = BadgeMethod.objects.get( badge__ref="coursecompleted") badge_award_method_percent = SettingProperties.get_int( constants.OPPIA_BADGES_PERCENT_COMPLETED, 100) except BadgeMethod.DoesNotExist: badge_award_method = "undefined" badge_award_method_percent = 100 cron_warning = False last_cron = SettingProperties.get_string(constants.OPPIA_CRON_LAST_RUN, None) last_summary_cron = SettingProperties.get_string( constants.OPPIA_SUMMARY_CRON_LAST_RUN, None) TIME_ZONE_FIX = '+00:00' # fix for bad timezone dates if last_cron and TIME_ZONE_FIX not in last_cron: last_cron += TIME_ZONE_FIX if last_summary_cron and TIME_ZONE_FIX not in last_summary_cron: last_summary_cron += TIME_ZONE_FIX if last_cron is None or last_summary_cron is None: cron_warning = True else: start_date = datetime.datetime.now() - datetime.timedelta(days=7) last_cron_date = datetime.datetime.strptime( last_cron, constants.CRON_DATETIME_FORMAT) if last_cron_date < start_date: cron_warning = True last_summary_cron_date = datetime.datetime.strptime( last_summary_cron, constants.CRON_DATETIME_FORMAT) if last_summary_cron_date < start_date: cron_warning = True return { 'OPPIA_ALLOW_SELF_REGISTRATION': self_register, 'OPPIA_GOOGLE_ANALYTICS_ENABLED': ga_enabled, 'OPPIA_GOOGLE_ANALYTICS_CODE': ga_code, 'OPPIA_GOOGLE_ANALYTICS_DOMAIN': ga_domain, 'OPPIA_SHOW_GRAVATARS': show_gravatars, 'OPPIA_REPORTS': menu_reports(request), 'DEBUG': settings.DEBUG, 'CRON_WARNING': cron_warning, 'COURSE_COMPLETE_BADGE_CRITERIA': badge_award_method, 'COURSE_COMPLETE_BADGE_CRITERIA_PERCENT': badge_award_method_percent }
def test_summary_from_start(self): call_command('update_summaries', '--fromstart', stdout=StringIO()) # check new details on pks tracker_id = SettingProperties.get_int('last_tracker_pk', 0) self.assertEqual(tracker_id, 1484256)