Package expedient :: Package clearinghouse :: Module utils
[hide private]
[frames] | no frames]

Source Code for Module expedient.clearinghouse.utils

  1  ''' 
  2  Created on Nov 9, 2010 
  3   
  4  @author: jnaous 
  5  ''' 
  6  from datetime import datetime, timedelta 
  7  from django.conf import settings 
  8  from expedient.common.messaging.models import DatedMessage 
  9  from expedient.common.middleware import threadlocals 
 10  from django.contrib.auth.models import User 
 11  from expedient.common.permissions.shortcuts import give_permission_to 
 12  from expedient.clearinghouse.aggregate.models import Aggregate 
 13  from expedient.clearinghouse.project.models import Project 
 14  from expedient.common.tests.client import test_get_and_post_form 
 15  from expedient.clearinghouse.slice.models import Slice 
 16  from expedient.clearinghouse.resources.models import Sliver 
 17  from expedient.clearinghouse.roles.models import ProjectRole 
 18   
19 -def post_message_to_current_user(msg_text, sender=None, 20 msg_type=DatedMessage.TYPE_ANNOUNCE):
21 """Post a message to the user whose request is being processed. 22 23 This function depends on the threadlocals middleware to find the current 24 user, so it must be installed. 25 26 @param msg_text: The message to post for the user. 27 @type msg_text: C{str} 28 @keyword sender: The message sender. Defaults to None. 29 @type sender: C{django.contrib.auth.models.User} 30 @keyword msg_type: The type of the message. Defaults to 31 L{DatedMessage.TYPE_ANNOUNCE}. 32 @type msg_type: C{str} limited to one of L{DatedMessage}.TYPE_* 33 34 """ 35 36 user = threadlocals.get_thread_locals()["user"] 37 DatedMessage.objects.post_message_to_user( 38 msg_text, user, sender, msg_type)
39
40 -def add_dummy_agg_to_test_settings(test):
41 """Adds the dummy aggregate test models to the installed apps of the test. 42 43 C{test} must be a child of L{expedient.common.tests.manager.SettingsTestCase}. 44 45 """ 46 from django.conf import settings 47 test.settings_manager.set( 48 INSTALLED_APPS=settings.INSTALLED_APPS + \ 49 ["expedient.clearinghouse.aggregate.tests"], 50 AGGREGATE_PLUGINS=settings.AGGREGATE_PLUGINS + \ 51 [("expedient.clearinghouse.aggregate.tests.models.DummyAggregate", 52 "dummy_agg", 53 "expedient.clearinghouse.aggregate.tests.urls"), 54 ], 55 DEBUG_PROPAGATE_EXCEPTIONS=True, 56 )
57 58
59 -def create_test_users(test):
60 # create users 61 test.u1 = User.objects.create_user( 62 "user1", "u@u.com", "password") 63 test.u2 = User.objects.create_user( 64 "user2", "u@u.com", "password") 65 test.u3 = User.objects.create_user( 66 "user3", "u@u.com", "password")
67
68 -def give_test_permissions(test):
69 70 create_test_users(test) 71 72 # give permissions 73 give_permission_to("can_add_aggregate", Aggregate, test.u1) 74 give_permission_to("can_create_project", Project, test.u2)
75
76 -def create_test_aggregates(test):
77 78 from expedient.clearinghouse.aggregate.tests.models import DummyAggregate 79 80 give_test_permissions(test) 81 82 test.client.login(username=test.u1.username, password="password") 83 threadlocals.push_frame(user=test.u1) 84 85 test.agg1 = DummyAggregate.objects.create( 86 name="Agg1", 87 ) 88 test.agg1.create_resources() 89 90 test.agg2 = DummyAggregate.objects.create( 91 name="Agg2", 92 ) 93 test.agg2.create_resources() 94 95 # give permissions to use aggregates 96 give_permission_to("can_use_aggregate", test.agg1, test.u2) 97 give_permission_to("can_use_aggregate", test.agg2, test.u2) 98 99 test.client.logout() 100 threadlocals.pop_frame()
101
102 -def create_test_project(test):
103 104 create_test_aggregates(test) 105 106 test.client.login(username=test.u2.username, password="password") 107 threadlocals.push_frame(user=test.u2) 108 109 # create the project 110 Project.objects.all().delete() 111 test_get_and_post_form( 112 client=test.client, 113 url=Project.get_create_url(), 114 params={"name": "project name", "description": "project description"}, 115 ) 116 test.project = Project.objects.all()[0] 117 118 test.client.logout() 119 threadlocals.pop_frame()
120
121 -def add_test_project_member(test):
122 123 create_test_project(test) 124 125 test.client.login(username=test.u2.username, password="password") 126 threadlocals.push_frame(user=test.u2) 127 128 # add a member 129 researcher = ProjectRole.objects.get(project=test.project, name="researcher") 130 test_get_and_post_form( 131 client=test.client, 132 url=test.project.get_member_add_url(), 133 params={"user": test.u3.id, "roles": researcher.id}, 134 ) 135 136 test.assertEqual(test.project.owners.count(), 1) 137 test.assertEqual(test.project.members.count(), 2) 138 139 test.client.logout() 140 threadlocals.pop_frame()
141
142 -def add_test_aggregate_to_project(test):
143 144 add_test_project_member(test) 145 146 test.client.login(username=test.u2.username, password="password") 147 threadlocals.push_frame(user=test.u2) 148 149 # add the aggregate to the project 150 test_get_and_post_form( 151 client=test.client, 152 url=test.project.get_agg_add_url(), 153 params={"id": "%s" % test.agg1.id}, 154 ) 155 test_get_and_post_form( 156 client=test.client, 157 url=test.project.get_agg_add_url(), 158 params={"id": "%s" % test.agg2.id}, 159 ) 160 test.assertEqual(test.project.aggregates.count(), 2) 161 162 test.client.logout() 163 threadlocals.pop_frame()
164
165 -def create_test_slice(test):
166 167 add_test_aggregate_to_project(test) 168 169 test.client.login(username=test.u2.username, password="password") 170 threadlocals.push_frame(user=test.u2) 171 172 # create the slice 173 Slice.objects.all().delete() 174 175 expiration = datetime.now() + timedelta(days=settings.MAX_SLICE_LIFE - 5) 176 177 test_get_and_post_form( 178 client=test.client, 179 url=Slice.get_create_url(proj_id=test.project.id), 180 params={ 181 "name": "slice name", 182 "description": "slice description", 183 "expiration_date_0": "%s" % expiration.date(), 184 "expiration_date_1": expiration.time().strftime("%H:%m:%S"), 185 }, 186 ) 187 test.slice = Slice.objects.all()[0] 188 189 test.client.logout() 190 threadlocals.pop_frame()
191
192 -def add_test_aggregate_to_slice(test):
193 194 create_test_slice(test) 195 196 test.client.login(username=test.u2.username, password="password") 197 threadlocals.push_frame(user=test.u2) 198 199 # add the aggregate to the slice 200 test_get_and_post_form( 201 client=test.client, 202 url=test.slice.get_agg_add_url(), 203 params={"id": "%s" % test.agg1.id}, 204 ) 205 test_get_and_post_form( 206 client=test.client, 207 url=test.slice.get_agg_add_url(), 208 params={"id": "%s" % test.agg2.id}, 209 ) 210 test.assertEqual(test.slice.aggregates.count(), 2) 211 212 test.client.logout() 213 threadlocals.pop_frame()
214
215 -def add_resources_to_test_slice(test):
216 217 from expedient.clearinghouse.aggregate.tests.models import DummyResource 218 219 add_test_aggregate_to_slice(test) 220 221 test.client.login(username=test.u2.username, password="password") 222 threadlocals.push_frame(user=test.u2) 223 224 # add resources to the slice 225 for agg in [test.agg1, test.agg2]: 226 for rsc in DummyResource.objects.filter(aggregate=agg): 227 Sliver.objects.create(resource=rsc, slice=test.slice) 228 229 test.client.logout() 230 threadlocals.pop_frame()
231
232 -def start_test_slice(test):
233 """Create a test setup with aggregates, users, project, and started slice. 234 235 Creates two users, test.u1 and test.u2. Gives test.u1 permission to 236 create aggregates. Creates two dummy aggregates with resources using u1. 237 238 Gives u2 permission to create project, creates project with u2, add u1 as 239 researcher member, creates a slice, add all dummy resources to slice, 240 and starts it. 241 242 C{test} must be a child of L{expedient.common.tests.manager.SettingsTestCase}. 243 244 """ 245 246 add_resources_to_test_slice(test) 247 248 test.client.login(username=test.u2.username, password="password") 249 threadlocals.push_frame(user=test.u2) 250 251 # start the slice 252 test_get_and_post_form( 253 client=test.client, 254 url=test.slice.get_start_url(), 255 params={}, 256 ) 257 test.slice = Slice.objects.get(pk=test.slice.pk) 258 test.assertTrue(test.slice.started) 259 260 test.client.logout() 261 threadlocals.pop_frame()
262