本文整理汇总了Python中main.models.UserLog类的典型用法代码示例。如果您正苦于以下问题:Python UserLog类的具体用法?Python UserLog怎么用?Python UserLog使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了UserLog类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: generate_fake_coachreport_logs
def generate_fake_coachreport_logs(password="hellothere"):
t,_ = FacilityUser.objects.get_or_create(
facility=Facility.objects.all()[0],
username=random.choice(firstnames)
)
t.set_password(password)
# TODO: create flags later
num_logs = 20
logs = []
for _ in xrange(num_logs):
date_logged_in = datetime.datetime.now() - datetime.timedelta(days=random.randint(1,10))
date_viewed_coachreport = date_logged_in + datetime.timedelta(minutes=random.randint(0, 30))
date_logged_out = date_viewed_coachreport + datetime.timedelta(minutes=random.randint(0, 30))
login_log = UserLog.objects.create(
user=t,
activity_type=UserLog.get_activity_int("login"),
start_datetime=date_logged_in,
last_active_datetime=date_viewed_coachreport,
end_datetime=date_logged_out,
)
logging.info("created login log for teacher %s" % t.username)
coachreport_log = UserLog.objects.create(
user=t,
activity_type=UserLog.get_activity_int("coachreport"),
start_datetime=date_viewed_coachreport,
last_active_datetime=date_viewed_coachreport,
end_datetime=date_viewed_coachreport,
)
logs.append((login_log, coachreport_log))
logging.info("created coachreport log for teacher %s" % t.username)
return logs
示例2: query_logs
def query_logs(users, items, logtype, logdict):
"""
Get a specified subset of logs for a particular set of users for either exercises or videos.
users: list of users to query against.
items: list of either exercises of videos to query.
logtype: video or exercise - in future this could be expanded to query activity logs too.
logdict: user keyed dictionary of logs (presumed to be empty by this code)
"""
if logtype == "exercise":
all_logs = ExerciseLog.objects.filter(user__in=users, exercise_id__in=items).values(
'user', 'complete', 'exercise_id', 'attempts', 'points', 'struggling', 'completion_timestamp', 'streak_progress').order_by('completion_timestamp')
elif logtype == "video":
all_logs = VideoLog.objects.filter(user__in=users, video_id__in=items).values(
'user', 'complete', 'video_id', 'total_seconds_watched', 'completion_timestamp', 'points').order_by('completion_timestamp')
elif logtype == "activity" and UserLog.is_enabled():
all_logs = UserLog.objects.filter(user__in=users).values(
'user', 'last_active_datetime', 'total_seconds').order_by('last_active_datetime')
elif logtype == "summaryactivity" and UserLog.is_enabled():
all_logs = UserLogSummary.objects.filter(user__in=users).values(
'user', 'device', 'total_seconds').order_by('end_datetime')
else:
assert False, "Unknown log type: '%s'" % logtype # indicates a programming error
for log in all_logs:
logdict[log['user']].append(log)
return logdict
示例3: login
def login(request, facility):
facility_id = facility and facility.id or None
facilities = list(Facility.objects.all())
# Fix for #1211: refresh cached facility info when it's free and relevant
refresh_session_facility_info(request, facility_count=len(facilities))
if request.method == 'POST':
# log out any Django user or facility user
logout(request)
username = request.POST.get("username", "")
password = request.POST.get("password", "")
# first try logging in as a Django user
user = authenticate(username=username, password=password)
if user:
auth_login(request, user)
return HttpResponseRedirect(request.next or reverse("easy_admin"))
# try logging in as a facility user
form = LoginForm(data=request.POST, request=request, initial={"facility": facility_id})
if form.is_valid():
user = form.get_user()
try:
UserLog.begin_user_activity(user, activity_type="login", language=request.language) # Success! Log the event (ignoring validation failures)
except ValidationError as e:
logging.error("Failed to begin_user_activity upon login: %s" % e)
request.session["facility_user"] = user
messages.success(request, _("You've been logged in! We hope you enjoy your time with KA Lite ") +
_("-- be sure to log out when you finish."))
# Send them back from whence they came
landing_page = form.cleaned_data["callback_url"]
if not landing_page:
# Just going back to the homepage? We can do better than that.
landing_page = reverse("coach_reports") if form.get_user().is_teacher else None
landing_page = landing_page or (reverse("account_management") if not settings.package_selected("RPi") else reverse("homepage"))
return HttpResponseRedirect(form.non_field_errors() or request.next or landing_page)
else:
messages.error(
request,
_("There was an error logging you in. Please correct any errors listed below, and try again."),
)
else: # render the unbound login form
referer = urlparse.urlparse(request.META["HTTP_REFERER"]).path if request.META.get("HTTP_REFERER") else None
# never use the homepage as the referer
if referer in [reverse("homepage"), reverse("add_facility_student")]:
referer = None
form = LoginForm(initial={"facility": facility_id, "callback_url": referer})
return {
"form": form,
"facilities": facilities,
}
示例4: logout
def logout(request):
if "facility_user" in request.session:
# Logout, ignore any errors.
try:
UserLog.end_user_activity(request.session["facility_user"], activity_type="login")
except ValidationError as e:
logging.error("Failed to end_user_activity upon logout: %s" % e)
del request.session["facility_user"]
auth_logout(request)
next = request.GET.get("next", reverse("homepage"))
if next[0] != "/":
next = "/"
return HttpResponseRedirect(next)
示例5: login
def login(request, facility):
facilities = Facility.objects.all()
facility_id = facility and facility.id or None
if request.method == 'POST':
# log out any Django user or facility user
logout(request)
username = request.POST.get("username", "")
password = request.POST.get("password", "")
# first try logging in as a Django user
user = authenticate(username=username, password=password)
if user:
auth_login(request, user)
return HttpResponseRedirect(request.next or reverse("easy_admin"))
# try logging in as a facility user
form = LoginForm(data=request.POST, request=request, initial={"facility": facility_id})
if form.is_valid():
user = form.get_user()
try:
UserLog.begin_user_activity(user, activity_type="login") # Success! Log the event (ignoring validation failures)
except ValidationError as e:
logging.debug("Failed to begin_user_activity upon login: %s" % e)
request.session["facility_user"] = user
messages.success(request, _("You've been logged in! We hope you enjoy your time with KA Lite ") +
_("-- be sure to log out when you finish."))
return HttpResponseRedirect(
form.non_field_errors()
or request.next
or reverse("coach_reports") if form.get_user().is_teacher else reverse("homepage")
)
else:
messages.error(
request,
strip_tags(form.non_field_errors())
or _("There was an error logging you in. Please correct any errors listed below, and try again.")
)
else: # render the unbound login form
form = LoginForm(initial={"facility": facility_id})
return {
"form": form,
"facilities": facilities
}
示例6: test_query_login_student
def test_query_login_student(self):
"""Check the # of queries when logging in as a student."""
student = FacilityUser(is_teacher=False, username="s1", facility=self.facility)
passwd = self._gen_valid_password()
student.set_password(passwd)
student.save()
with self.assertNumQueries(39 + 3*UserLog.is_enabled()):
self.browser_login_student("s1", passwd, self.facility)
示例7: test_query_login_teacher
def test_query_login_teacher(self):
"""Check the # of queries when logging in as a teacher."""
teacher = FacilityUser(is_teacher=True, username="t1", facility=self.facility)
passwd = self._gen_valid_password()
teacher.set_password(passwd)
teacher.save()
with self.assertNumQueries(39 + 3*UserLog.is_enabled()):
self.browser_login_teacher("t1", passwd, self.facility)
示例8: account_management
def account_management(request, org_id=None):
# Only log 'coachreport' activity for students,
# (otherwise it's hard to compare teachers)
if "facility_user" in request.session and not request.session["facility_user"].is_teacher and reverse("login") not in request.META.get("HTTP_REFERER", ""):
try:
# Log a "begin" and end here
user = request.session["facility_user"]
UserLog.begin_user_activity(user, activity_type="coachreport")
UserLog.update_user_activity(user, activity_type="login") # to track active login time for teachers
UserLog.end_user_activity(user, activity_type="coachreport")
except ValidationError as e:
# Never report this error; don't want this logging to block other functionality.
logging.error("Failed to update student userlog activity: %s" % e)
return student_view_context(request)
示例9: generate_fake_exercise_logs
#.........这里部分代码省略.........
(elogs, ulogs) = generate_fake_exercise_logs(facility_user=user, topics=[topic], start_date=start_date)
exercise_logs.append(elogs)
user_logs.append(ulogs)
# Actually generate!
else:
# Get (or create) user type
try:
user_settings = json.loads(facility_user.notes)
except:
user_settings = sample_user_settings()
facility_user.notes = json.dumps(user_settings)
facility_user.save()
date_diff_started = datetime.timedelta(seconds=datediff(date_diff, units="seconds") * user_settings["time_in_program"]) # when this user started in the program, relative to NOW
for topic in topics:
# Get all exercises related to the topic
exercises = get_topic_exercises(topic_id=topic)
# Problem:
# Not realistic for students to have lots of unfinished exercises.
# If they start them, they tend to get stuck, right?
#
# So, need to make it more probable that they will finish an exercise,
# and less probable that they start one.
#
# What we need is P(streak|started), not P(streak)
# Probability of doing any particular exercise
p_exercise = probability_of(qty="exercise", user_settings=user_settings)
logging.debug("# exercises: %d; p(exercise)=%4.3f, user settings: %s\n" % (len(exercises), p_exercise, json.dumps(user_settings)))
# of exercises is related to
for j, exercise in enumerate(exercises):
if random.random() > p_exercise:
continue
# Probability of completing this exercise, and .. proportion of attempts
p_completed = probability_of(qty="completed", user_settings=user_settings)
p_attempts = probability_of(qty="attempts", user_settings=user_settings)
attempts = int(random.random() * p_attempts * 30 + 10) # always enough to have completed
completed = (random.random() < p_completed)
if completed:
streak_progress = 100
else:
streak_progress = max(0, min(90, random.gauss(100 * user_settings["speed_of_learning"], 20)))
streak_progress = int(floor(streak_progress / 10.)) * 10
points = streak_progress / 10 * 12 if completed else 0 # only get points when you master.
# Choose a rate of exercises, based on their effort level and speed of learning.
# Compute the latest possible start time.
# Then sample a start time between their start time
# and the latest possible start_time
rate_of_exercises = 0.66 * user_settings["effort_level"] + 0.33 * user_settings["speed_of_learning"] # exercises per day
time_for_attempts = min(datetime.timedelta(days=rate_of_exercises * attempts), date_diff_started) # protect with min
time_delta_completed = datetime.timedelta(seconds=random.randint(int(datediff(time_for_attempts, units="seconds")), int(datediff(date_diff_started, units="seconds"))))
date_completed = datetime.datetime.now() - time_delta_completed
# Always create new
logging.info("Creating exercise log: %-12s: %-25s (%d points, %d attempts, %d%% streak on %s)" % (
facility_user.first_name,
exercise["name"],
points,
attempts,
streak_progress,
date_completed,
))
try:
elog = ExerciseLog.objects.get(user=facility_user, exercise_id=exercise["name"])
except ExerciseLog.DoesNotExist:
elog = ExerciseLog(
user=facility_user,
exercise_id=exercise["name"],
attempts=int(attempts),
streak_progress=streak_progress,
points=int(points),
completion_timestamp=date_completed,
completion_counter=datediff(date_completed, start_date, units="seconds"),
)
elog.full_clean()
elog.save() # TODO(bcipolli): bulk saving of logs
# For now, make all attempts on an exercise into a single UserLog.
seconds_per_attempt = 10 * (1 + user_settings["speed_of_learning"] * random.random())
time_to_navigate = 15 * (0.5 + random.random()) #between 7.5s and 22.5s
time_to_logout = 5 * (0.5 + random.random()) # between 2.5 and 7.5s
ulog = UserLog(
user=facility_user,
activity_type=1,
start_datetime = date_completed - datetime.timedelta(seconds=int(attempts * seconds_per_attempt + time_to_navigate)),
end_datetime = date_completed + datetime.timedelta(seconds=time_to_logout),
last_active_datetime = date_completed,
)
ulog.full_clean()
ulog.save()
user_logs.append(ulog)
exercise_logs.append(elog)
return (exercise_logs, user_logs)
示例10: test_query_logout_student
def test_query_logout_student(self):
""""""
self.test_query_login_student()
with self.assertNumQueries(14 + 11*UserLog.is_enabled()):
self.browser_logout_user()
示例11: test_query_logout_teacher
def test_query_logout_teacher(self):
""""""
self.test_query_login_teacher()
with self.assertNumQueries(16 + 11*UserLog.is_enabled()):
self.browser_logout_user()
示例12: test_query_logout_admin
def test_query_logout_admin(self):
""""""
self.test_query_login_admin()
with self.assertNumQueries(17 + 0*UserLog.is_enabled()):
self.browser_logout_user()
示例13: test_query_login_admin
def test_query_login_admin(self):
with self.assertNumQueries(38 + 0*UserLog.is_enabled()):
self.browser_login_admin()
示例14: _get_user_usage_data
def _get_user_usage_data(users, period_start=None, period_end=None):
"""
Returns facility user data, within the given date range.
"""
# compute period start and end
# Now compute stats, based on queried data
len_all_exercises = len(topicdata.NODE_CACHE['Exercise'])
user_data = OrderedDict()
group_data = OrderedDict()
# Make queries efficiently
exercise_logs = ExerciseLog.objects.filter(user__in=users, complete=True)
video_logs = VideoLog.objects.filter(user__in=users)
login_logs = UserLogSummary.objects.filter(user__in=users)
# filter results
if period_start:
exercise_logs = exercise_logs.filter(completion_timestamp__gte=period_start)
video_logs = video_logs.filter(completion_timestamp__gte=period_start)
login_logs = login_logs.filter(start_datetime__gte=period_start)
if period_end:
exercise_logs = exercise_logs.filter(completion_timestamp__lte=period_end)
video_logs = video_logs.filter(completion_timestamp__lte=period_end)
login_logs = login_logs.filter(end_datetime__lte=period_end)
# Force results in a single query
exercise_logs = list(exercise_logs.values("exercise_id", "user__pk"))
video_logs = list(video_logs.values("youtube_id", "user__pk"))
login_logs = list(login_logs.values("activity_type", "total_seconds", "user__pk"))
for user in users:
user_data[user.pk] = OrderedDict()
user_data[user.pk]["first_name"] = user.first_name
user_data[user.pk]["last_name"] = user.last_name
user_data[user.pk]["username"] = user.username
user_data[user.pk]["group"] = user.group
user_data[user.pk]["total_report_views"] = 0#report_stats["count__sum"] or 0
user_data[user.pk]["total_logins"] =0# login_stats["count__sum"] or 0
user_data[user.pk]["total_hours"] = 0#login_stats["total_seconds__sum"] or 0)/3600.
user_data[user.pk]["total_exercises"] = 0
user_data[user.pk]["pct_mastery"] = 0.
user_data[user.pk]["exercises_mastered"] = []
user_data[user.pk]["total_videos"] = 0
user_data[user.pk]["videos_watched"] = []
for elog in exercise_logs:
user_data[elog["user__pk"]]["total_exercises"] += 1
user_data[elog["user__pk"]]["pct_mastery"] += 1. / len_all_exercises
user_data[elog["user__pk"]]["exercises_mastered"].append(elog["exercise_id"])
for vlog in video_logs:
user_data[vlog["user__pk"]]["total_videos"] += 1
user_data[vlog["user__pk"]]["videos_watched"].append(vlog["youtube_id"])
for llog in login_logs:
if llog["activity_type"] == UserLog.get_activity_int("coachreport"):
user_data[llog["user__pk"]]["total_report_views"] += 1
elif llog["activity_type"] == UserLog.get_activity_int("login"):
user_data[llog["user__pk"]]["total_hours"] += (llog["total_seconds"]) / 3600.
user_data[llog["user__pk"]]["total_logins"] += 1
# Add group data. Allow a fake group "Ungrouped"
for user in users:
group_pk = getattr(user.group, "pk", None)
group_name = getattr(user.group, "name", "Ungrouped")
if not group_pk in group_data:
group_data[group_pk] = {
"name": group_name,
"total_logins": 0,
"total_hours": 0,
"total_users": 0,
"total_videos": 0,
"total_exercises": 0,
"pct_mastery": 0,
}
group_data[group_pk]["total_users"] += 1
group_data[group_pk]["total_logins"] += user_data[user.pk]["total_logins"]
group_data[group_pk]["total_hours"] += user_data[user.pk]["total_hours"]
group_data[group_pk]["total_videos"] += user_data[user.pk]["total_videos"]
group_data[group_pk]["total_exercises"] += user_data[user.pk]["total_exercises"]
total_mastery_so_far = (group_data[group_pk]["pct_mastery"] * (group_data[group_pk]["total_users"] - 1) + user_data[user.pk]["pct_mastery"])
group_data[group_pk]["pct_mastery"] = total_mastery_so_far / group_data[group_pk]["total_users"]
return (user_data, group_data)
示例15: facility_usage
def facility_usage(request, facility_id, org_id=None, zone_id=None):
# Basic data
org = get_object_or_None(Organization, pk=org_id) if org_id else None
zone = get_object_or_None(Zone, pk=zone_id) if zone_id else None
facility = get_object_or_404(Facility, pk=facility_id)
groups = FacilityGroup.objects.filter(facility=facility).order_by("name")
users = FacilityUser.objects.filter(facility=facility).order_by("last_name")
# Accumulating data
len_all_exercises = len(topicdata.NODE_CACHE['Exercise'])
group_data = collections.OrderedDict()
user_data = collections.OrderedDict()
for user in users:
exercise_logs = ExerciseLog.objects.filter(user=user)
exercise_stats = {"count": exercise_logs.count(), "total_mastery": exercise_logs.aggregate(Sum("complete"))["complete__sum"]}
video_stats = {"count": VideoLog.objects.filter(user=user).count()}
login_stats = UserLogSummary.objects \
.filter(user=user, activity_type=UserLog.get_activity_int("login")) \
.aggregate(Sum("count"), Sum("total_seconds"))
user_data[user.pk] = {
"first_name": user.first_name,
"last_name": user.last_name,
"name": user.get_name(),
"group": user.group,
"total_logins": login_stats["count__sum"] or 0,
"total_hours": (login_stats["total_seconds__sum"] or 0)/3600.,
"total_videos": video_stats["count"],
"total_exercises": exercise_stats["count"],
"pct_mastery": (exercise_stats["total_mastery"] or 0)/float(len_all_exercises),
}
group = user.group
if group:
if not group.pk in group_data:
group_data[group.pk] = {
"name": group.name,
"total_logins": 0,
"total_hours": 0,
"total_users": 0,
"total_videos": 0,
"total_exercises": 0,
"pct_mastery": 0,
}
group_data[group.pk]["total_users"] += 1
group_data[group.pk]["total_logins"] += user_data[user.pk]["total_logins"]
group_data[group.pk]["total_hours"] += user_data[user.pk]["total_hours"]
group_data[group.pk]["total_videos"] += user_data[user.pk]["total_videos"]
group_data[group.pk]["total_exercises"] += user_data[user.pk]["total_exercises"]
total_mastery_so_far = (group_data[group.pk]["pct_mastery"] * (group_data[group.pk]["total_users"] - 1) + user_data[user.pk]["pct_mastery"])
group_data[group.pk]["pct_mastery"] = total_mastery_so_far / group_data[group.pk]["total_users"]
return {
"org": org,
"zone": zone,
"facility": facility,
"groups": group_data,
"users": user_data,
}