<s> import re <EOL> import warnings <EOL> from django import template <EOL> from django . contrib . gis . geoip import HAS_GEOIP <EOL> from django . utils . safestring import mark_safe <EOL> from django . utils . translation import ugettext_lazy as _ , ugettext <EOL> register = template . Library ( ) <EOL> BROWSERS = ( <EOL> ( re . compile ( '<STR_LIT>' ) , _ ( '<STR_LIT>' ) ) , <EOL> ( re . compile ( '<STR_LIT>' ) , _ ( '<STR_LIT>' ) ) , <EOL> ( re . compile ( '<STR_LIT>' ) , _ ( '<STR_LIT>' ) ) , <EOL> ( re . compile ( '<STR_LIT>' ) , _ ( '<STR_LIT>' ) ) , <EOL> ( re . compile ( '<STR_LIT>' ) , _ ( '<STR_LIT>' ) ) , <EOL> ) <EOL> DEVICES = ( <EOL> ( re . compile ( '<STR_LIT>' ) , _ ( '<STR_LIT>' ) ) , <EOL> ( re . compile ( '<STR_LIT>' ) , _ ( '<STR_LIT>' ) ) , <EOL> ( re . compile ( '<STR_LIT>' ) , _ ( '<STR_LIT>' ) ) , <EOL> ( re . compile ( '<STR_LIT>' ) , _ ( '<STR_LIT>' ) ) , <EOL> ( re . compile ( '<STR_LIT>' ) , _ ( '<STR_LIT>' ) ) , <EOL> ( re . compile ( '<STR_LIT>' ) , _ ( '<STR_LIT>' ) ) , <EOL> ( re . compile ( '<STR_LIT>' ) , _ ( '<STR_LIT>' ) ) , <EOL> ( re . compile ( '<STR_LIT>' ) , _ ( '<STR_LIT>' ) ) , <EOL> ( re . compile ( '<STR_LIT>' ) , _ ( '<STR_LIT>' ) ) , <EOL> ( re . compile ( '<STR_LIT>' ) , _ ( '<STR_LIT>' ) ) , <EOL> ( re . compile ( '<STR_LIT>' ) , _ ( '<STR_LIT>' ) ) , <EOL> ) <EOL> @ register . filter <EOL> def device ( value ) : <EOL> """<STR_LIT>""" <EOL> for regex , name in BROWSERS : <EOL> if regex . search ( value ) : <EOL> browser = name <EOL> break <EOL> else : <EOL> browser = '<STR_LIT>' <EOL> for regex , name in DEVICES : <EOL> if regex . search ( value ) : <EOL> device = name <EOL> break <EOL> else : <EOL> device = '<STR_LIT>' <EOL> return _ ( '<STR_LIT>' ) % { '<STR_LIT>' : browser , <EOL> '<STR_LIT>' : device } <EOL> @ register . filter <EOL> def location ( value ) : <EOL> """<STR_LIT>""" <EOL> location = geoip ( ) and geoip ( ) . city ( value ) <EOL> if location and location [ '<STR_LIT>' ] : <EOL> if location [ '<STR_LIT>' ] : <EOL> return '<STR_LIT>' % ( location [ '<STR_LIT>' ] , location [ '<STR_LIT>' ] ) <EOL> else : <EOL> return location [ '<STR_LIT>' ] <EOL> return mark_safe ( '<STR_LIT>' % ugettext ( '<STR_LIT>' ) ) <EOL> _geoip = None <EOL> def geoip ( ) : <EOL> global _geoip <EOL> if _geoip is None and HAS_GEOIP : <EOL> from django . contrib . gis . geoip import GeoIP <EOL> try : <EOL> _geoip = GeoIP ( ) <EOL> except Exception as e : <EOL> warnings . warn ( str ( e ) ) <EOL> return _geoip </s>
