# Path: subrosa/models/UsersModel.py
# class Users(BaseModel):
# 
#     """
#     Models and methods related to user database interaction
#     """
# 
#     username = CharField(max_length=40, unique=True, index=True)
#     hash = CharField()
#     real_name = CharField(max_length=40, null=True)
#     description = TextField(null=True)
#     about = TextField(null=True)
# 
#     @staticmethod
#     def check_any_exist():
#         """ Check if any users exist """
#         return Users.get_count() > 0
# 
#     @staticmethod
#     def check_exists(username):
# 
#         """ Check if user with given username already exists """
# 
#         return Users.select()\
#                .where(Users.username == username)\
#                .exists()
# 
#     @staticmethod
#     def get_user(id):
#         """ Get user by id """
#         return Users.get_single("id", id)
# 
#     @staticmethod
#     def get_user_by_username(username):
# 
#         """ Get user by his username , returns 0 if not exists """
# 
#         return Users.get_single("username", username)
# 
#     @staticmethod
#     @db.commit_on_success
#     def create_user(username, password, description=None, real_name=None):
# 
#         """ Create new user """
# 
#         try:
#             return Users.create(username=username,
#                                 hash=generate_password_hash(password),
#                                 description=description,
#                                 real_name=real_name).get_id()
# 
#         except:
#             handle_errors("Error creating user")
#             raise
# 
#     def check_password(self, password):
# 
#         """ Compare password against the one in db """
# 
#         return check_password_hash(self.hash, password)
# 
#     def __repr__(self):
#         return "<User: %s>" % self.username
# 
#     class Meta:
#         order_by = ("username",)
# 
# Path: subrosa/models/UserImagesModel.py
# class UserImages(BaseModel):
# 
#     image_link = TextField()
#     date_added = DateTimeField(default=datetime.datetime.utcnow())
#     delete_hash = TextField(null=True)
#     description = TextField(null=True)
#     is_vertical = IntegerField(null=True)
#     gallery = BooleanField(default=False)
#     imgur_img = BooleanField(default=False)
#     owner = ForeignKeyField(Users, related_name="images")
# 
#     @staticmethod
#     def get_image(id):
#         return UserImages.get_single("id", id)
# 
#     @staticmethod
#     def check_exists(image_link):
#         return (UserImages.select()
#                 .where(UserImages.image_link == image_link)
#                 .exists())
# 
#     @staticmethod
#     def get_gallery_images(page, per_page, username=None, gallery=False):
#         q = UserImages.select()
#         if username:
#             q = q.join(Users).where(Users.username == username)
#         if gallery:
#             return q.where(UserImages.gallery == True).paginate(page, per_page)
#         return q.paginate(page, per_page)
# 
#     @staticmethod
#     @db.commit_on_success
#     def gallerify(image):
# 
#         try:
#             is_gallerified = image.gallery
#             image.gallery = not is_gallerified
#             image.save()
# 
#         except Exception as e:
#             handle_errors("Error updating image")
# 
#     @staticmethod
#     @db.commit_on_success
#     def add_image(image_link,
#                   description,
#                   owner,
#                   is_vertical=True,
#                   imgur_img=False,
#                   delete_hash=None):
#         try:
#             UserImages.create(
#                 image_link=image_link,
#                 description=description,
#                 is_vertical=is_vertical,
#                 owner=owner,
#                 imgur_img=imgur_img,
#                 delete_hash=delete_hash,
#             )
#             return 1
#         except Exception as e:
#             handle_errors("Error creating image")
#             raise
# 
#     @staticmethod
#     @db.commit_on_success
#     def delete_image(image):
#         try:
#             image.delete_instance()
#             return 1
#         except Exception as e:
#             handle_errors("Error deleting image")
#             raise
# 
#     def __repr__(self):
#         return "<Image: {0}>".format(self.image_link)


import unittest
from subrosa.models.UsersModel import Users
from subrosa.models.UserImagesModel import UserImages
from playhouse.test_utils import test_database
from peewee import *
        with test_database(db, (Users, UserImages)):
            Users.create_user(username = "konrad", password = "test")
            user1 = Users.select().get()

            UserImages.add_image(image_link = self.ADDRESS,\
                                description = "test",\
                                is_vertical = True, \
                                owner = user1,\
                                imgur_img = False,\
                                delete_hash = None)


            image = UserImages.select().get()

            self.assertEquals(self.ADDRESS ,image.image_link)
            self.assertEquals("test", image.description)
            self.assertEquals(user1, image.owner)

            UserImages.add_image(image_link = self.ADDRESS,\
                                description = "test",\
                                is_vertical = True, \
                                owner = user1,\
                                imgur_img = False,\
                                delete_hash = None)

            self.assertEquals(2, UserImages.get_count())

            self.assertTrue(UserImages.check_exists(self.ADDRESS))
            self.assertFalse(UserImages.check_exists(self.ADDRESS + "bam"))
            image = UserImages.get_image(2)

