This repository has been archived by the owner on Jan 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
misc.py
93 lines (75 loc) · 3.24 KB
/
misc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# coding:utf-8
'''
Created on 27.03.2010
@license: GNU GPL v3 or above, see LICENSE for more details.
@copyleft: 2010-2011 by the django-sync-server team, see AUTHORS for more details.
'''
# Due to Mozilla Weave supporting Recaptcha solely, we have to stick with it until
# they decide to change the interface to pluggable captchas.
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse
# django-sync-server own stuff
from weave import Logging, VERSION_STRING
from weave.decorators import weave_assert_version, debug_sync_request
from django.shortcuts import render_to_response
import time
from weave.models import Wbo
logger = Logging.get_logger()
@debug_sync_request
@weave_assert_version(("1.0", "1.1"))
@csrf_exempt
def captcha(request, version):
if settings.WEAVE.DONT_USE_CAPTCHA == True:
logger.warn("You should activate captcha!")#
return HttpResponse("Captcha support is disabled.")
# Check for aviability of recaptcha
# (can be found at: http://pypi.python.org/pypi/recaptcha-client)
try:
from recaptcha.client.captcha import displayhtml
except ImportError:
logger.error("Captcha requested but unable to import the 'recaptcha' package!")
return HttpResponse("Captcha support disabled due to missing Python package 'recaptcha'.")
if not getattr(settings.WEAVE, "RECAPTCHA_PUBLIC_KEY"):
logger.error("Trying to create user but settings.WEAVE.RECAPTCHA_PUBLIC_KEY not set")
raise ImproperlyConfigured
# Send a simple HTML to the client. It get's rendered inside the Weave client.
return HttpResponse("<html><body>%s</body></html>" % displayhtml(settings.WEAVE.RECAPTCHA_PUBLIC_KEY))
def info_page(request):
server_url = request.build_absolute_uri(request.path)
if not server_url.endswith("/"):
# sync setup dialog only accept the server url if it's ends with a slash
server_url += "/"
context = {
"title": "django-sync-server - info page",
"request": request,
"weave_version": VERSION_STRING,
"server_url":server_url,
}
if request.user.is_authenticated() and request.user.is_active:
start_time = time.time()
payload_queryset = Wbo.objects.filter(user=request.user.id).only("payload")
wbo_count = 0
payload_size = 0
for item in payload_queryset.iterator():
wbo_count += 1
payload_size += len(item.payload)
modified_queryset = Wbo.objects.filter(user=request.user.id).only("modified")
try:
latest = modified_queryset.latest("modified").modified
except Wbo.DoesNotExist:
# User hasn't used sync, so no WBOs exist from him
latest = None
oldest = None
else:
oldest = modified_queryset.order_by("modified")[0].modified
duration = time.time() - start_time
context.update({
"wbo_count": wbo_count,
"payload_size": payload_size,
"duration": duration,
"latest_modified": latest,
"oldest_modified": oldest,
})
return render_to_response("weave/info_page.html", context)