# Path: cms/models.py
# class Member(models.Model):
#     GROUP_CHOICES = (
#         (u'graduate', u'Graduate'),
#         (u'undergraduate', u'Undergraduate'),
#         (u'faculty', u'Faculty')
#     )
# 
#     CLASS_CHOICES = (
#         (u'freshman', u'Freshman'),
#         (u'sophomore', u'Sophomore'),
#         (u'junior', u'Junior'),
#         (u'senior', u'Senior')
#     )
# 
#     STATUS_EMPTY    = 0 # this member is pending "creation" by its owner
#     STATUS_ACTIVE   = 1 # this member has been created
#     STATUS_ARCHIVED = 2 # this member has been archived and is frozen
# 
#     STATUS_CHOICES  = (
#         (STATUS_EMPTY, u'Empty'),
#         (STATUS_ACTIVE, u'Active'),
#         (STATUS_ARCHIVED, u'Archived')
#     )
# 
#     user            = models.ForeignKey(User, related_name='profile', unique=True)
#     group           = models.CharField(max_length=255, choices=GROUP_CHOICES)
#     classification  = models.CharField(max_length=255, choices=CLASS_CHOICES, blank=True)
#     hometown        = models.CharField(max_length=255, blank=True)
#     interests       = models.TextField(blank=True)
#     homepage        = models.URLField(blank=True)
#     blurb           = models.TextField(blank=True)
#     image           = models.ImageField(upload_to=make_member_image_name, storage=OverwriteStorage(), blank=True)
#     status          = models.IntegerField(choices=STATUS_CHOICES)
#     #activation_key  = models.CharField(max_length=255, blank=True)
# 
#     def __unicode__(self):
#         return unicode(self.user.get_full_name())
# 
#     @models.permalink
#     def get_absolute_url(self):
#         return ('cms:profile_url', (self.pk,), {})
# 
#     def generate_hashed_email(self):
#         return hashlib.md5(self.user.email).hexdigest()
# 
#     def get_coordinated_projects(self):
#         return Project.objects.filter(pk__in=ProjectMember.objects.filter(member__pk=self.pk, is_coordinator=True).values_list('project__pk', flat=True))
# 
#     @staticmethod
#     def get_possible_project_members():
# 	    # need to allow member one year old as well since activity status is predicated on project membership
# 	    # but you can't create a project for a new year without a project coordinator
#         # hence, the one year offset avoids the chicken-and-the-egg problem
#         #return Member.objects.filter(Q(status=Member.STATUS_ACTIVE) | Q(pk__in=ProjectMember.objects.filter(project__year__gte= \
#         #    settings.CURRENT_YEAR-1).distinct().values_list('member'))).order_by('user__first_name', 'user__last_name')
#         return Member.objects.exclude(status=Member.STATUS_ARCHIVED).order_by('user__first_name', 'user__last_name')
# 
#     class Meta:
#         verbose_name        = 'member'
#         verbose_name_plural = 'members'
#         ordering            = ['user__first_name']
# 
# class Project(models.Model):
#     STATUS_EMPTY    = 0 # this project is pending "creation" by coordinator
#     STATUS_ACTIVE   = 1 # this project has been created and is editable by coordinators
#     STATUS_ARCHIVED = 2 # this project has been archived and is frozen
# 
#     STATUS_CHOICES  = (
#         (STATUS_EMPTY, u'Empty'),
#         (STATUS_ACTIVE, u'Active'),
#         (STATUS_ARCHIVED, u'Archived')
#     )
# 
#     CATEGORY_OTHER          = 0
#     CATEGORY_OUTREACH       = 1
#     CATEGORY_RESEARCH       = 2
#     CATEGORY_SERVICE        = 3
#     CATEGORY_INTERNSHIP     = 4
#     CATEGORY_ORGANIZATIONAL = 5
# 
#     CATEGORY_CHOICES = (
#         (CATEGORY_OUTREACH, u'Outreach'),
#         (CATEGORY_RESEARCH, u'Research'),
#         (CATEGORY_SERVICE, u'Service'),
#         (CATEGORY_INTERNSHIP, u'Internship'),
#         (CATEGORY_ORGANIZATIONAL, u'Organizational'),
#         (CATEGORY_OTHER, u'Other'),
#     )
# 
#     title           = models.CharField(max_length=255)
#     description     = models.TextField()
#     image           = models.ImageField(upload_to=make_project_image_name, storage=OverwriteStorage(), blank=True)
#     #active          = models.BooleanField(default=False)
#     status          = models.IntegerField(choices=STATUS_CHOICES)
#     category        = models.IntegerField(choices=CATEGORY_CHOICES, default=CATEGORY_OTHER)
#     year            = models.IntegerField()
#     members         = models.ManyToManyField(Member, through='ProjectMember')
#     parent          = models.ForeignKey('self', blank=True, null=True, on_delete=models.SET_NULL)
# 
#     def __unicode__(self):
#         return unicode('%s %d' % (self.title, self.year))
# 
#     @models.permalink
#     def get_absolute_url(self):
#         return ('cms:projects_year_pk_url', (), {'year': self.year, 'pk': self.pk})
# 
#     def is_member_coordinator(self, member):
#         return ProjectMember.objects.filter(member__pk=member.pk, project__pk=self.pk, is_coordinator=True).count() != 0
# 
#     class Meta:
#         verbose_name        = 'project'
#         verbose_name_plural = 'projects'
# 
# MEMBER_IMAGE_FOLDER = 'member'
# 
# PROJECT_IMAGE_FOLDER = 'project'