# Path: subrosa/models/UsersModel.py
# class Users(BaseModel):
# 
#     """
#     Models and methods related to user database interaction
#     """
# 
#     username = CharField(max_length=40, unique=True, index=True)
#     hash = CharField()
#     real_name = CharField(max_length=40, null=True)
#     description = TextField(null=True)
#     about = TextField(null=True)
# 
#     @staticmethod
#     def check_any_exist():
#         """ Check if any users exist """
#         return Users.get_count() > 0
# 
#     @staticmethod
#     def check_exists(username):
# 
#         """ Check if user with given username already exists """
# 
#         return Users.select()\
#                .where(Users.username == username)\
#                .exists()
# 
#     @staticmethod
#     def get_user(id):
#         """ Get user by id """
#         return Users.get_single("id", id)
# 
#     @staticmethod
#     def get_user_by_username(username):
# 
#         """ Get user by his username , returns 0 if not exists """
# 
#         return Users.get_single("username", username)
# 
#     @staticmethod
#     @db.commit_on_success
#     def create_user(username, password, description=None, real_name=None):
# 
#         """ Create new user """
# 
#         try:
#             return Users.create(username=username,
#                                 hash=generate_password_hash(password),
#                                 description=description,
#                                 real_name=real_name).get_id()
# 
#         except:
#             handle_errors("Error creating user")
#             raise
# 
#     def check_password(self, password):
# 
#         """ Compare password against the one in db """
# 
#         return check_password_hash(self.hash, password)
# 
#     def __repr__(self):
#         return "<User: %s>" % self.username
# 
#     class Meta:
#         order_by = ("username",)
# 
# Path: subrosa/models/UserImagesModel.py
# class UserImages(BaseModel):
# 
#     image_link = TextField()
#     date_added = DateTimeField(default=datetime.datetime.utcnow())
#     delete_hash = TextField(null=True)
#     description = TextField(null=True)
#     is_vertical = IntegerField(null=True)
#     gallery = BooleanField(default=False)
#     imgur_img = BooleanField(default=False)
#     owner = ForeignKeyField(Users, related_name="images")
# 
#     @staticmethod
#     def get_image(id):
#         return UserImages.get_single("id", id)
# 
#     @staticmethod
#     def check_exists(image_link):
#         return (UserImages.select()
#                 .where(UserImages.image_link == image_link)
#                 .exists())
# 
#     @staticmethod
#     def get_gallery_images(page, per_page, username=None, gallery=False):
#         q = UserImages.select()
#         if username:
#             q = q.join(Users).where(Users.username == username)
#         if gallery:
#             return q.where(UserImages.gallery == True).paginate(page, per_page)
#         return q.paginate(page, per_page)
# 
#     @staticmethod
#     @db.commit_on_success
#     def gallerify(image):
# 
#         try:
#             is_gallerified = image.gallery
#             image.gallery = not is_gallerified
#             image.save()
# 
#         except Exception as e:
#             handle_errors("Error updating image")
# 
#     @staticmethod
#     @db.commit_on_success
#     def add_image(image_link,
#                   description,
#                   owner,
#                   is_vertical=True,
#                   imgur_img=False,
#                   delete_hash=None):
#         try:
#             UserImages.create(
#                 image_link=image_link,
#                 description=description,
#                 is_vertical=is_vertical,
#                 owner=owner,
#                 imgur_img=imgur_img,
#                 delete_hash=delete_hash,
#             )
#             return 1
#         except Exception as e:
#             handle_errors("Error creating image")
#             raise
# 
#     @staticmethod
#     @db.commit_on_success
#     def delete_image(image):
#         try:
#             image.delete_instance()
#             return 1
#         except Exception as e:
#             handle_errors("Error deleting image")
#             raise
# 
#     def __repr__(self):
#         return "<Image: {0}>".format(self.image_link)


# Path: subrosa/tests/test_images.py
import unittest
from subrosa.models.UsersModel import Users
from subrosa.models.UserImagesModel import UserImages
from playhouse.test_utils import test_database
from peewee import *

        with test_database(db, (Users, UserImages)):
            Users.create_user(username = "konrad", password = "test")
            user1 = Users.select().get()

            UserImages.add_image(image_link = self.ADDRESS,\
                                description = "test",\
                                is_vertical = True, \
                                owner = user1,\
                                imgur_img = False,\
                                delete_hash = None)


            image = UserImages.select().get()

            self.assertEquals(self.ADDRESS ,image.image_link)
            self.assertEquals("test", image.description)
            self.assertEquals(user1, image.owner)

            UserImages.add_image(image_link = self.ADDRESS,\
                                description = "test",\
                                is_vertical = True, \
                                owner = user1,\
                                imgur_img = False,\
                                delete_hash = None)

            self.assertEquals(2, UserImages.get_count())

            self.assertTrue(UserImages.check_exists(self.ADDRESS))
            self.assertFalse(UserImages.check_exists(self.ADDRESS + "bam"))
            image = UserImages.get_image(2)

            UserImages.delete_image(image)