~widelands-dev/widelands-website/trunk

« back to all changes in this revision

Viewing changes to tracking/views.py

  • Committer: franku
  • Author(s): GunChleoc
  • Date: 2016-12-13 18:30:38 UTC
  • mfrom: (438.1.6 pyformat_util)
  • Revision ID: somal@arcor.de-20161213183038-5cgmvfh2fkgmoc1s
adding a script to run pyformat over the code base

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
                                    'tracking/visitor_map.html')
17
17
log = logging.getLogger('tracking.views')
18
18
 
 
19
 
19
20
def update_active_users(request):
20
 
    """
21
 
    Returns a list of all active users
22
 
    """
 
21
    """Returns a list of all active users."""
23
22
    if request.is_ajax():
24
23
        active = Visitor.objects.active()
25
24
        user = getattr(request, 'user', None)
41
40
    # if the request was not made via AJAX, raise a 404
42
41
    raise Http404
43
42
 
 
43
 
44
44
@never_cache
45
45
def get_active_users(request):
46
 
    """
47
 
    Retrieves a list of active users which is returned as plain JSON for
48
 
    easier manipulation with JavaScript.
49
 
    """
 
46
    """Retrieves a list of active users which is returned as plain JSON for
 
47
    easier manipulation with JavaScript."""
50
48
    if request.is_ajax():
51
49
        active = Visitor.objects.active().reverse()
52
50
        now = datetime.now()
63
61
                    'geoip': v.geoip_data_json,
64
62
                    'last_update': (now - v.last_update).seconds,
65
63
                    'friendly_time': ', '.join(friendly_time((now - v.last_update).seconds)),
66
 
                } for v in active]}
 
64
                    } for v in active]}
67
65
        except:
68
 
            log.error('There was a problem putting all of the visitor data together:\n%s\n\n%s' % (traceback.format_exc(), locals()))
 
66
            log.error('There was a problem putting all of the visitor data together:\n%s\n\n%s' % (
 
67
                traceback.format_exc(), locals()))
69
68
            return HttpResponse(content='{}', mimetype='text/javascript')
70
69
 
71
70
        response = HttpResponse(content=JSONEncoder().encode(data),
77
76
    # if the request was not made via AJAX, raise a 404
78
77
    raise Http404
79
78
 
 
79
 
80
80
def friendly_time(last_update):
81
81
    minutes = last_update / 60
82
82
    seconds = last_update % 60
84
84
    friendly_time = []
85
85
    if minutes > 0:
86
86
        friendly_time.append(ungettext(
87
 
                '%(minutes)i minute',
88
 
                '%(minutes)i minutes',
89
 
                minutes
90
 
        ) % {'minutes': minutes })
 
87
            '%(minutes)i minute',
 
88
            '%(minutes)i minutes',
 
89
            minutes
 
90
        ) % {'minutes': minutes})
91
91
    if seconds > 0:
92
92
        friendly_time.append(ungettext(
93
 
                '%(seconds)i second',
94
 
                '%(seconds)i seconds',
95
 
                seconds
96
 
        ) % {'seconds': seconds })
 
93
            '%(seconds)i second',
 
94
            '%(seconds)i seconds',
 
95
            seconds
 
96
        ) % {'seconds': seconds})
97
97
 
98
98
    return friendly_time or 0
99
99
 
 
100
 
100
101
def display_map(request, template_name=DEFAULT_TRACKING_TEMPLATE,
101
 
        extends_template='base.html'):
102
 
    """
103
 
    Displays a map of recently active users.  Requires a Google Maps API key
104
 
    and GeoIP in order to be most effective.
 
102
                extends_template='base.html'):
 
103
    """Displays a map of recently active users.
 
104
 
 
105
    Requires a Google Maps API key and GeoIP in order to be most
 
106
    effective.
 
107
 
105
108
    """
106
109
 
107
110
    GOOGLE_MAPS_KEY = getattr(settings, 'GOOGLE_MAPS_KEY', None)