# Path: scripts/run_munger.py
# def print_run_status(run_start_time, message):
# def main(munger_builder_id=1):
# 
# Path: scripts/build_munger.py
# def main(munger_builder_id=1):
# 
# Path: script_builder/models.py
# class MungerBuilder(models.Model, PermissionedModel):
# 
#     class Meta:
#         permissions = (
#             ('view_mungerbuilder', 'View Munger'),
#         )
# 
#     munger_name = models.CharField(max_length=200)
# 
#     munger_template = models.FilePathField(path='script_builder/templates/munger_templates', max_length=200,
#                                            default='pandas_munger_template_basic.html')
# 
#     input_path = models.CharField(max_length=999, default='', blank=True)
#     output_path = models.CharField(max_length=999, default='', blank=True)
# 
#     rows_to_delete_top = models.IntegerField(default=0)
#     rows_to_delete_bottom = models.IntegerField(default=0)
# 
#     field_types = models.ManyToManyField(FieldType, related_name='munger_builder', related_query_name='munger_builder')
# 
#     default_aggregate_field_type = models.ForeignKey(FieldType, default=3, limit_choices_to={'pk__gt': 2},)
#     is_sample = models.BooleanField(default=False)
# 
#     def save(self, *args, **kwargs):
#         super().save(*args, **kwargs)
#         # Always add default field types unless set from admin
#         self.field_types.add(*(field_type for field_type in FieldType.default_field_types()))
#         self.assign_perms(current_user())
# 
#     def user_is_authorized(self):
#         return self.id == 1 or current_user().has_perm('script_builder.change_mungerbuilder', self)
# 
#     @property
#     def pivot_fields(self):
#         return PivotField.objects.filter(data_field__munger_builder__id=self.id)
# 
#     @property
#     def index_fields(self):
#         return [pf.active_name for pf in self.pivot_fields.filter(field_type__id=1)]
# 
#     @property
#     def column_fields(self):
#         return [pf.active_name for pf in self.pivot_fields.filter(field_type__id=2)]
# 
#     def aggregate_names_with_functions(self, evaled=False):
#         # Needs to be ordered dicts
#         func = eval if evaled else str
#         aggregates_dict = defaultdict(list)
#         for pf in self.pivot_fields.filter(field_type__id__gt=2):
#             aggregates_dict[pf.active_name].append(pf.type_function)
#         return {name: func(', '.join(type_functions)) for name, type_functions in aggregates_dict.items()}
# 
#     @property
#     def rename_field_dict(self):
#         return {field.current_name: field.new_name for field in self.data_fields.all() if field.needs_rename}
# 
#     @property
#     def safe_file_name(self):
#         return self.munger_name.replace(' ', '_').lower()
# 
#     @property
#     def get_output_path(self):
#         if self.output_path:
#             return self.output_path
#         else:
#             input_dir = os.path.dirname(self.input_path)
#             return os.path.join(input_dir, '{0}-output.csv'.format(self.safe_file_name))
# 
#     def __str__(self):
#         return self.munger_name


import time
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth.decorators import login_required, user_passes_test
from django.contrib import messages
from django.http import StreamingHttpResponse
from django.core.urlresolvers import reverse
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter
from scripts import run_munger, build_munger
from script_builder.models import MungerBuilder
INDEX_REDIRECT = HttpResponseRedirect('/script_builder/munger_builder_index')

@user_passes_test(lambda u: u.is_superuser)
def script_runner_index(request):
    munger_builder_list = MungerBuilder.objects.order_by('id')
    context = {'munger_builder_list': munger_builder_list}
    return render(request, 'script_runner/script_runner_index.html', context)

@user_passes_test(lambda u: u.is_superuser)
def munger_builder_index(request):
    munger_builder_list = MungerBuilder.objects.order_by('id')
    context = {'munger_builder_list': munger_builder_list}
    return render(request, 'script_runner/munger_builder_index.html', context)

@user_passes_test(lambda u: u.is_superuser)
def run_munger_output(request, munger_builder_id):
    pretext_url = reverse('pivot_builder', args=[munger_builder_id])
    pretext = "<p><a class=back-link href=\"{0}\">< Munger Tools</a></p>".format(pretext_url)
    return StreamingHttpResponse(
        content_generator(run_munger.main(munger_builder_id), pretext=pretext)
    )

def build_munger_output(request, munger_builder_id):

    mb = MungerBuilder.objects.get(pk=munger_builder_id)
    if not mb.user_is_authorized():
        return INDEX_REDIRECT

    print(mb.munger_template)


