# Path: apps/spider/models.py
# class CrawledData(models.Model):
#     MEDIANS = "medians"
#     ORC_DEPARTMENT_COURSES = "orc_department_courses"
#     COURSE_TIMETABLE = "course_timetable"
#     DATA_TYPE_CHOICES = (
#         (MEDIANS, "Medians"),
#         (ORC_DEPARTMENT_COURSES, "ORC Department Courses"),
#         (COURSE_TIMETABLE, "Course Timetable"),
#     )
#     objects = CrawledDataManager()
# 
#     resource = models.CharField(max_length=128, db_index=True, unique=True)
#     data_type = models.CharField(max_length=32, choices=DATA_TYPE_CHOICES)
#     pending_data = JSONField()
#     current_data = JSONField(null=True)
# 
#     created_at = models.DateTimeField(auto_now_add=True)
#     updated_at = models.DateTimeField(auto_now=True)
# 
#     def __unicode__(self):
#         return "[{data_type}] {resource}".format(
#             data_type=self.data_type,
#             resource=self.resource,
#         )
# 
#     def has_change(self):
#         return self.pending_data != self.current_data
# 
#     @property
#     def diff(self):
#         if not self.current_data:
#             return utils.pretty_json(self.pending_data)
#         if self.has_change:
#             return "\n".join(difflib.unified_diff(
#                 utils.pretty_json(self.current_data).splitlines(),
#                 utils.pretty_json(self.pending_data).splitlines(),
#             ))
# 
#     @property
#     def pretty_current_data(self):
#         return utils.pretty_json(self.current_data)
# 
#     def email_change(self):
#         assert self.has_change()
#         send_mail(
#             "[{type}][{resource}][{pk}] New Import".format(
#                 type=self.data_type,
#                 resource=self.resource,
#                 pk=self.pk,
#             ),
#             self.diff,
#             constants.SUPPORT_EMAIL,
#             [email for _, email in settings.ADMINS],
#             fail_silently=False,
#         )
# 
#     def approve_change(self):
#         from apps.spider.tasks import import_pending_crawled_data
#         import_pending_crawled_data.delay(self.pk)


from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.auth.decorators import user_passes_test
from django.db.models import F
from django.shortcuts import redirect, render
from apps.spider.models import CrawledData



@staff_member_required
@user_passes_test(lambda u: u.is_superuser)
def crawled_data_list(request):
    if request.method == 'POST':
        for crawled_data in CrawledData.objects.all():
            if crawled_data.has_change():
                crawled_data.approve_change()
    return render(request, "crawled_data_list.html", {

# Path: apps/spider/models.py
# class CrawledData(models.Model):
#     MEDIANS = "medians"
#     ORC_DEPARTMENT_COURSES = "orc_department_courses"
#     COURSE_TIMETABLE = "course_timetable"
#     DATA_TYPE_CHOICES = (
#         (MEDIANS, "Medians"),
#         (ORC_DEPARTMENT_COURSES, "ORC Department Courses"),
#         (COURSE_TIMETABLE, "Course Timetable"),
#     )
#     objects = CrawledDataManager()
# 
#     resource = models.CharField(max_length=128, db_index=True, unique=True)
#     data_type = models.CharField(max_length=32, choices=DATA_TYPE_CHOICES)
#     pending_data = JSONField()
#     current_data = JSONField(null=True)
# 
#     created_at = models.DateTimeField(auto_now_add=True)
#     updated_at = models.DateTimeField(auto_now=True)
# 
#     def __unicode__(self):
#         return "[{data_type}] {resource}".format(
#             data_type=self.data_type,
#             resource=self.resource,
#         )
# 
#     def has_change(self):
#         return self.pending_data != self.current_data
# 
#     @property
#     def diff(self):
#         if not self.current_data:
#             return utils.pretty_json(self.pending_data)
#         if self.has_change:
#             return "\n".join(difflib.unified_diff(
#                 utils.pretty_json(self.current_data).splitlines(),
#                 utils.pretty_json(self.pending_data).splitlines(),
#             ))
# 
#     @property
#     def pretty_current_data(self):
#         return utils.pretty_json(self.current_data)
# 
#     def email_change(self):
#         assert self.has_change()
#         send_mail(
#             "[{type}][{resource}][{pk}] New Import".format(
#                 type=self.data_type,
#                 resource=self.resource,
#                 pk=self.pk,
#             ),
#             self.diff,
#             constants.SUPPORT_EMAIL,
#             [email for _, email in settings.ADMINS],
#             fail_silently=False,
#         )
# 
#     def approve_change(self):
#         from apps.spider.tasks import import_pending_crawled_data
#         import_pending_crawled_data.delay(self.pk)


# Path: apps/spider/views.py
from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.auth.decorators import user_passes_test
from django.db.models import F
from django.shortcuts import redirect, render
from apps.spider.models import CrawledData




@staff_member_required
@user_passes_test(lambda u: u.is_superuser)
def crawled_data_list(request):
    if request.method == 'POST':
        for crawled_data in CrawledData.objects.all():
            if crawled_data.has_change():
                crawled_data.approve_change()
    return render(request, "crawled_data_list.html", {

        "crawled_datas": CrawledData.objects.sorted(),