本文整理汇总了Python中securesync.models.Device.get_own_device方法的典型用法代码示例。如果您正苦于以下问题:Python Device.get_own_device方法的具体用法?Python Device.get_own_device怎么用?Python Device.get_own_device使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类securesync.models.Device
的用法示例。
在下文中一共展示了Device.get_own_device方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: zone_management
# 需要导入模块: from securesync.models import Device [as 别名]
# 或者: from securesync.models.Device import get_own_device [as 别名]
def zone_management(request, zone_id, org_id=None):
context = control_panel_context(request, org_id=org_id, zone_id=zone_id)
own_device = Device.get_own_device()
if not context["zone"] and (zone_id != "None" or Zone.objects.count() != 0 or settings.CENTRAL_SERVER):
raise Http404() # on distributed server, we can make due if they're not registered.
# Accumulate device data
device_data = OrderedDict()
if context["zone"]:
devices = Device.objects.filter(devicezone__zone=context["zone"])
else:
devices = Device.objects.filter(devicemetadata__is_trusted=False)
for device in list(devices.order_by("devicemetadata__is_demo_device", "name")):
user_activity = UserLogSummary.objects.filter(device=device)
sync_sessions = SyncSession.objects.filter(client_device=device)
if not settings.CENTRAL_SERVER and device.id != own_device.id: # Non-local sync sessions unavailable on distributed server
sync_sessions = None
exercise_activity = ExerciseLog.objects.filter(signed_by=device)
device_data[device.id] = {
"name": device.name or device.id,
"num_times_synced": sync_sessions.count() if sync_sessions is not None else None,
"last_time_synced": sync_sessions.aggregate(Max("timestamp"))["timestamp__max"] if sync_sessions is not None else None,
"is_demo_device": device.get_metadata().is_demo_device,
"last_time_used": exercise_activity.order_by("-completion_timestamp")[0:1] if user_activity.count() == 0 else user_activity.order_by("-last_activity_datetime", "-end_datetime")[0],
"counter": device.get_counter_position(),
}
# Accumulate facility data
facility_data = OrderedDict()
if context["zone"]:
facilities = Facility.objects.by_zone(context["zone"])
else:
facilities = Facility.objects.all()
for facility in list(facilities.order_by("name")):
user_activity = UserLogSummary.objects.filter(user__facility=facility)
exercise_activity = ExerciseLog.objects.filter(user__facility=facility)
facility_data[facility.id] = {
"name": facility.name,
"num_users": FacilityUser.objects.filter(facility=facility).count(),
"num_groups": FacilityGroup.objects.filter(facility=facility).count(),
"id": facility.id,
"last_time_used": exercise_activity.order_by("-completion_timestamp")[0:1] if user_activity.count() == 0 else user_activity.order_by("-last_activity_datetime", "-end_datetime")[0],
}
context.update({
"facilities": facility_data,
"devices": device_data,
"upload_form": UploadFileForm(),
"own_device_is_trusted": Device.get_own_device().get_metadata().is_trusted,
})
return context
示例2: update_context
# 需要导入模块: from securesync.models import Device [as 别名]
# 或者: from securesync.models.Device import get_own_device [as 别名]
def update_context(request):
device = Device.get_own_device()
zone = device.get_zone()
context = {
"registered": Device.get_own_device().is_registered(),
"zone_id": zone.id if zone else None,
"device_id": device.id,
}
return context
示例3: zone_management
# 需要导入模块: from securesync.models import Device [as 别名]
# 或者: from securesync.models.Device import get_own_device [as 别名]
def zone_management(request, zone_id, org_id=None):
org = get_object_or_None(Organization, pk=org_id) if org_id else None
zone = get_object_or_404(Zone, pk=zone_id)# if zone_id != "new" else None
own_device = Device.get_own_device()
# Accumulate device data
device_data = dict()
for device in Device.objects.filter(devicezone__zone=zone).order_by("devicemetadata__is_demo_device", "name"):
user_activity = UserLogSummary.objects.filter(device=device)
sync_sessions = SyncSession.objects.filter(client_device=device)
if not settings.CENTRAL_SERVER and device.id != own_device.id: # Non-local sync sessions unavailable on distributed server
sync_sessions = None
exercise_activity = ExerciseLog.objects.filter(signed_by=device)
device_data[device.id] = {
"name": device.name or device.id,
"is_demo_device": device.devicemetadata.is_demo_device,
"num_times_synced": sync_sessions.count() if sync_sessions is not None else None,
"last_time_synced": sync_sessions.aggregate(Max("timestamp"))["timestamp__max"] if sync_sessions is not None else None,
"is_demo_device": device.get_metadata().is_demo_device,
"last_time_used": exercise_activity.order_by("-completion_timestamp")[0:1] if user_activity.count() == 0 else user_activity.order_by("-end_datetime")[0],
"counter": device.get_counter(),
}
# Accumulate facility data
facility_data = dict()
for facility in Facility.objects.by_zone(zone):
user_activity = UserLogSummary.objects.filter(user__facility=facility)
exercise_activity = ExerciseLog.objects.filter(user__facility=facility)
facility_data[facility.id] = {
"name": facility.name,
"num_users": FacilityUser.objects.filter(facility=facility).count(),
"num_groups": FacilityGroup.objects.filter(facility=facility).count(),
"id": facility.id,
"last_time_used": exercise_activity.order_by("-completion_timestamp")[0:1] if user_activity.count() == 0 else user_activity.order_by("-end_datetime")[0],
}
return {
"org": org,
"zone": zone,
"facilities": facility_data,
"devices": device_data,
"upload_form": UploadFileForm(),
"own_device_is_trusted": Device.get_own_device().get_metadata().is_trusted,
}
示例4: register_public_key_client
# 需要导入模块: from securesync.models import Device [as 别名]
# 或者: from securesync.models.Device import get_own_device [as 别名]
def register_public_key_client(request):
if Device.get_own_device().get_zone():
set_as_registered()
return {"already_registered": True}
client = SyncClient()
if client.test_connection() != "success":
return {"no_internet": True}
reg_response = client.register()
reg_status = reg_response.get("code")
if reg_status == "registered":
set_as_registered()
return {"newly_registered": True}
if reg_status == "device_already_registered":
set_as_registered()
return {"already_registered": True}
if reg_status == "public_key_unregistered":
return {
"unregistered": True,
"registration_url": client.path_to_url(
"/securesync/register/?" + urllib.quote(crypto.serialize_public_key())),
}
error_msg = reg_response.get("error", "")
if error_msg:
return {"error_msg": error_msg}
return HttpResponse("Registration status: " + reg_status)
示例5: register_public_key_client
# 需要导入模块: from securesync.models import Device [as 别名]
# 或者: from securesync.models.Device import get_own_device [as 别名]
def register_public_key_client(request):
own_device = Device.get_own_device()
if own_device.get_zone():
set_as_registered()
return {"already_registered": True}
client = SyncClient()
if client.test_connection() != "success":
return {"no_internet": True}
reg_response = client.register()
reg_status = reg_response.get("code")
if reg_status == "registered":
set_as_registered()
return {"newly_registered": True}
if reg_status == "device_already_registered":
set_as_registered()
return {"already_registered": True}
if reg_status == "public_key_unregistered":
return {
"unregistered": True,
"registration_url": client.path_to_url(
reverse("register_public_key") + "?" + urllib.quote(own_device.public_key)
),
"central_login_url": "%s://%s/accounts/login" % (settings.SECURESYNC_PROTOCOL, settings.CENTRAL_SERVER_HOST),
"callback_url": request.build_absolute_uri(reverse("register_public_key")),
}
error_msg = reg_response.get("error", "")
if error_msg:
return {"error_msg": error_msg}
return HttpResponse(_("Registration status: ") + reg_status)
示例6: _do_fake_registration
# 需要导入模块: from securesync.models import Device [as 别名]
# 或者: from securesync.models.Device import get_own_device [as 别名]
def _do_fake_registration(self):
# Create a Zone and DeviceZone to fool the Device into thinking it's registered
zone = Zone(name="The Danger Zone", description="Welcome to it.")
zone.save()
device = Device.get_own_device()
deviceZone = DeviceZone(device=device, zone=zone)
deviceZone.save()
示例7: test_valid_trusted
# 需要导入模块: from securesync.models import Device [as 别名]
# 或者: from securesync.models.Device import get_own_device [as 别名]
def test_valid_trusted(self):
"""
Chain of trust:
1. Zone created by this device
2. Another device joins (no central server) through an invitation
"""
own_device = Device.get_own_device()
zone = Zone(name="test_zone")
zone.save()
new_device = Device(name="new_device") # make a new device
new_device.set_key(Key())
new_device.save() # get an ID
new_device.get_metadata().save()
# Now create an invitation, and claim that invitation for the new device.
invitation = ZoneInvitation.generate(zone=zone, invited_by=own_device)
invitation.claim(used_by=new_device)
self.assertEqual(invitation.used_by, new_device, "Invitation should now be used by device %s" % new_device)
self.assertEqual(DeviceZone.objects.filter(device=new_device).count(), 1, "There should be a DeviceZone for device %s" % new_device)
self.assertEqual(DeviceZone.objects.get(device=new_device).zone, zone, "DeviceZone for device %s should be zone %s" % (new_device, zone))
# Now get a chain of trust establishing the new device on the zone
chain = ChainOfTrust(zone=zone, device=new_device)
self.assertTrue(chain.verify(), "Chain of trust should verify.")
示例8: device_management
# 需要导入模块: from securesync.models import Device [as 别名]
# 或者: from securesync.models.Device import get_own_device [as 别名]
def device_management(request, device_id, zone_id=None, per_page=None, cur_page=None):
context = control_panel_context(request, zone_id=zone_id, device_id=device_id)
#Get pagination details
cur_page = cur_page or request.REQUEST.get("cur_page", "1")
per_page = per_page or request.REQUEST.get("per_page", "10")
# Retrieve sync sessions
all_sessions = SyncSession.objects.filter(client_device=context["device"])
total_sessions = all_sessions.count()
shown_sessions = list(all_sessions.order_by("-timestamp").values("timestamp", "ip", "models_uploaded", "models_downloaded", "errors"))
session_pages, page_urls = paginate_data(request, shown_sessions, page=cur_page, per_page=per_page)
context.update({
"session_pages": session_pages,
"page_urls": page_urls,
"total_sessions": total_sessions,
"device_version": total_sessions and all_sessions[0].client_version or None,
"device_os": total_sessions and all_sessions[0].client_os or None,
"is_own_device": not settings.CENTRAL_SERVER and device_id == Device.get_own_device().id,
})
# If local (and, for security purposes, a distributed server), get device metadata
if context["is_own_device"]:
context.update(local_install_context(request))
return context
示例9: add_log_to_summary
# 需要导入模块: from securesync.models import Device [as 别名]
# 或者: from securesync.models.Device import get_own_device [as 别名]
def add_log_to_summary(cls, user_log, device=None):
"""Adds total_time to the appropriate user/device/activity's summary log."""
assert user_log.end_datetime, "all log items must have an end_datetime to be saved here."
assert user_log.total_seconds >= 0, "all log items must have a non-negative total_seconds to be saved here."
device = device or Device.get_own_device() # Must be done here, or install fails
# Check for an existing object
log_summary = cls.objects.filter(
device=device,
user=user_log.user,
activity_type=user_log.activity_type,
start_datetime__lte=user_log.end_datetime,
end_datetime__gte=user_log.end_datetime,
)
assert log_summary.count() <= 1, "There should never be multiple summaries in the same time period/device/user/type combo"
# Get (or create) the log item
log_summary = log_summary[0] if log_summary.count() else cls(
device=device,
user=user_log.user,
activity_type=user_log.activity_type,
start_datetime=cls.get_period_start_datetime(user_log.end_datetime, settings.USER_LOG_SUMMARY_FREQUENCY),
end_datetime=cls.get_period_end_datetime(user_log.end_datetime, settings.USER_LOG_SUMMARY_FREQUENCY),
total_seconds=0,
count=0,
)
logging.debug("Adding %d seconds for %s/%s/%d, period %s to %s" % (user_log.total_seconds, device.name, user_log.user.username, user_log.activity_type, log_summary.start_datetime, log_summary.end_datetime))
# Add the latest info
log_summary.total_seconds += user_log.total_seconds
log_summary.count += 1
log_summary.save()
示例10: test_facility_group_save
# 需要导入模块: from securesync.models import Device [as 别名]
# 或者: from securesync.models.Device import get_own_device [as 别名]
def test_facility_group_save(self):
# only perform test if we are ourselves a trusted (i.e. central server) device
if Device.get_own_device().is_trusted():
group = FacilityGroup(name="MyGroup", facility=self.facility)
group.save()
assert group.zone_fallback is not None, "Centrally created FacilityGroup was not assigned a zone."
示例11: zone_redirect
# 需要导入模块: from securesync.models import Device [as 别名]
# 或者: from securesync.models.Device import get_own_device [as 别名]
def zone_redirect(request):
"""
Dummy view to generate a helpful dynamic redirect to interface with 'control_panel' app
"""
device = Device.get_own_device()
zone = device.get_zone()
return HttpResponseRedirect(reverse("zone_management", kwargs={"zone_id": (zone and zone.pk) or "None"}))
示例12: confirm_or_generate_zone
# 需要导入模块: from securesync.models import Device [as 别名]
# 或者: from securesync.models.Device import get_own_device [as 别名]
def confirm_or_generate_zone(invitation=None, device_zone=None):
invitation = invitation or get_object_or_None(ZoneInvitation, used_by=Device.get_own_device())
device_zone = device_zone or get_object_or_None(DeviceZone, device=Device.get_own_device())
if invitation:
sys.stdout.write("Confirmed existing sharing network %s, using invitation %s.\n" % (invitation.zone, invitation))
elif device_zone:
sys.stdout.write("Confirmed existing sharing network %s, using device_zone %s.\n" % (device_zone.zone, device_zone))
else:
# Sorry dude, you weren't invited to the party. You'll have to have your own!
# Generate a zone (for stand-alone machines)
call_command("generate_zone")
sys.stdout.write("Successfully generated a sharing network, and joined!.\n")
initialize_registration() # would try to sync
示例13: check_setup_status_wrapper_fn
# 需要导入模块: from securesync.models import Device [as 别名]
# 或者: from securesync.models.Device import get_own_device [as 别名]
def check_setup_status_wrapper_fn(request, *args, **kwargs):
if "registered" not in request.session:
logging.error("Key 'registered' not defined in session, but should be by now.")
if User.objects.exists():
request.has_superuser = True
# next line is for testing
# User.objects.all().delete()
if request.is_admin:
# TODO(bcipolli): move this to the client side?
if not request.session.get("registered", True) and BaseClient().test_connection() == "success":
# Being able to register is more rare, so prioritize.
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 not request.session["facility_exists"]:
zone_id = (Zone.objects.all() and Zone.objects.all()[0].id) or "None"
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", kwargs={"zone_id": zone_id})))
elif not request.is_logged_in:
if not request.session.get("registered", True) and BaseClient().test_connection() == "success":
# Being able to register is more rare, so prioritize.
redirect_url = reverse("register_public_key")
elif not request.session["facility_exists"]:
zone = Device.get_own_device().get_zone()
zone_id = "None" if not zone else zone.id
redirect_url = reverse("add_facility", kwargs={"zone_id": zone_id})
else:
redirect_url = None
if redirect_url:
messages.warning(request, mark_safe(
"Please login with the account you created while running the installation script, \
to complete the setup."))
return handler(request, *args, **kwargs)
示例14: test_facility_user_save
# 需要导入模块: from securesync.models import Device [as 别名]
# 或者: from securesync.models.Device import get_own_device [as 别名]
def test_facility_user_save(self):
# only perform test if we are ourselves a trusted (i.e. central server) device
if Device.get_own_device().is_trusted():
user = FacilityUser(username="bobmcknob", facility=self.facility)
user.set_password("blahblah")
user.save()
assert user.zone_fallback is not None, "Centrally created FacilityUser was not assigned a zone."
示例15: test_not_redirected_when_offline
# 需要导入模块: from securesync.models import Device [as 别名]
# 或者: from securesync.models.Device import get_own_device [as 别名]
def test_not_redirected_when_offline(self):
self.assertFalse(Device.get_own_device().is_registered(), "The device should be unregistered!")
self.assertFalse(am_i_online(url=settings.CENTRAL_SERVER_URL), "Central server should be unreachable!")
updated_videos_url = self.reverse("update_videos")
response = self.client.get(updated_videos_url, follow=True)
redirect_chain = response.redirect_chain # Will be the empty list if there are no redirects
self.assertFalse(redirect_chain, "Should not be redirected when the central server is not reachable! "
"Redirect chain: {0}".format(redirect_chain))