# Path: scripts/run_munger.py
# def print_run_status(run_start_time, message):
# def main(munger_builder_id=1):
# 
# Path: scripts/build_munger.py
# def main(munger_builder_id=1):
# 
# Path: script_builder/models.py
# class MungerBuilder(models.Model, PermissionedModel):
# 
#     class Meta:
#         permissions = (
#             ('view_mungerbuilder', 'View Munger'),
#         )
# 
#     munger_name = models.CharField(max_length=200)
# 
#     munger_template = models.FilePathField(path='script_builder/templates/munger_templates', max_length=200,
#                                            default='pandas_munger_template_basic.html')
# 
#     input_path = models.CharField(max_length=999, default='', blank=True)
#     output_path = models.CharField(max_length=999, default='', blank=True)
# 
#     rows_to_delete_top = models.IntegerField(default=0)
#     rows_to_delete_bottom = models.IntegerField(default=0)
# 
#     field_types = models.ManyToManyField(FieldType, related_name='munger_builder', related_query_name='munger_builder')
# 
#     default_aggregate_field_type = models.ForeignKey(FieldType, default=3, limit_choices_to={'pk__gt': 2},)
#     is_sample = models.BooleanField(default=False)
# 
#     def save(self, *args, **kwargs):
#         super().save(*args, **kwargs)
#         # Always add default field types unless set from admin
#         self.field_types.add(*(field_type for field_type in FieldType.default_field_types()))
#         self.assign_perms(current_user())
# 
#     def user_is_authorized(self):
#         return self.id == 1 or current_user().has_perm('script_builder.change_mungerbuilder', self)
# 
#     @property
#     def pivot_fields(self):
#         return PivotField.objects.filter(data_field__munger_builder__id=self.id)
# 
#     @property
#     def index_fields(self):
#         return [pf.active_name for pf in self.pivot_fields.filter(field_type__id=1)]
# 
#     @property
#     def column_fields(self):
#         return [pf.active_name for pf in self.pivot_fields.filter(field_type__id=2)]
# 
#     def aggregate_names_with_functions(self, evaled=False):
#         # Needs to be ordered dicts
#         func = eval if evaled else str
#         aggregates_dict = defaultdict(list)
#         for pf in self.pivot_fields.filter(field_type__id__gt=2):
#             aggregates_dict[pf.active_name].append(pf.type_function)
#         return {name: func(', '.join(type_functions)) for name, type_functions in aggregates_dict.items()}
# 
#     @property
#     def rename_field_dict(self):
#         return {field.current_name: field.new_name for field in self.data_fields.all() if field.needs_rename}
# 
#     @property
#     def safe_file_name(self):
#         return self.munger_name.replace(' ', '_').lower()
# 
#     @property
#     def get_output_path(self):
#         if self.output_path:
#             return self.output_path
#         else:
#             input_dir = os.path.dirname(self.input_path)
#             return os.path.join(input_dir, '{0}-output.csv'.format(self.safe_file_name))
# 
#     def __str__(self):
#         return self.munger_name


# Path: script_runner/views.py
import time
from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from django.contrib.auth.decorators import login_required, user_passes_test
from django.contrib import messages
from django.http import StreamingHttpResponse
from django.core.urlresolvers import reverse
from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter
from scripts import run_munger, build_munger
from script_builder.models import MungerBuilder

INDEX_REDIRECT = HttpResponseRedirect('/script_builder/munger_builder_index')

@user_passes_test(lambda u: u.is_superuser)
def script_runner_index(request):
    munger_builder_list = MungerBuilder.objects.order_by('id')
    context = {'munger_builder_list': munger_builder_list}
    return render(request, 'script_runner/script_runner_index.html', context)

@user_passes_test(lambda u: u.is_superuser)
def munger_builder_index(request):
    munger_builder_list = MungerBuilder.objects.order_by('id')
    context = {'munger_builder_list': munger_builder_list}
    return render(request, 'script_runner/munger_builder_index.html', context)

@user_passes_test(lambda u: u.is_superuser)
def run_munger_output(request, munger_builder_id):
    pretext_url = reverse('pivot_builder', args=[munger_builder_id])
    pretext = "<p><a class=back-link href=\"{0}\">< Munger Tools</a></p>".format(pretext_url)
    return StreamingHttpResponse(
        content_generator(run_munger.main(munger_builder_id), pretext=pretext)
    )

def build_munger_output(request, munger_builder_id):

    mb = MungerBuilder.objects.get(pk=munger_builder_id)
    if not mb.user_is_authorized():
        return INDEX_REDIRECT

    print(mb.munger_template)


    script_string = build_munger.main(munger_builder_id)