Revision 3e3743f2 snf-astakos-app/astakos/im/models.py

b/snf-astakos-app/astakos/im/models.py
1231 1231

  
1232 1232
    def user_visible_by_chain(self, flt):
1233 1233
        model = self.model
1234
        pending = self.filter(model.Q_PENDING).values_list('chain')
1234
        pending = self.filter(model.Q_PENDING | model.Q_DENIED).values_list('chain')
1235 1235
        approved = self.filter(model.Q_APPROVED).values_list('chain')
1236 1236
        by_chain = dict(pending.annotate(models.Max('id')))
1237 1237
        by_chain.update(approved.annotate(models.Max('id')))
......
1326 1326
    # Compiled queries
1327 1327
    Q_PENDING  = Q(state=PENDING)
1328 1328
    Q_APPROVED = Q(state=APPROVED)
1329
    Q_DENIED   = Q(state=DENIED)
1329 1330

  
1330 1331
    class Meta:
1331 1332
        unique_together = ("chain", "id")
......
1396 1397
            uplimit = p.get('uplimit', 0)
1397 1398
            self.add_resource_policy(service, resource, uplimit)
1398 1399

  
1399
    def followers(self):
1400
        followers = self.chained_applications()
1401
        followers = followers.exclude(id=self.pk).filter(state=self.PENDING)
1402
        followers = followers.order_by('id')
1403
        return followers
1400
    def pending_modifications(self):
1401
        q = self.chained_applications()
1402
        q = q.filter(~Q(id=self.id) & Q(state=self.PENDING))
1403
        q = q.order_by('id')
1404
        return q
1404 1405

  
1405
    def last_follower(self):
1406
    def last_pending(self):
1406 1407
        try:
1407
            return self.followers().order_by('-id')[0]
1408
            return self.pending_modifications().order_by('-id')[0]
1408 1409
        except IndexError:
1409 1410
            return None
1410 1411

  
1411 1412
    def is_modification(self):
1413
        if self.state != self.PENDING:
1414
            return False
1412 1415
        parents = self.chained_applications().filter(id__lt=self.id)
1413 1416
        parents = parents.filter(state__in=[self.APPROVED])
1414 1417
        return parents.count() > 0
......
1417 1420
        return ProjectApplication.objects.filter(chain=self.chain)
1418 1421

  
1419 1422
    def has_pending_modifications(self):
1420
        return bool(self.last_follower())
1423
        return bool(self.last_pending())
1421 1424

  
1422 1425
    def get_project(self):
1423 1426
        try:
......
1530 1533
    def member_leave_policy_display(self):
1531 1534
        return PROJECT_MEMBER_LEAVE_POLICIES.get(str(self.member_leave_policy))
1532 1535

  
1533
def submit_application(**kw):
1534

  
1535
    resource_policies = kw.pop('resource_policies', None)
1536
    application = ProjectApplication(**kw)
1537

  
1538
    precursor = kw['precursor_application']
1539

  
1540
    if precursor is None:
1541
        application.chain = new_chain()
1542
    else:
1543
        application.chain = precursor.chain
1544
        if precursor.state == ProjectApplication.PENDING:
1545
            precursor.state = ProjectApplication.REPLACED
1546
            precursor.save()
1547

  
1548
    application.save()
1549
    application.resource_policies = resource_policies
1550
    return application
1551

  
1552 1536
class ProjectResourceGrant(models.Model):
1553 1537

  
1554 1538
    resource                =   models.ForeignKey(Resource)

Also available in: Unified diff