# Path: cntapp/models.py
# class Document(models.Model):
#     TYPE_VIDEO = 'v'
#     TYPE_PDF = 'p'
#     TYPE_IMAGE = 'i'
#     TYPE_AUDIO = 'a'
#     TYPE_GOOGLE_APK = 'g'  # for google ;)
#     TYPE_OTHERS = 'o'
#     TYPES = (
#         (TYPE_VIDEO, 'video'),
#         (TYPE_AUDIO, 'sound'),
#         (TYPE_PDF, 'pdf'),
#         (TYPE_GOOGLE_APK, 'google_apk'),
#         (TYPE_IMAGE, 'image'),
#         (TYPE_OTHERS, 'others'),
#     )
# 
#     name = models.CharField(max_length=100)
#     type = models.CharField(max_length=2, choices=TYPES, blank=True)
#     description = models.CharField(max_length=250, blank=True)
#     file = models.FileField()
#     thumbnail = ProcessedImageField(upload_to='thumbnails', blank=True, null=True,
#                                     processors=[ResizeToFill(150, 150)],
#                                     format='PNG',
#                                     options={'quality': 50})
# 
#     def __str__(self):
#         return self.name
# 
# class Directory(models.Model):
#     MAX_NAME_LEN = 255
#     name = models.CharField(max_length=MAX_NAME_LEN)
#     documents = models.ManyToManyField(Document, blank=True)
#     sub_dirs = models.ManyToManyField(
#         'self',
#         blank=True,
#         symmetrical=False,
#         through='SubDirRelation',
#         through_fields=('parent', 'child'))
# 
#     def get_sub_dirs(self):
#         return self.sub_dirs.all()
# 
#     def get_sub_dir_by_name(self, name):
#         dirs = self.get_sub_dirs()
#         return dirs.get(name=name)
# 
#     def get_parents(self):
#         return self.directory_set.all()
# 
#     def get_paths(self):
#         """ return paths recursively
#         """
#         parents = self.get_parents()
#         if parents.count() == 0:
#             return [[self]]
# 
#         paths = []
#         for p in parents:
#             p_paths = p.get_paths()
#             for p_path in p_paths:
#                 p_path.append(self)
#             paths.extend(p_paths)
#         return paths
# 
#     def add_sub_dir(self, sub_dir):
#         if len(SubDirRelation.objects.filter(parent=self, child=sub_dir)) > 0:
#             logger.warn('SubDirRelation already exists between parent_id=%d and child_id=%d' % (
#                 self.id, sub_dir.id))
#             return self
#         SubDirRelation.objects.create(parent=self, child=sub_dir)
#         return self
# 
#     @transaction.atomic
#     def remove_sub_dir(self, sub_dir):
#         """ Remove recursively a sub directory.
# 
#         Delete the directories that have only one parent.
#         Only delete the parent-child relation for the directories that have multiple parents."""
#         l = SubDirRelation.objects.get(parent=self, child=sub_dir)
#         l.delete()
#         if len(sub_dir.get_parents()) == 0:
#             for d in sub_dir.get_sub_dirs():
#                 sub_dir.remove_sub_dir(d)
#             Directory.objects.get(pk=sub_dir.pk).delete()
# 
#     def unlink_sub_dir(self, sub_dir):
#         try:
#             l = SubDirRelation.objects.get(parent=self, child=sub_dir)
#             l.delete()
#             return True
#         except models.ObjectDoesNotExist as e:
#             logger.warn('No SubDirRelation found between parent_id=%d and child_id=%d: %s' % (
#                 self.id, sub_dir.id, e
#             ))
#             return False
# 
#     def __str__(self):
#         return self.name


import subprocess
from django.shortcuts import render
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect, JsonResponse
from django.contrib.auth import authenticate, login, logout
from django.conf import settings
from edupi import VERSION
from cntapp.models import Document, Directory


def logout_admin(request):
    logout(request)
    return HttpResponseRedirect(reverse('cntapp:login'))


def sys_info(request):
    """
    Return system information in JSON
    """
    info = {}
    cntapp_info = {}
    system_info = {}

    df_index = {
        "size": 0,
        "used": 1,
        "available": 2,
        "used_percentage": 3,
    }

    # result in k-bytes
    df_res = subprocess.check_output(['df', '/home/']).decode().split()[8:-1]
    system_info["TotalSize"] = df_res[df_index['size']]
    system_info["Used"] = df_res[df_index['used']]
    system_info["Available"] = df_res[df_index['available']]
    system_info["UsedPercentage"] = df_res[df_index['used_percentage']]

    cntapp_info["Used"] = subprocess.check_output(['du', '-s', settings.MEDIA_ROOT]).decode().split()[0]

