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

b/snf-astakos-app/astakos/im/models.py
1473 1473
        return "application %s (%s) for project %s" % (
1474 1474
            self.id, self.name, self.chain)
1475 1475

  
1476
    def get_project(self):
1477
        try:
1478
            project = Project.objects.get(id=self.chain, state=Project.APPROVED)
1479
            return Project
1480
        except Project.DoesNotExist, e:
1481
            return None
1482

  
1483 1476
    def state_display(self):
1484 1477
        return self.APPLICATION_STATE_DISPLAY.get(self.state, _('Unknown'))
1485 1478

  
......
1579 1572
    def project_exists(self):
1580 1573
        return self.get_project() is not None
1581 1574

  
1582
    def _get_project_for_update(self):
1583
        try:
1584
            objects = Project.objects
1585
            project = objects.get_for_update(id=self.chain)
1586
            return project
1587
        except Project.DoesNotExist:
1588
            return None
1589

  
1590 1575
    def can_cancel(self):
1591 1576
        return self.state == self.PENDING
1592 1577

  
......
1629 1614
        return self.state == self.PENDING
1630 1615

  
1631 1616
    def approve(self, reason):
1632
        new_project_name = self.name
1633 1617
        if not self.can_approve():
1634 1618
            m = _("cannot approve: project '%s' in state '%s'") % (
1635
                    new_project_name, self.state)
1619
                    self.name, self.state)
1636 1620
            raise AssertionError(m) # invalid argument
1637 1621

  
1638 1622
        now = datetime.now()
1639
        project = self._get_project_for_update()
1640

  
1641
        try:
1642
            q = Q(name=new_project_name) & ~Q(state=Project.TERMINATED)
1643
            conflicting_project = Project.objects.get(q)
1644
            if (conflicting_project != project):
1645
                m = (_("cannot approve: project with name '%s' "
1646
                       "already exists (id: %s)") % (
1647
                        new_project_name, conflicting_project.id))
1648
                raise PermissionDenied(m) # invalid argument
1649
        except Project.DoesNotExist:
1650
            pass
1623
        self.state = self.APPROVED
1624
        self.response_date = now
1625
        self.response = reason
1626
        self.save()
1651 1627

  
1652
        new_project = False
1628
        project = self.get_project()
1653 1629
        if project is None:
1654
            new_project = True
1655 1630
            project = Project(id=self.chain)
1656 1631

  
1657
        project.name = new_project_name
1632
        project.name = self.name
1658 1633
        project.application = self
1659 1634
        project.last_approval_date = now
1660

  
1661 1635
        project.save()
1662

  
1663
        self.state = self.APPROVED
1664
        self.response_date = now
1665
        self.response = reason
1666
        self.save()
1667 1636
        return project
1668 1637

  
1669 1638
    @property

Also available in: Unified diff