#
# Copyright (c) 2009 rPath, Inc.
#
# All Rights Reserved
#

from mint.rest import errors
from mint.rest.api import models

class ReleaseManager(object):
    def __init__(self, cfg, db, auth):
        self.cfg = cfg
        self.db = db
        self.auth = auth

    def listReleasesForProduct(self, fqdn):
        hostname = fqdn.split('.')[0]
        cu = self.db.cursor()
        sql = '''
        SELECT pubReleaseId as releaseId, Projects.hostname,
            PR.name, PR.version, PR.description, PR.timeCreated,  
            PR.timeUpdated, timePublished, 
            CreateUser.username as creator,
            UpdateUser.username as updater, 
            PublishUser.username as publisher, 
            shouldMirror, timeMirrored
        FROM  PublishedReleases as PR
        JOIN Projects USING(projectId)
        JOIN Users as CreateUser ON (createdBy=CreateUser.userId)
        JOIN Users as UpdateUser ON (updatedBy=UpdateUser.userId)
        LEFT JOIN Users as PublishUser ON (publishedBy=PublishUser.userId)
        WHERE hostname=?'''
        cu.execute(sql, hostname)
        releases = models.ReleaseList()
        for row in cu:
            row = dict(row)
            releases.releases.append(models.Release(**row))
        return releases

    def getReleaseForProduct(self, fqdn, releaseId):
        hostname = fqdn.split('.')[0]
        cu = self.db.cursor()
        sql = '''
        SELECT pubReleaseId as releaseId, Projects.hostname,
            PR.name, PR.version, PR.description, PR.timeCreated,  
            PR.timeUpdated, timePublished, 
            CreateUser.username as creator,
            UpdateUser.username as updater, 
            PublishUser.username as publisher, 
            shouldMirror, timeMirrored
        FROM  PublishedReleases as PR
        JOIN Projects USING(projectId)
        JOIN Users as CreateUser ON (createdBy=CreateUser.userId)
        JOIN Users as UpdateUser ON (updatedBy=UpdateUser.userId)
        LEFT JOIN Users as PublishUser ON (publishedBy=PublishUser.userId)
        WHERE hostname=? and releaseId=?'''
        cu.execute(sql, hostname, releaseId)
        row = dict(self.db._getOne(cu, errors.ReleaseNotFound, 
                                   (hostname, releaseId)))
        return models.Release(**row)

@@ -54,16 +54,84 @@
 erved%0A#%0A
+import time%0A%0Afrom mint import buildtypes%0Afrom mint import mint_error
 %0Afrom mi
@@ -1240,16 +1240,74 @@
 ct(row)%0A
+            row%5B'published'%5D = bool(row%5B'timePublished'%5D)%0A
         
@@ -2399,12 +2399,1093 @@
 ease(**row)%0A
+%0A    def createRelease(self, fqdn, buildIds):%0A        hostname = fqdn.split('.')%5B0%5D%0A        sql = '''%0A        INSERT INTO PublishedReleases %0A            (projectId, timeCreated, createdBy)%0A            SELECT projectId, ?, ? FROM Projects WHERE hostname=?%0A        '''%0A        cu = self.db.cursor()%0A        cu.execute(sql, time.time(), self.auth.userId, hostname)%0A        releaseId = cu.lastrowid%0A        for buildId in buildIds:%0A            self._addBuildToRelease(hostname, releaseId, buildId)%0A        self.db.commit()%0A        return releaseId%0A%0A    def _addBuildToRelease(self, hostname, releaseId, imageId):%0A        image = self.db.getImageForProduct(hostname, imageId)%0A        if image.release and releaseId != image.release:%0A            raise mint_error.BuildPublished()%0A        if (image.imageType not in (buildtypes.AMI, buildtypes.IMAGELESS)%0A                and not image.files.files):%0A            raise mint_error.BuildEmpty()%0A        cu = self.db.cursor()%0A        cu.execute('''UPDATE Builds SET pubReleaseId=?%0A                      WHERE buildId=?''', releaseId, imageId)%0A

Add createRelease, and add published field.