Permalink
Browse files

Upgrade to Django 1.4

Introduces requirements.txt, removes remnants of
simplejson and updates README.txt.  Also removes
stale import_member_data.py which was used for
importing the initial data to the system.
  • Loading branch information...
1 parent 09e6056 commit b04adc5f7964ecc2efa99de199b41678cef6c46a @guaq guaq committed Apr 14, 2012
Showing with 25 additions and 200 deletions.
  1. +3 −3 README.txt
  2. +2 −4 install-virtualenv.sh
  3. +8 −8 membership/tests.py
  4. +8 −9 membership/views.py
  5. +1 −5 munin_plugins/sikteeri_memberships
  6. +3 −3 services/views.py
  7. +0 −168 sikteeri/import_member_data.py
View
6 README.txt
@@ -1,9 +1,9 @@
REQUIREMENTS
============
-* django (1.2, known not to work on older versions)
-* simplejson
-* pysqlite, if using Python < 2.5
+* Django 1.4 (as of 2012/04/14)
+* Python 2.6
+* gunicorn for production deployment
* gettext
The virtualenv install script installs everything but the Python sqlite
View
6 install-virtualenv.sh
@@ -32,17 +32,15 @@ function fatal () {
test -a $ENVDIR && fatal "$ENVDIR already exists"
-$VIRTUALENV $ENVDIR || fatal "Failed to create virtualenv $ENVDIR"
+$VIRTUALENV $ENVDIR --no-site-packages || fatal "Failed to create virtualenv $ENVDIR"
source $ENVDIR/bin/activate
if [[ ! -a $ENVDIR/bin/pip ]]; then
$ENVDIR/bin/easy_install pip || fatal "Could not install pip in virtualenv"
fi
-for pkg in django simplejson; do
- $ENVDIR/bin/pip install $pkg || fatal "Could not install $pkg in $ENVDIR"
-done
+pip install -r requirements.txt
echo "export PYTHONPATH=.." >> $ENVDIR/bin/activate
View
16 membership/tests.py
@@ -9,7 +9,7 @@
from decimal import Decimal
from datetime import datetime, timedelta
from random import randint
-import simplejson
+import json
from django.contrib.auth.models import User
from django.core import mail
@@ -746,10 +746,10 @@ def test_clean_ajax_output(self):
login = self.client.login(username='admin', password='dhtn')
self.failUnless(login, 'Could not log in')
json_response = self.client.post('/membership/application/handle_json/',
- simplejson.dumps({"requestType": "MEMBERSHIP_DETAIL", "payload": new.id}),
- content_type="application/json")
+ json.dumps({"requestType": "MEMBERSHIP_DETAIL", "payload": new.id}),
+ content_type="application/json")
self.assertEqual(json_response.status_code, 200)
- json_dict = simplejson.loads(json_response.content)
+ json_dict = json.loads(json_response.content)
self.assertEqual(json_dict['contacts']['person']['first_name'],
u'&lt;b&gt;Yrjö&lt;/b&gt;')
self.assertEqual(json_dict['extra_info'],
@@ -758,10 +758,10 @@ def test_clean_ajax_output(self):
def _validate_alias(self, alias):
json_response = self.client.post('/membership/application/handle_json/',
- simplejson.dumps({"requestType": "VALIDATE_ALIAS", "payload": alias}),
- content_type="application/json")
+ json.dumps({"requestType": "VALIDATE_ALIAS", "payload": alias}),
+ content_type="application/json")
self.assertEqual(json_response.status_code, 200)
- json_dict = simplejson.loads(json_response.content)
+ json_dict = json.loads(json_response.content)
return json_dict
def test_validate_alias_ajax(self):
@@ -930,7 +930,7 @@ def tearDown(self):
def test_membership_statuses(self):
response = self.client.get('/membership/metrics/')
self.assertEqual(response.status_code, 200)
- d = simplejson.loads(response.content)
+ d = json.loads(response.content)
self.assertTrue(d.has_key(u'memberships'))
for key in [u'new', u'preapproved', u'approved', u'deleted']:
self.assertTrue(d[u'memberships'].has_key(key))
View
17 membership/views.py
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
import logging
+import json
from sikteeri import settings
from membership.models import Contact, Membership, MEMBER_TYPES_DICT, Bill,\
BillingCycle, Payment
@@ -19,8 +20,6 @@
from django.http import HttpResponse, HttpResponseForbidden
from django.contrib import messages
-import simplejson
-
from services.models import Alias, Service, ServiceType
from forms import PersonApplicationForm, OrganizationApplicationForm, PersonContactForm, LoginField, ServiceForm
@@ -748,15 +747,15 @@ def membership_detail_json(request, id):
membership = get_object_or_404(Membership, id=id)
#sleep(1)
json_obj = serializable_membership_info(membership)
- return HttpResponse(simplejson.dumps(json_obj, sort_keys=True, indent=4),
+ return HttpResponse(json.dumps(json_obj, sort_keys=True, indent=4),
mimetype='application/json')
- # return HttpResponse(simplejson.dumps(json_obj, sort_keys=True, indent=4),
- # mimetype='text/plain')
+ # return HttpResponse(json.dumps(json_obj, sort_keys=True, indent=4),
+ # mimetype='text/plain')
# Public access
def handle_json(request):
logger.debug("RAW POST DATA: %s" % request.raw_post_data)
- msg = simplejson.loads(request.raw_post_data)
+ msg = json.loads(request.raw_post_data)
funcs = {'PREAPPROVE': membership_preapprove_json,
'APPROVE': membership_approve_json,
'MEMBERSHIP_DETAIL': membership_detail_json,
@@ -812,7 +811,7 @@ def membership_metrics(request):
'unpaid_sum': float(unpaid_sum),
},
}
- return HttpResponse(simplejson.dumps(d, sort_keys=True, indent=4),
+ return HttpResponse(json.dumps(d, sort_keys=True, indent=4),
mimetype='application/json')
@trusted_host_required
@@ -824,14 +823,14 @@ def public_memberlist(request):
@trusted_host_required
def unpaid_members(request):
json_obj = unpaid_members_data()
- return HttpResponse(simplejson.dumps(json_obj, sort_keys=True, indent=4),
+ return HttpResponse(json.dumps(json_obj, sort_keys=True, indent=4),
mimetype='application/json')
@trusted_host_required
def admtool_membership_detail_json(request, id):
membership = get_object_or_404(Membership, id=id)
json_obj = admtool_membership_details(membership)
- return HttpResponse(simplejson.dumps(json_obj, sort_keys=True, indent=4),
+ return HttpResponse(json.dumps(json_obj, sort_keys=True, indent=4),
mimetype='application/json')
@trusted_host_required
View
6 munin_plugins/sikteeri_memberships
@@ -4,13 +4,9 @@
from os import environ as env
import sys
import urllib2
+import json
from urllib2 import HTTPError, URLError
-try:
- import simplejson
- json = simplejson
-except Exception, e:
- import json
def httpreq(url):
req = urllib2.Request(url)
View
6 services/views.py
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
import re
-import simplejson
+import json
import logging
logger = logging.getLogger("services.views")
from membership.utils import log_change
@@ -107,5 +107,5 @@ def validate_alias(request, alias):
if re.match(VALID_USERNAME_RE, alias) == None:
valid = False
json_obj = {'exists' : exists, 'valid' : valid}
- return HttpResponse(simplejson.dumps(json_obj, sort_keys=True, indent=4),
- mimetype='application/json')
+ return HttpResponse(json.dumps(json_obj, sort_keys=True, indent=4),
+ mimetype='application/json')
View
168 sikteeri/import_member_data.py
@@ -1,168 +0,0 @@
-#!/usr/bin/env python
-# encoding: utf-8
-"""
-import_member_data.py
-
-Copyright (c) 2010 Kapsi Internet-käyttäjät ry. All rights reserved.
-"""
-from __future__ import with_statement
-
-import sys
-import os
-from datetime import datetime, timedelta
-from optparse import OptionParser
-from services.models import Alias
-
-os.environ['DJANGO_SETTINGS_MODULE'] = 'sikteeri.settings'
-sys.path.insert(0, '..')
-
-from django.conf import settings
-import logging
-logger = logging.getLogger("import_member_data")
-
-from django.contrib.auth.models import User
-from membership.models import Contact, Membership, Bill, BillingCycle
-from membership.utils import log_change
-
-user = User.objects.get(id=1)
-
-def create_member(mdata, logins):
- # legacy fields
- # ['application_id', 'sendinfo', 'memberclass', 'applicationtime', 'sms',
- # 'id', 'email', 'website', 'publicwebsite', 'lastname', 'phone',
- # 'firstnames', 'address', 'nationality', 'post', 'removed', 'publicname',
- # 'name', 'mobile', 'residence', 'time', 'publicemail', 'period_start',
- # 'period_end']
- # TODO: latest billing period start date?
- post_index = mdata['post'].find(' ')
- postcode = mdata['post'][:post_index]
- postoffice = mdata['post'][post_index+1:]
- d = {
- 'street_address' : mdata['address'],
- 'postal_code' : postcode,
- 'post_office' : postoffice,
- 'country' : mdata['nationality'],
- 'phone' : mdata['phone'].replace(" ", "").replace("-", ""),
- 'sms' : mdata['sms'].replace(" ", "").replace("-", ""),
- 'email' : mdata['email'].strip(" "),
- 'homepage' : mdata['website'].strip(" "),
- 'first_name' : mdata['name'].strip(" "),
- 'given_names' : mdata['firstnames'].strip(" "),
- 'last_name' : mdata['lastname'].strip(" "),
- # mdata['application_id'],
- # mdata['sendinfo'],
- }
-
- # Hide non-public websites
- if not mdata['publicwebsite']:
- d['homepage'] = ""
-
- if not mdata['memberclass']:
- mtype = 'P'
- print >> sys.stderr, "# Member type missing for member %d" % mdata['id']
- elif mdata['memberclass'] == 'member':
- mtype = 'P'
- elif mdata['memberclass'] == 'supporting':
- mtype = 'S'
- elif mdata['memberclass'] == 'organization':
- mtype = 'O'
- elif mdata['memberclass'] == 'honorary':
- mtype = 'H'
- else:
- print >> sys.stderr, "! Not importing, member class unknown for member %d" % mdata['id']
- return False
-
- contact = Contact(**d)
- contact.save()
- if mtype == 'O':
- membership = Membership(id=mdata['id'], type=mtype, status='A',
- created=datetime.utcfromtimestamp(mdata['time']),
- approved=datetime.utcfromtimestamp(mdata['time']),
- organization=contact,
- nationality=mdata['nationality'],
- municipality=mdata['residence'],
- extra_info='Imported from legacy',
- public_memberlist=bool(mdata['publicname']))
- else:
- membership = Membership(id=mdata['id'], type=mtype, status='A',
- created=datetime.utcfromtimestamp(mdata['time']),
- approved=datetime.utcfromtimestamp(mdata['time']),
- person=contact,
- nationality=mdata['nationality'],
- municipality=mdata['residence'],
- extra_info='Imported from legacy',
- public_memberlist=bool(mdata['publicname']))
- logger.info("Member %s imported from legacy database." % (unicode(contact)))
- membership.save()
-
- # Create a period only if there already is one previously. Else let
- # makebills create one.
- if mdata.has_key('period_start'):
- billing_cycle = BillingCycle(membership=membership, is_paid=False,
- start=datetime.strptime(mdata['period_start'], "%Y-%m-%d %H:%M:%S"),
- end=datetime.strptime(mdata['period_end'], "%Y-%m-%d %H:%M:%S")+timedelta(days=1))
- billing_cycle.save()
- bill = Bill(billingcycle=billing_cycle)
- bill.save()
- # Due to auto_now_add, need to save first before changing
- bill.created=datetime.strptime(mdata['bill_creation'], "%Y-%m-%d %H:%M:%S")
- bill.due_date=datetime.strptime(mdata['bill_dueday'], "%Y-%m-%d %H:%M:%S")
- bill.save()
- for alias in mdata['aliases']:
- if alias in logins:
- a = Alias(owner=membership, name=alias, account=True,
- created=membership.created)
- else:
- a = Alias(owner=membership, name=alias, account=False,
- created=membership.created)
- a.save()
-
- account_alias_count = Alias.objects.filter(owner=a.owner, account=True).count()
- if account_alias_count > 1:
- logger.warning("%i account aliases for %s" % (account_alias_count, a.owner))
-
- log_change(membership, user, change_message="Imported into system")
- return True
-
-
-def main(filename, login_filename):
- logins = set()
- with open(login_filename, 'r') as f:
- for line in f:
- logins.add(line.replace('\n', ''))
- logger.info('Read %i logins' % len(logins))
-
- import simplejson
- members = simplejson.load(open(filename, 'r'))
- print >> sys.stderr, "# %i items, starting processing." % len(members)
- processed = 0
- for mid, mdata in members.iteritems():
- assert mid == str(mdata['id'])
- create_member(mdata, logins)
- processed = processed + 1
- if processed % 100 == 0:
- print >> sys.stderr, "# %i items processed" % processed
-
- if 'postgresql' in settings.DATABASES['default']['ENGINE']:
- from django.db import connection, transaction
- cursor = connection.cursor()
- cursor.execute("SELECT setval('membership_membership_id_seq', max(id)) FROM membership_membership;")
- transaction.commit_unless_managed()
-
- print >> sys.stderr, "# %i items in total processed" % processed
-
-
-if __name__ == '__main__':
- parser = OptionParser()
- parser.add_option("-f", "--file", dest="filename",
- help="read member data from FILE", metavar="FILE")
- parser.add_option("-l", "--login-file", dest="login_filename",
- help="read logins ($ getent passwd|cut -d':' -f 1 > file) from FILE to set unix_account for aliases", metavar="FILE")
- (options, args) = parser.parse_args()
-
- if not options.filename or not options.login_filename:
- parser.print_help()
- sys.exit(1)
- if not os.path.isfile(options.filename):
- parser.error("File '%s' not found." % options.filename)
- main(options.filename, options.login_filename)

0 comments on commit b04adc5

Please sign in to comment.