15 from xml.dom.minidom import parse, parseString
16 from forum.startup import get_database_engine
17 from forum.models import Question, User
18 from forum.settings import APP_URL, SVN_REVISION, APP_TITLE, APP_DESCRIPTION
19 from django import VERSION as DJANGO_VERSION
20 from django.utils import simplejson
21 from django.utils.html import escape
22 from django.utils.encoding import smart_unicode
23 from django.conf import settings as django_settings
24 from django.utils.translation import ugettext as _
27 def generate_installation_key():
28 gen = lambda length: "".join( [random.choice(string.digits+string.letters) for i in xrange(length)])
29 return '%s-%s-%s-%s' % (gen(4), gen(4), gen(4), gen(4))
31 # To get the site views count we get the SUM of all questions views.
35 # Go through all questions and increase the views count
36 for question in Question.objects.all():
37 views += question.view_count
41 # Gets the active users count since the last visit
42 def get_active_users():
46 if settings.LATEST_UPDATE_DATETIME:
47 users_count = User.objects.filter(last_login__gt=settings.LATEST_UPDATE_DATETIME).count()
53 def get_server_name():
58 request = urllib2.Request(url)
59 response = urllib2.urlopen(request)
61 # Get the response information
62 response_info = response.info()
64 server_name = re.findall("Server: (?P<server_name>.*)$", str(response_info))[0]
65 server_name = ''.join(server_name.splitlines())
71 def get_admin_emails():
74 for user in User.objects.filter(is_superuser=True):
75 emails.append(user.email)
79 def check_for_updates():
80 # Get the SVN Revision
82 svn_revision = int(SVN_REVISION.replace('SVN-', ''))
84 # Here we'll have to find another way of getting the SVN revision
87 admin_emails_xml = '<emails>'
88 for email in get_admin_emails():
89 admin_emails_xml += '<email value="%s" />' % email
90 admin_emails_xml += '</emails>'
92 database_type = get_database_engine()
94 statistics = """<check>
95 <key value="%(site_key)s" />
96 <app_url value="%(app_url)s" />
97 <app_title value="%(app_title)s" />
98 <app_description value="%(app_description)s" />
99 <svn_revision value="%(svn_revision)d" />
100 <views value="%(site_views)d" />
101 <active_users value="%(active_users)d" />
102 <server value="%(server_name)s" />
103 <python_version value="%(python_version)s" />
104 <django_version value="%(django_version)s" />
105 <database value="%(database)s" />
106 <os value="%(os)s" />
109 'site_key' : settings.SITE_KEY,
111 'app_title' : escape(APP_TITLE.value),
112 'app_description' : escape(APP_DESCRIPTION.value),
113 'svn_revision' : svn_revision,
114 'site_views' : get_site_views(),
115 'server_name' : get_server_name(),
116 'active_users' : get_active_users(),
117 'python_version' : ''.join(sys.version.splitlines()),
118 'django_version' : str(DJANGO_VERSION),
119 'database' : database_type,
120 'os' : str(os.uname()),
121 'emails' : admin_emails_xml,
124 # Compress the statistics XML dump
125 statistics_compressed = bz2.compress(statistics)
127 # Pass the compressed statistics to the update server
129 'statistics' : binascii.b2a_base64(statistics_compressed),
131 data = urllib.urlencode(post_data)
133 # We simulate some browser, otherwise the server can return 403 response
134 user_agent = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-us) AppleWebKit/531.21.8 (KHTML, like Gecko) Version/4.0.4 Safari/5'
135 headers={ 'User-Agent' : user_agent,}
138 check_request = urllib2.Request('%s%s' % (settings.UPDATE_SERVER_URL, '/site_check/'), data, headers=headers)
139 check_response = urllib2.urlopen(check_request)
140 content = check_response.read()
141 except urllib2.HTTPError, error:
142 content = error.read()
144 return _("Wasn't able to check to the update server.")
146 # Read the messages from the Update Server
148 messages_xml_url = '%s%s' % (settings.UPDATE_SERVER_URL, '/messages/xml/')
149 messages_request = urllib2.Request(messages_xml_url, headers=headers)
150 messages_response = urllib2.urlopen(messages_request)
151 messages_xml = messages_response.read()
153 return _("Wasn't able to retreive the update messages.")
155 # Store the messages XML in a Setting object
156 settings.UPDATE_MESSAGES_XML.set_value(messages_xml)
158 messages_dom = parseString(messages_xml)
159 messages_count = len(messages_dom.getElementsByTagName('message'))
161 # Set the latest update datetime to now.
162 now = datetime.datetime.now()
163 settings.LATEST_UPDATE_DATETIME.set_value(now)
165 return _('%d update messages have been downloaded.') % messages_count
167 def update_trigger():
168 # Trigger the update process
169 now = datetime.datetime.now()
170 if (now - settings.LATEST_UPDATE_DATETIME) > datetime.timedelta(days=1):
171 update_status = check_for_updates()
173 logging.error(smart_unicode("Update process has been triggered: %s" % update_status))
174 settings.LATEST_UPDATE_DATETIME.set_value(now)