Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split device template filter into two extra template filters #133

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea/
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't include unrelated changes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hint: You can include this in $HOME/.config/git/ignore and it will be ignored on any local repository.

/example/database.sqlite3
/example/GeoLiteCity.dat
/django_user_sessions.egg-info/
Expand Down
73 changes: 55 additions & 18 deletions user_sessions/templatetags/user_sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
(re.compile('Opera'), _('Opera')),
(re.compile('IE'), _('Internet Explorer')),
)
DEVICES = (
PLATFORMS = (
(re.compile('Windows Mobile'), _('Windows Mobile')),
(re.compile('Android'), _('Android')),
(re.compile('Linux'), _('Linux')),
Expand All @@ -40,43 +40,80 @@


@register.filter
def device(value):
def platform(value):
"""
Transform a User Agent into human readable text.

Example output:

* Safari on iPhone
* Chrome on Windows 8.1
* Safari on macOS
* Firefox
* iPhone
* Windows 8.1
* macOS
* Linux
* None
"""

platform = None
for regex, name in PLATFORMS:
if regex.search(value):
platform = name
break

return platform


@register.filter
def browser(value):
"""
Transform a User Agent into human readable text.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having the same docstring for all three filters will be confusing, update the docstring to reflect the return value.


Example output:

* Safari
* Chrome
* Safari
* Firefox
* None
"""

browser = None
for regex, name in BROWSERS:
if regex.search(value):
browser = name
break

device = None
for regex, name in DEVICES:
if regex.search(value):
device = name
break
return browser


@register.filter
def device(value):
"""
Transform a User Agent into human readable text.

Example output:

* Safari on iPhone
* Chrome on Windows 8.1
* Safari on macOS
* Firefox
* Linux
* None
"""

browser_ = browser(value)
platform_ = platform(value)

if browser and device:
if browser_ and platform_:
return _('%(browser)s on %(device)s') % {
'browser': browser,
'device': device
'browser': browser_,
'device': platform_
}

if browser:
return browser
if browser_:
return browser_

if device:
return device
if platform_:
return platform_

return None

Expand Down