Revision 2a965273 snf-astakos-app/astakos/im/models.py
b/snf-astakos-app/astakos/im/models.py | ||
---|---|---|
1111 | 1111 |
) |
1112 | 1112 |
application = models.OneToOneField(ProjectApplication, related_name='project') |
1113 | 1113 |
creation_date = models.DateTimeField() |
1114 |
last_approval_date = models.DateTimeField() |
|
1115 |
termination_date = models.DateTimeField() |
|
1114 |
last_approval_date = models.DateTimeField(null=True)
|
|
1115 |
termination_date = models.DateTimeField(null=True)
|
|
1116 | 1116 |
members = models.ManyToManyField(AstakosUser, through='ProjectMembership') |
1117 | 1117 |
last_synced_application = models.OneToOneField( |
1118 | 1118 |
ProjectApplication, related_name='last_project', null=True, blank=True |
... | ... | |
1192 | 1192 |
def sync(self): |
1193 | 1193 |
c, rejected = send_quota(self.approved_members) |
1194 | 1194 |
return rejected |
1195 |
|
|
1196 |
def add_member(self, user, request_user=None): |
|
1197 |
if isinstance(user, int): |
|
1198 |
user = _lookup_object(AstakosUser, id=user) |
|
1199 |
if request_user and \ |
|
1200 |
(not self.owner == request_user and not request_user.is_superuser): |
|
1201 |
raise Exception(_(astakos_messages.NOT_ALLOWED)) |
|
1202 |
if not self.is_alive: |
|
1203 |
raise Exception(_(astakos_messages.NOT_ALIVE_PROJECT) % project.__dict__) |
|
1204 |
if self.definition.member_accept_policy == 'closed': |
|
1205 |
raise Exception(_(astakos_messages.MEMBER_ACCEPT_POLICY_CLOSED)) |
|
1206 |
if len(self.approved_members) + 1 > self.limit_on_members_number: |
|
1207 |
raise Exception(_(astakos_messages.MEMBER_NUMBER_LIMIT_REACHED)) |
|
1208 |
created, m = ProjectMembership.objects.get_or_create( |
|
1209 |
person=user, project=project |
|
1210 |
) |
|
1211 |
if m.is_accepted: |
|
1212 |
return |
|
1213 |
if created: |
|
1214 |
m.issue_date = datetime.now() |
|
1215 |
m.is_accepted = True |
|
1216 |
m.decision_date = datetime.now() |
|
1217 |
m.save() |
|
1218 |
notification = build_notification( |
|
1219 |
settings.SERVER_EMAIL, |
|
1220 |
[user.email], |
|
1221 |
_('Your membership on project %(name)s has been accepted.') % project.definition.__dict__, |
|
1222 |
_('Your membership on project %(name)s has been accepted.') % project.definition.__dict__ |
|
1223 |
) |
|
1224 |
notification.send() |
|
1225 |
|
|
1226 |
def remove_member(self, user, request_user=None): |
|
1227 |
if user.is_digit(): |
|
1228 |
user = _lookup_object(AstakosUser, id=user) |
|
1229 |
if request_user and \ |
|
1230 |
(not self.owner == request_user and not request_user.is_superuser): |
|
1231 |
raise Exception(_(astakos_messages.NOT_ALLOWED)) |
|
1232 |
if not self.is_alive: |
|
1233 |
raise Exception(_(astakos_messages.NOT_ALIVE_PROJECT) % project.__dict__) |
|
1234 |
m = _lookup_object(ProjectMembership, person=user, project=project) |
|
1235 |
if not m.is_accepted: |
|
1236 |
return |
|
1237 |
m.is_accepted = False |
|
1238 |
m.decision_date = datetime.now() |
|
1239 |
m.save() |
|
1240 |
notification = build_notification( |
|
1241 |
settings.SERVER_EMAIL, |
|
1242 |
[user.email], |
|
1243 |
_('Your membership on project %(name)s has been removed.') % project.definition.__dict__, |
|
1244 |
_('Your membership on project %(name)s has been removed.') % project.definition.__dict__ |
|
1245 |
) |
|
1246 |
notification.send() |
|
1247 |
|
|
1195 | 1248 |
|
1196 | 1249 |
class ProjectMembership(models.Model): |
1197 | 1250 |
person = models.ForeignKey(AstakosUser) |
1198 | 1251 |
project = models.ForeignKey(Project) |
1199 | 1252 |
issue_date = models.DateField(default=datetime.now()) |
1200 | 1253 |
decision_date = models.DateField(null=True, db_index=True) |
1201 |
is_accepted = models.BooleanField( |
|
1202 |
_('Whether the membership application is accepted'), |
|
1203 |
default=False |
|
1204 |
) |
|
1254 |
is_accepted = models.BooleanField(default=False) |
|
1205 | 1255 |
|
1206 | 1256 |
class Meta: |
1207 | 1257 |
unique_together = ("person", "project") |
... | ... | |
1253 | 1303 |
return o |
1254 | 1304 |
|
1255 | 1305 |
def list_applications(): |
1256 |
return ProjectAppication.objects.all() |
|
1306 |
return ProjectApplication.objects.all()
|
|
1257 | 1307 |
|
1258 | 1308 |
def submit_application(definition, applicant, comments, precursor_application=None, commit=True): |
1259 | 1309 |
if precursor_application: |
... | ... | |
1268 | 1318 |
if commit: |
1269 | 1319 |
definition.save() |
1270 | 1320 |
application.save() |
1271 |
notification = build_notification(
|
|
1321 |
notification = build_notification( |
|
1272 | 1322 |
settings.SERVER_EMAIL, |
1273 | 1323 |
[i[1] for i in settings.ADMINS], |
1274 | 1324 |
_(GROUP_CREATION_SUBJECT) % {'group':application.definition.name}, |
... | ... | |
1277 | 1327 |
notification.send() |
1278 | 1328 |
return application |
1279 | 1329 |
|
1280 |
def approve_application(serial): |
|
1281 |
app = _lookup_object(ProjectAppication, serial=serial) |
|
1282 |
notify = False |
|
1330 |
def approve_application(serial, request_user=None): |
|
1331 |
app = _lookup_object(ProjectApplication, serial=serial) |
|
1283 | 1332 |
if not app.precursor_application: |
1284 | 1333 |
kwargs = { |
1285 | 1334 |
'application':app, |
... | ... | |
1287 | 1336 |
'last_approval_date':datetime.now(), |
1288 | 1337 |
} |
1289 | 1338 |
project = _create_object(Project, **kwargs) |
1339 |
project.add_member(app.owner, request_user) |
|
1290 | 1340 |
else: |
1291 | 1341 |
project = app.precursor_application.project |
1292 | 1342 |
last_approval_date = project.last_approval_date |
... | ... | |
1328 | 1378 |
) |
1329 | 1379 |
return Project.objects.all() |
1330 | 1380 |
|
1331 |
def add_project_member(serial, user_id, request_user): |
|
1332 |
project = _lookup_object(Project, serial=serial) |
|
1333 |
user = _lookup_object(AstakosUser, id=user_id) |
|
1334 |
if not project.owner == request_user: |
|
1335 |
raise Exception(_(astakos_messages.NOT_PROJECT_OWNER)) |
|
1336 |
|
|
1337 |
if not project.is_alive: |
|
1338 |
raise Exception(_(astakos_messages.NOT_ALIVE_PROJECT) % project.__dict__) |
|
1339 |
if len(project.members) + 1 > project.limit_on_members_number: |
|
1340 |
raise Exception(_(astakos_messages.MEMBER_NUMBER_LIMIT_REACHED)) |
|
1341 |
m = self._lookup_object(ProjectMembership, person=user, project=project) |
|
1342 |
if m.is_accepted: |
|
1343 |
return |
|
1344 |
m.is_accepted = True |
|
1345 |
m.decision_date = datetime.now() |
|
1346 |
m.save() |
|
1347 |
notification = build_notification( |
|
1348 |
settings.SERVER_EMAIL, |
|
1349 |
[user.email], |
|
1350 |
_('Your membership on project %(name)s has been accepted.') % project.definition.__dict__, |
|
1351 |
_('Your membership on project %(name)s has been accepted.') % project.definition.__dict__, |
|
1352 |
) |
|
1353 |
notification.send() |
|
1354 |
|
|
1355 |
def remove_project_member(serial, user_id, request_user): |
|
1356 |
project = _lookup_object(Project, serial=serial) |
|
1357 |
if not project.is_alive: |
|
1358 |
raise Exception(_(astakos_messages.NOT_ALIVE_PROJECT) % project.__dict__) |
|
1359 |
if not project.owner == request_user: |
|
1360 |
raise Exception(_(astakos_messages.NOT_PROJECT_OWNER)) |
|
1361 |
user = self.lookup_user(user_id) |
|
1362 |
m = _lookup_object(ProjectMembership, person=user, project=project) |
|
1363 |
if not m.is_accepted: |
|
1364 |
return |
|
1365 |
m.is_accepted = False |
|
1366 |
m.decision_date = datetime.now() |
|
1367 |
m.save() |
|
1368 |
notification = build_notification( |
|
1369 |
settings.SERVER_EMAIL, |
|
1370 |
[user.email], |
|
1371 |
_('Your membership on project %(name)s has been removed.') % project.definition.__dict__, |
|
1372 |
_('Your membership on project %(name)s has been removed.') % project.definition.__dict__ |
|
1373 |
) |
|
1374 |
notification.send() |
|
1375 |
|
|
1376 | 1381 |
def suspend_project(serial): |
1377 | 1382 |
project = _lookup_object(Project, serial=serial) |
1378 | 1383 |
project.suspend() |
Also available in: Unified diff