# Path: cntapp/models.py
# class Document(models.Model):
#     TYPE_VIDEO = 'v'
#     TYPE_PDF = 'p'
#     TYPE_IMAGE = 'i'
#     TYPE_AUDIO = 'a'
#     TYPE_GOOGLE_APK = 'g'  # for google ;)
#     TYPE_OTHERS = 'o'
#     TYPES = (
#         (TYPE_VIDEO, 'video'),
#         (TYPE_AUDIO, 'sound'),
#         (TYPE_PDF, 'pdf'),
#         (TYPE_GOOGLE_APK, 'google_apk'),
#         (TYPE_IMAGE, 'image'),
#         (TYPE_OTHERS, 'others'),
#     )
# 
#     name = models.CharField(max_length=100)
#     type = models.CharField(max_length=2, choices=TYPES, blank=True)
#     description = models.CharField(max_length=250, blank=True)
#     file = models.FileField()
#     thumbnail = ProcessedImageField(upload_to='thumbnails', blank=True, null=True,
#                                     processors=[ResizeToFill(150, 150)],
#                                     format='PNG',
#                                     options={'quality': 50})
# 
#     def __str__(self):
#         return self.name
# 
# class Directory(models.Model):
#     MAX_NAME_LEN = 255
#     name = models.CharField(max_length=MAX_NAME_LEN)
#     documents = models.ManyToManyField(Document, blank=True)
#     sub_dirs = models.ManyToManyField(
#         'self',
#         blank=True,
#         symmetrical=False,
#         through='SubDirRelation',
#         through_fields=('parent', 'child'))
# 
#     def get_sub_dirs(self):
#         return self.sub_dirs.all()
# 
#     def get_sub_dir_by_name(self, name):
#         dirs = self.get_sub_dirs()
#         return dirs.get(name=name)
# 
#     def get_parents(self):
#         return self.directory_set.all()
# 
#     def get_paths(self):
#         """ return paths recursively
#         """
#         parents = self.get_parents()
#         if parents.count() == 0:
#             return [[self]]
# 
#         paths = []
#         for p in parents:
#             p_paths = p.get_paths()
#             for p_path in p_paths:
#                 p_path.append(self)
#             paths.extend(p_paths)
#         return paths
# 
#     def add_sub_dir(self, sub_dir):
#         if len(SubDirRelation.objects.filter(parent=self, child=sub_dir)) > 0:
#             logger.warn('SubDirRelation already exists between parent_id=%d and child_id=%d' % (
#                 self.id, sub_dir.id))
#             return self
#         SubDirRelation.objects.create(parent=self, child=sub_dir)
#         return self
# 
#     @transaction.atomic
#     def remove_sub_dir(self, sub_dir):
#         """ Remove recursively a sub directory.
# 
#         Delete the directories that have only one parent.
#         Only delete the parent-child relation for the directories that have multiple parents."""
#         l = SubDirRelation.objects.get(parent=self, child=sub_dir)
#         l.delete()
#         if len(sub_dir.get_parents()) == 0:
#             for d in sub_dir.get_sub_dirs():
#                 sub_dir.remove_sub_dir(d)
#             Directory.objects.get(pk=sub_dir.pk).delete()
# 
#     def unlink_sub_dir(self, sub_dir):
#         try:
#             l = SubDirRelation.objects.get(parent=self, child=sub_dir)
#             l.delete()
#             return True
#         except models.ObjectDoesNotExist as e:
#             logger.warn('No SubDirRelation found between parent_id=%d and child_id=%d: %s' % (
#                 self.id, sub_dir.id, e
#             ))
#             return False
# 
#     def __str__(self):
#         return self.name


# Path: cntapp/views/custom.py
import subprocess
from django.shortcuts import render
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect, JsonResponse
from django.contrib.auth import authenticate, login, logout
from django.conf import settings
from edupi import VERSION
from cntapp.models import Document, Directory



def logout_admin(request):
    logout(request)
    return HttpResponseRedirect(reverse('cntapp:login'))


def sys_info(request):
    """
    Return system information in JSON
    """
    info = {}
    cntapp_info = {}
    system_info = {}

    df_index = {
        "size": 0,
        "used": 1,
        "available": 2,
        "used_percentage": 3,
    }

    # result in k-bytes
    df_res = subprocess.check_output(['df', '/home/']).decode().split()[8:-1]
    system_info["TotalSize"] = df_res[df_index['size']]
    system_info["Used"] = df_res[df_index['used']]
    system_info["Available"] = df_res[df_index['available']]
    system_info["UsedPercentage"] = df_res[df_index['used_percentage']]

    cntapp_info["Used"] = subprocess.check_output(['du', '-s', settings.MEDIA_ROOT]).decode().split()[0]

    cntapp_info['TotalDocuments'] = Document.objects.all().count()