def testBasicRegistration(self): path = os.path.dirname(__file__) data_path = os.path.join(path, "data") # Make sure there's nothing to start self.assertEqual(0, Phone.objects.count()) # submit the xml submission = populate("reg.xml", domain=self.domain, path=data_path) # should create a phone and user object self.assertEqual(1, Phone.objects.count()) self.assertEqual(1, PhoneUserInfo.objects.count()) # sanity check the data phone = Phone.objects.all()[0] user_info = PhoneUserInfo.objects.all()[0] django_user = user_info.user # phone self.assertEqual("67QQ86GVH8CCDNSCL0VQVKF7A", phone.device_id) # phone user info self.assertEqual(phone, user_info.phone) self.assertEqual(submission.attachments.all()[0], user_info.attachment) self.assertEqual("phone_registered", user_info.status) self.assertEqual("test_registration", user_info.username) self.assertEqual("1982", user_info.password) self.assertEqual("BXPKZLP49P3DDTJH3W0BRM2HV", user_info.uuid) self.assertEqual(date(2010,03,23), user_info.registered_on) expected_data = {"sid": "18", "hcbpid": "29", "district": "district 9", "region": "ally", "ward": "collins"} additional_data = user_info.additional_data for key in expected_data: self.assertTrue(key in additional_data, "Key %s should be set in the additional data" % key) self.assertEqual(expected_data[key], additional_data[key], "Value for %s was %s but should be %s" % \ (key, additional_data[key], expected_data[key])) for key in additional_data: self.assertTrue(key in expected_data, "Extra key %s was found in submitted data" % key) # django user self.assertNotEqual(None, django_user) self.assertEqual("test_registration", django_user.username) user_domains = Domain.active_for_user(django_user) self.assertEqual(1, user_domains.count()) self.assertEqual(self.domain, user_domains.all()[0]) # also, make sure we created an instance of the right handler way_handled = SubmissionHandlingOccurrence.objects.get\ (submission=user_info.attachment.submission) self.assertEqual(APP_NAME, way_handled.handled.app) self.assertEqual(REGISTRATION_HANDLER, way_handled.handled.method)
def select( request, redirect_field_name = REDIRECT_FIELD_NAME, domain_select_template = 'domain/select.html' ): domains_for_user = Domain.active_for_user(request.user) if len(domains_for_user) == 0: vals = dict( error_msg = "You are not a member of any existing domains - please contact your system administrator", show_homepage_link = False ) return render_to_response('error.html', vals, context_instance = RequestContext(request)) redirect_to = request.REQUEST.get(redirect_field_name, '') if request.method == 'POST': # If the form has been submitted... form = DomainSelectionForm(domain_list=domains_for_user, data=request.POST) # A form bound to the POST data if form.is_valid(): # We've just checked the submitted data against a freshly-retrieved set of domains # associated with the user. It's safe to set the domain in the sesssion (and we'll # check again on views validated with the domain-checking decorator) form.save(request) # Needs request because it saves domain in session # Weak attempt to give user a good UX - make sure redirect_to isn't garbage. if not redirect_to or '//' in redirect_to or ' ' in redirect_to: redirect_to = reverse('homepage') return HttpResponseRedirect(redirect_to) # Redirect after POST else: # An unbound form form = DomainSelectionForm( domain_list=domains_for_user ) vals = dict( next = redirect_to, form = form ) return render_to_response(domain_select_template, vals, context_instance = RequestContext(request))
def index(request): if not request.user.is_authenticated(): return HttpResponseRedirect('/ui/login/?next=%s' % request.path) domain_list = Domain.active_for_user(request.user) # if the user has only 1 active domain, skip the select domain page if domain_list != None and len(domain_list) == 1: return HttpResponseRedirect('/ui/map/%d' % domain_list[0].id) else: # otherwise, send on to select domain return HttpResponseRedirect('/ui/select-domain/')
def uilogin(request): log_start_time = datetime.utcnow() if request.user.is_authenticated(): return HttpResponseRedirect('/ui/select-domain') error = None message = None if request.GET.has_key('action') and request.GET.get('action') == 'logged_out': message = "You have been logged out." if request.POST.has_key('txtUsername') and request.POST.has_key('txtPassword'): username = request.POST.get('txtUsername') password = request.POST.get('txtPassword') user = authenticate(username=username, password=password) if user is not None: if user.is_active: login(request, user) log_entry = LogEntry() log_entry.user = user log_entry.timestamp = log_start_time log_entry.event_type = 'login' log_entry.set_processing_time(log_start_time) log_entry.save() if request.GET.has_key('next'): return HttpResponseRedirect(request.GET.get('next')) else: domain_list = Domain.active_for_user(request.user) # if the user has only 1 active domain, skip the select domain page if domain_list != None and len(domain_list) == 1: return HttpResponseRedirect('/ui/map/%d' % domain_list[0].id) else: # otherwise, send on to select domain return HttpResponseRedirect('/ui/select-domain/') # Redirect to a success page. else: # Return a 'disabled account' error message error = 'This account is disabled' else: # Return an 'invalid login' error message. error = 'Invalid login' # if there's an error to display, clear the message to avoid confusion if error != None: message = None return render_to_response('ui-login.html', {'user' : request.user, 'error' : error, 'message' : message})
def process_view(self, request, view_func, view_args, view_kwargs): user = request.user # Lookup is done via the ContentTypes framework, stored in the domain_membership table # id(user) == id(request.user), so we can save a lookup into request by using 'user' alone active_domains = Domain.active_for_user(user) user.active_domains = active_domains user.selected_domain = None # default case domain_from_session = request.session.get(_SESSION_KEY_SELECTED_DOMAIN, None) if domain_from_session and domain_from_session in active_domains: user.selected_domain = domain_from_session if not domain_from_session and len(active_domains) == 1: request.session[_SESSION_KEY_SELECTED_DOMAIN] = active_domains[0] user.selected_domain = active_domains[0] return None
def domain_flat(report_schedule, run_frequency): '''A report that shows, per user, how many forms were submitted over time, for a single domain (the domain of the associated user)''' domains = Domain.active_for_user(report_schedule.recipient_user) title = "Domain Report - %s" % (", ".join([domain.name for domain in domains])) return _catch_all_report(report_schedule, run_frequency, domains, title)