~widelands-dev/widelands-website/trunk

« back to all changes in this revision

Viewing changes to tracking/models.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:
18
18
 
19
19
log = logging.getLogger('tracking.models')
20
20
 
 
21
 
21
22
class VisitorManager(models.Manager):
 
23
 
22
24
    def active(self, timeout=None):
23
 
        """
24
 
        Retrieves only visitors who have been active within the timeout
25
 
        period.
26
 
        """
 
25
        """Retrieves only visitors who have been active within the timeout
 
26
        period."""
27
27
        if not timeout:
28
28
            timeout = utils.get_timeout()
29
29
 
32
32
 
33
33
        return self.get_queryset().filter(last_update__gte=cutoff)
34
34
 
 
35
 
35
36
class Visitor(models.Model):
36
37
    session_key = models.CharField(max_length=40)
37
38
    ip_address = models.CharField(max_length=20)
44
45
    last_update = models.DateTimeField()
45
46
 
46
47
    objects = VisitorManager()
47
 
        
 
48
 
48
49
    def _time_on_site(self):
49
 
        """
50
 
        Attempts to determine the amount of time a visitor has spent on the
51
 
        site based upon their information that's in the database.
52
 
        """
 
50
        """Attempts to determine the amount of time a visitor has spent on the
 
51
        site based upon their information that's in the database."""
53
52
        if self.session_start:
54
53
            seconds = (self.last_update - self.session_start).seconds
55
54
 
64
63
    time_on_site = property(_time_on_site)
65
64
 
66
65
    def _get_geoip_data(self):
67
 
        """
68
 
        Attempts to retrieve MaxMind GeoIP data based upon the visitor's IP
69
 
        """
 
66
        """Attempts to retrieve MaxMind GeoIP data based upon the visitor's
 
67
        IP."""
70
68
 
71
69
        if not HAS_GEOIP or not USE_GEOIP:
72
70
            # go no further when we don't need to
73
 
            log.debug('Bailing out.  HAS_GEOIP: %s; TRACKING_USE_GEOIP: %s' % (HAS_GEOIP, USE_GEOIP))
 
71
            log.debug('Bailing out.  HAS_GEOIP: %s; TRACKING_USE_GEOIP: %s' % (
 
72
                HAS_GEOIP, USE_GEOIP))
74
73
            return None
75
74
 
76
75
        if not hasattr(self, '_geoip_data'):
80
79
                self._geoip_data = gip.city(self.ip_address)
81
80
            except GeoIPException:
82
81
                # don't even bother...
83
 
                log.error('Error getting GeoIP data for IP "%s": %s' % (self.ip_address, traceback.format_exc()))
 
82
                log.error('Error getting GeoIP data for IP "%s": %s' %
 
83
                          (self.ip_address, traceback.format_exc()))
84
84
 
85
85
        return self._geoip_data
86
86
 
87
87
    geoip_data = property(_get_geoip_data)
88
88
 
89
89
    def _get_geoip_data_json(self):
90
 
        """
91
 
        Cleans out any dirty unicode characters to make the geoip data safe for
92
 
        JSON encoding.
93
 
        """
 
90
        """Cleans out any dirty unicode characters to make the geoip data safe
 
91
        for JSON encoding."""
94
92
        clean = {}
95
 
        if not self.geoip_data: return {}
 
93
        if not self.geoip_data:
 
94
            return {}
96
95
 
97
 
        for key,value in self.geoip_data.items():
 
96
        for key, value in self.geoip_data.items():
98
97
            clean[key] = utils.u_clean(value)
99
98
        return clean
100
99
 
105
104
        unique_together = ('session_key', 'ip_address',)
106
105
        app_label = 'tracking'
107
106
 
 
107
 
108
108
class UntrackedUserAgent(models.Model):
109
 
    keyword = models.CharField(_('keyword'), max_length=100, help_text=_('Part or all of a user-agent string.  For example, "Googlebot" here will be found in "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" and that visitor will not be tracked.'))
 
109
    keyword = models.CharField(_('keyword'), max_length=100, help_text=_(
 
110
        'Part or all of a user-agent string.  For example, "Googlebot" here will be found in "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" and that visitor will not be tracked.'))
110
111
 
111
112
    def __unicode__(self):
112
113
        return self.keyword
117
118
        verbose_name_plural = _('Untracked User-Agents')
118
119
        app_label = 'tracking'
119
120
 
 
121
 
120
122
class BannedIP(models.Model):
121
 
    ip_address = models.GenericIPAddressField('IP Address', help_text=_('The IP address that should be banned'))
 
123
    ip_address = models.GenericIPAddressField(
 
124
        'IP Address', help_text=_('The IP address that should be banned'))
122
125
 
123
126
    def __unicode__(self):
124
127
        return self.ip_address
127
130
        ordering = ('ip_address',)
128
131
        verbose_name = _('Banned IP')
129
132
        verbose_name_plural = _('Banned IPs')
130
 
        app_label = 'tracking'
 
 
b'\\ No newline at end of file'
 
133
        app_label = 'tracking'