# Path: dropbeat/exceptions.py
# class UserException(DropbeatException):
#     pass
# 
# class TrackException(DropbeatException):
#     pass
# 
# class PlaylistException(DropbeatException):
#     pass
# 
# Path: dropbeat/constants.py
# class ErrorCode:
#     """Error codes for HTTP response.
# 
#     """
#     DUPLICATED_EMAIL = '100'
#     INVALID_EMAIL = '101'
#     PASSWORD_TOO_SHORT = '102'
#     EMAIL_NOT_EXIST = '103'
#     PASSWORD_MISMATCH = '104'
#     DUPLICATED_PLAYLIST_NAME = '105'
#     TRACK_ALREADY_EXIST = '106'
#     PLAYLIST_NOT_EXIST = '107'
#     TRACK_NOT_EXIST = '108'
#     RESULT_NOT_READY = '109'
#     INVALID_ACCESS = '110'
# 
# class TrackSource(ChoiceEnum):
#     """Streaming source of track.
# 
#     """
#     youtube = 0
#     soundcloud = 1


import hashlib
import datetime
from dropbeat.exceptions import (
    UserException, TrackException, PlaylistException)
from dropbeat.constants import ErrorCode, TrackSource
from django.db import models, IntegrityError
from django.core.validators import validate_email
from django.core.exceptions import ValidationError, ObjectDoesNotExist
from django.contrib.auth.models import BaseUserManager
            raise PlaylistException(ErrorCode.DUPLICATED_PLAYLIST_NAME)

        uid = hashlib.md5(
            user.email + str(datetime.datetime.now())).hexdigest()
        return self.create(user=user, name=name, uid=uid)

    def remove_playlist(self, user, uid):
        try:
            playlist = self.get(user=user, uid=uid)
            playlist.delete()
        except ObjectDoesNotExist:
            raise PlaylistException(ErrorCode.PLAYLIST_NOT_EXIST)

    def change_playlist_name(self, user, uid, name):
        try:
            # TODO: Check length of the new name.
            playlist = self.get(user=user, uid=uid)
            playlist.name = name
            playlist.save()
        except ObjectDoesNotExist:
            raise PlaylistException(ErrorCode.PLAYLIST_NOT_EXIST)


class TrackManager(models.Manager):
    def create_track(self, name, uid, playlist):
        if self.filter(uid=uid, playlist=playlist).exists():
            raise TrackException(ErrorCode.TRACK_ALREADY_EXIST)

        youtube_uid_len = 11
        source = TrackSource.youtube \

# Path: dropbeat/exceptions.py
# class UserException(DropbeatException):
#     pass
# 
# class TrackException(DropbeatException):
#     pass
# 
# class PlaylistException(DropbeatException):
#     pass
# 
# Path: dropbeat/constants.py
# class ErrorCode:
#     """Error codes for HTTP response.
# 
#     """
#     DUPLICATED_EMAIL = '100'
#     INVALID_EMAIL = '101'
#     PASSWORD_TOO_SHORT = '102'
#     EMAIL_NOT_EXIST = '103'
#     PASSWORD_MISMATCH = '104'
#     DUPLICATED_PLAYLIST_NAME = '105'
#     TRACK_ALREADY_EXIST = '106'
#     PLAYLIST_NOT_EXIST = '107'
#     TRACK_NOT_EXIST = '108'
#     RESULT_NOT_READY = '109'
#     INVALID_ACCESS = '110'
# 
# class TrackSource(ChoiceEnum):
#     """Streaming source of track.
# 
#     """
#     youtube = 0
#     soundcloud = 1


# Path: dropbeat/managers.py
import hashlib
import datetime
from dropbeat.exceptions import (
    UserException, TrackException, PlaylistException)
from dropbeat.constants import ErrorCode, TrackSource
from django.db import models, IntegrityError
from django.core.validators import validate_email
from django.core.exceptions import ValidationError, ObjectDoesNotExist
from django.contrib.auth.models import BaseUserManager

            raise PlaylistException(ErrorCode.DUPLICATED_PLAYLIST_NAME)

        uid = hashlib.md5(
            user.email + str(datetime.datetime.now())).hexdigest()
        return self.create(user=user, name=name, uid=uid)

    def remove_playlist(self, user, uid):
        try:
            playlist = self.get(user=user, uid=uid)
            playlist.delete()
        except ObjectDoesNotExist:
            raise PlaylistException(ErrorCode.PLAYLIST_NOT_EXIST)

    def change_playlist_name(self, user, uid, name):
        try:
            # TODO: Check length of the new name.
            playlist = self.get(user=user, uid=uid)
            playlist.name = name
            playlist.save()
        except ObjectDoesNotExist:
            raise PlaylistException(ErrorCode.PLAYLIST_NOT_EXIST)


class TrackManager(models.Manager):
    def create_track(self, name, uid, playlist):
        if self.filter(uid=uid, playlist=playlist).exists():
            raise TrackException(ErrorCode.TRACK_ALREADY_EXIST)

        youtube_uid_len = 11
        source = TrackSource.youtube \

            if len(uid) == youtube_uid_len else TrackSource.soundcloud