from django.core.management.base import BaseCommand
from django.conf import settings
from cms.models import Member, Project, MEMBER_IMAGE_FOLDER, PROJECT_IMAGE_FOLDER
import os



class Command(BaseCommand):
    args = ''
    help = 'Removes unused files and folders from media/'

    def handle(self, *args, **kwargs):
        member_active_files = [ os.path.join(settings.MEDIA_ROOT, x) for x in Member.objects.all().values_list('image', flat=True) ]

# Path: cms/models.py
# class Member(models.Model):
#     GROUP_CHOICES = (
#         (u'graduate', u'Graduate'),
#         (u'undergraduate', u'Undergraduate'),
#         (u'faculty', u'Faculty')
#     )
# 
#     CLASS_CHOICES = (
#         (u'freshman', u'Freshman'),
#         (u'sophomore', u'Sophomore'),
#         (u'junior', u'Junior'),
#         (u'senior', u'Senior')
#     )
# 
#     STATUS_EMPTY    = 0 # this member is pending "creation" by its owner
#     STATUS_ACTIVE   = 1 # this member has been created
#     STATUS_ARCHIVED = 2 # this member has been archived and is frozen
# 
#     STATUS_CHOICES  = (
#         (STATUS_EMPTY, u'Empty'),
#         (STATUS_ACTIVE, u'Active'),
#         (STATUS_ARCHIVED, u'Archived')
#     )
# 
#     user            = models.ForeignKey(User, related_name='profile', unique=True)
#     group           = models.CharField(max_length=255, choices=GROUP_CHOICES)
#     classification  = models.CharField(max_length=255, choices=CLASS_CHOICES, blank=True)
#     hometown        = models.CharField(max_length=255, blank=True)
#     interests       = models.TextField(blank=True)
#     homepage        = models.URLField(blank=True)
#     blurb           = models.TextField(blank=True)
#     image           = models.ImageField(upload_to=make_member_image_name, storage=OverwriteStorage(), blank=True)
#     status          = models.IntegerField(choices=STATUS_CHOICES)
#     #activation_key  = models.CharField(max_length=255, blank=True)
# 
#     def __unicode__(self):
#         return unicode(self.user.get_full_name())
# 
#     @models.permalink
#     def get_absolute_url(self):
#         return ('cms:profile_url', (self.pk,), {})
# 
#     def generate_hashed_email(self):
#         return hashlib.md5(self.user.email).hexdigest()
# 
#     def get_coordinated_projects(self):
#         return Project.objects.filter(pk__in=ProjectMember.objects.filter(member__pk=self.pk, is_coordinator=True).values_list('project__pk', flat=True))
# 
#     @staticmethod
#     def get_possible_project_members():
# 	    # need to allow member one year old as well since activity status is predicated on project membership
# 	    # but you can't create a project for a new year without a project coordinator
#         # hence, the one year offset avoids the chicken-and-the-egg problem
#         #return Member.objects.filter(Q(status=Member.STATUS_ACTIVE) | Q(pk__in=ProjectMember.objects.filter(project__year__gte= \
#         #    settings.CURRENT_YEAR-1).distinct().values_list('member'))).order_by('user__first_name', 'user__last_name')
#         return Member.objects.exclude(status=Member.STATUS_ARCHIVED).order_by('user__first_name', 'user__last_name')
# 
#     class Meta:
#         verbose_name        = 'member'
#         verbose_name_plural = 'members'
#         ordering            = ['user__first_name']
# 
# class Project(models.Model):
#     STATUS_EMPTY    = 0 # this project is pending "creation" by coordinator
#     STATUS_ACTIVE   = 1 # this project has been created and is editable by coordinators
#     STATUS_ARCHIVED = 2 # this project has been archived and is frozen
# 
#     STATUS_CHOICES  = (
#         (STATUS_EMPTY, u'Empty'),
#         (STATUS_ACTIVE, u'Active'),
#         (STATUS_ARCHIVED, u'Archived')
#     )
# 
#     CATEGORY_OTHER          = 0
#     CATEGORY_OUTREACH       = 1
#     CATEGORY_RESEARCH       = 2
#     CATEGORY_SERVICE        = 3
#     CATEGORY_INTERNSHIP     = 4
#     CATEGORY_ORGANIZATIONAL = 5
# 
#     CATEGORY_CHOICES = (
#         (CATEGORY_OUTREACH, u'Outreach'),
#         (CATEGORY_RESEARCH, u'Research'),
#         (CATEGORY_SERVICE, u'Service'),
#         (CATEGORY_INTERNSHIP, u'Internship'),
#         (CATEGORY_ORGANIZATIONAL, u'Organizational'),
#         (CATEGORY_OTHER, u'Other'),
#     )
# 
#     title           = models.CharField(max_length=255)
#     description     = models.TextField()
#     image           = models.ImageField(upload_to=make_project_image_name, storage=OverwriteStorage(), blank=True)
#     #active          = models.BooleanField(default=False)
#     status          = models.IntegerField(choices=STATUS_CHOICES)
#     category        = models.IntegerField(choices=CATEGORY_CHOICES, default=CATEGORY_OTHER)
#     year            = models.IntegerField()
#     members         = models.ManyToManyField(Member, through='ProjectMember')
#     parent          = models.ForeignKey('self', blank=True, null=True, on_delete=models.SET_NULL)
# 
#     def __unicode__(self):
#         return unicode('%s %d' % (self.title, self.year))
# 
#     @models.permalink
#     def get_absolute_url(self):
#         return ('cms:projects_year_pk_url', (), {'year': self.year, 'pk': self.pk})
# 
#     def is_member_coordinator(self, member):
#         return ProjectMember.objects.filter(member__pk=member.pk, project__pk=self.pk, is_coordinator=True).count() != 0
# 
#     class Meta:
#         verbose_name        = 'project'
#         verbose_name_plural = 'projects'
# 
# MEMBER_IMAGE_FOLDER = 'member'
# 
# PROJECT_IMAGE_FOLDER = 'project'


# Path: cms/management/commands/cleanmedia.py
from django.core.management.base import BaseCommand
from django.conf import settings
from cms.models import Member, Project, MEMBER_IMAGE_FOLDER, PROJECT_IMAGE_FOLDER
import os




class Command(BaseCommand):
    args = ''
    help = 'Removes unused files and folders from media/'

    def handle(self, *args, **kwargs):
        member_active_files = [ os.path.join(settings.MEDIA_ROOT, x) for x in Member.objects.all().values_list('image', flat=True) ]

        member_base = os.path.join(settings.MEDIA_ROOT, MEMBER_IMAGE_FOLDER)