from flask import abort, request
from . import CollectionModel, AlbumModel, AuthenticatedResource

class AlbumInstance (AuthenticatedResource):

    def get (self, id):

        if AlbumModel.select().where(AlbumModel.id == id).count() != 1:

            abort(404)

        album = AlbumModel.get(AlbumModel.id == id)

        photos = [{
                    'id': photo.id,
                    'name': photo.name,
                    'uploaded': photo.uploaded.strftime("%Y-%m-%d %H:%M:%S") ,
                    'size': photo.size
                  } for photo in album.photos]

        return {'id': album.id, 'name': album.name, 'collection': {'name': album.collection.name, 'id': album.collection.id}, 'photos': photos}

class AlbumsResource (AuthenticatedResource):

    def get (self):

        collection = request.args.get('collection')

        if collection is None:

            albums = [{
                        'id':album.id,
                        'name':album.name,
                        'collection': {
                            'name': album.collection.name,
                            'id': album.collection.id
                        }
                      } for album in AlbumModel.select()]

            return albums

        else:

            if CollectionModel.select().where(CollectionModel.name == collection).count() != 1:

                abort(404)

            collection = CollectionModel.get(CollectionModel.name == collection)

            albums = [{
                        'id':album.id,
                        'name':album.name,
                        'collection': {
                            'name': album.collection.name,
                            'id': album.collection.id
                        }
                      } for album in collection.albums]

            return albums

    def post (self):

        collection = request.form.get('collection')
        name = request.form.get('name')

        if not collection or not name:

            print collection
            print name

            abort(400)

        if CollectionModel.select().where(CollectionModel.name == collection).count() != 1:

            abort(404)

        collection = CollectionModel.get(CollectionModel.name == collection)

        for album in collection.albums:

            if album.name == name:

                abort(409)

        AlbumModel.create(
            name = name,
            collection = collection
        )

        albums = [{
                    'id':album.id,
                    'name':album.name,
                    'collection': {
                        'name': album.collection.name,
                        'id': album.collection.id
                    }
                  } for album in AlbumModel.select()]

        return albums

@@ -1255,24 +1255,94 @@
      else:%0A%0A
+            if not collection.isdigit():%0A%0A                abort(400)%0A%0A
             
@@ -1383,36 +1383,34 @@
 CollectionModel.
-name
+id
  == collection).
@@ -1505,36 +1505,34 @@
 CollectionModel.
-name
+id
  == collection)%0A
@@ -2057,62 +2057,37 @@
 name
-:%0A%0A            print collection%0A            print name
+ or not collection.isdigit():
 %0A%0A  
@@ -2158,36 +2158,34 @@
 CollectionModel.
-name
+id
  == collection).
@@ -2280,20 +2280,18 @@
 onModel.
-name
+id
  == coll

Use collection ID's in place of names when creating and filtering albums