16 from xml.dom.minidom import parse, parseString
17 from forum.base import get_database_engine
18 from forum.models import Question, Answer, Comment, User
19 from forum.settings import APP_URL, SVN_REVISION, APP_TITLE, APP_DESCRIPTION
20 from django import VERSION as DJANGO_VERSION
21 from django.utils import simplejson
22 from django.utils.html import escape
23 from django.utils.encoding import smart_unicode
24 from django.conf import settings as django_settings
25 from django.utils.translation import ugettext as _
28 def generate_installation_key():
29 gen = lambda length: "".join( [random.choice(string.digits+string.letters) for i in xrange(length)])
30 return '%s-%s-%s-%s' % (gen(4), gen(4), gen(4), gen(4))
32 # To get the site views count we get the SUM of all questions views.
36 # Go through all questions and increase the views count
37 for question in Question.objects.all():
38 views += question.view_count
42 # Gets the active users count since the last visit
43 def get_active_users():
47 if settings.LATEST_UPDATE_DATETIME:
48 users_count = User.objects.filter(last_login__gt=settings.LATEST_UPDATE_DATETIME).count()
54 def get_server_name():
59 request = urllib2.Request(url)
60 response = urllib2.urlopen(request)
62 # Get the response information
63 response_info = response.info()
65 server_name = re.findall("Server: (?P<server_name>.*)$", str(response_info))[0]
66 server_name = ''.join(server_name.splitlines())
72 def get_admin_emails():
75 for user in User.objects.filter(is_superuser=True):
76 emails.append(user.email)
80 def check_for_updates():
81 # Get the SVN Revision
83 svn_revision = int(SVN_REVISION.replace('SVN-', ''))
85 # Here we'll have to find another way of getting the SVN revision
88 admin_emails_xml = '<emails>'
89 for email in get_admin_emails():
90 admin_emails_xml += '<email value="%s" />' % email
91 admin_emails_xml += '</emails>'
93 database_type = get_database_engine()
95 statistics = """<check>
96 <key value="%(site_key)s" />
97 <app_url value="%(app_url)s" />
98 <app_title value="%(app_title)s" />
99 <app_description value="%(app_description)s" />
100 <svn_revision value="%(svn_revision)d" />
101 <views value="%(site_views)d" />
102 <questions_count value="%(questions_count)d" />
103 <answers_count value="%(answers_count)d" />
104 <comments_count value="%(comments_count)d" />
105 <active_users value="%(active_users)d" />
106 <server value="%(server_name)s" />
107 <python_version value="%(python_version)s" />
108 <django_version value="%(django_version)s" />
109 <database value="%(database)s" />
110 <os value="%(os)s" />
113 'site_key' : settings.SITE_KEY,
115 'app_title' : escape(APP_TITLE.value),
116 'app_description' : escape(APP_DESCRIPTION.value),
117 'svn_revision' : svn_revision,
118 'site_views' : get_site_views(),
119 'server_name' : get_server_name(),
120 'questions_count' : Question.objects.filter_state(deleted=False).count(),
121 'answers_count' : Answer.objects.filter_state(deleted=False).count(),
122 'comments_count' : Comment.objects.filter_state(deleted=False).count(),
123 'active_users' : get_active_users(),
124 'python_version' : ''.join(sys.version.splitlines()),
125 'django_version' : str(DJANGO_VERSION),
126 'database' : database_type,
127 'os' : str(platform.uname()),
128 'emails' : admin_emails_xml,
131 # Compress the statistics XML dump
132 statistics_compressed = bz2.compress(statistics)
134 # Pass the compressed statistics to the update server
136 'statistics' : binascii.b2a_base64(statistics_compressed),
138 data = urllib.urlencode(post_data)
140 # We simulate some browser, otherwise the server can return 403 response
141 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'
142 headers={ 'User-Agent' : user_agent,}
145 check_request = urllib2.Request('%s%s' % (settings.UPDATE_SERVER_URL, '/site_check/'), data, headers=headers)
146 check_response = urllib2.urlopen(check_request)
147 content = check_response.read()
148 except urllib2.HTTPError, error:
149 content = error.read()
151 return _("Wasn't able to check to the update server.")
153 # Read the messages from the Update Server
155 messages_xml_url = '%s%s' % (settings.UPDATE_SERVER_URL, '/messages/xml/')
156 messages_request = urllib2.Request(messages_xml_url, headers=headers)
157 messages_response = urllib2.urlopen(messages_request)
158 messages_xml = messages_response.read()
160 return _("Wasn't able to retreive the update messages.")
162 # Store the messages XML in a Setting object
163 settings.UPDATE_MESSAGES_XML.set_value(messages_xml)
165 messages_dom = parseString(messages_xml)
166 messages_count = len(messages_dom.getElementsByTagName('message'))
168 # Set the latest update datetime to now.
169 now = datetime.datetime.now()
170 settings.LATEST_UPDATE_DATETIME.set_value(now)
172 return _('%d update messages have been downloaded.') % messages_count
174 def update_trigger():
175 # Trigger the update process
176 now = datetime.datetime.now()
177 if (now - settings.LATEST_UPDATE_DATETIME) > datetime.timedelta(days=1):
178 update_status = check_for_updates()
180 logging.error(smart_unicode("Update process has been triggered: %s" % update_status))
181 settings.LATEST_UPDATE_DATETIME.set_value(now)