Example #1
0
    def migrate(self, data, branch_slug=None):
        do_save = True
        if branch_slug is not None:
            do_save = False

            if TeachersXClasses.objects.filter(
                    teacher_id=int(data['id'])).exists():
                class_old_join_rows = TeachersXClasses.objects.filter(
                    teacher_id=int(data['id']))
                for class_old_join_row in class_old_join_rows:
                    try:
                        old_class_row = Classes.objects.get(
                            pk=class_old_join_row.class_id)
                        try:
                            venue = Venue.objects.get(
                                pk=old_class_row.venue_id)
                            try:
                                branch = Branch.objects.get(pk=venue.branch_id)
                                if branch.slug.lower() == branch_slug.lower():
                                    do_save = True
                            except Branch.DoesNotExist:
                                pass
                        except Venue.DoesNotExist:
                            pass
                    except Classes.DoesNotExist:
                        pass

        if do_save is True:
            slug = slugify(Person, data['fullname'])

            try:
                teacher = Person.objects.get(email=data['email'].lower())
                print "     found Person: [%s]" % teacher
            except Person.DoesNotExist:
                teacher = Person.objects.create_user(
                    pk=int(data['id']),
                    fullname=data['fullname'],
                    email=data['email'].lower(),
                    phone=data['phone'],
                    bio=data['bio'],
                    website=data['website'],
                    slug=slug,
                    created=timezone.make_aware(data['timestamp'], timezone.utc),
                    updated=timezone.make_aware(data['timestamp'], timezone.utc),
                )
                teacher.save()
                print "     saved Person: [%s]" % teacher
Example #2
0
    def migrate(self, data, branch_slug=None):

        do_save = True
        if branch_slug is not None:
            do_save = False
            if StudentsXClasses.objects.filter(
                    student_id=int(data['id'])).exists():
                class_old_join_rows = StudentsXClasses.objects.filter(
                    student_id=int(data['id']))
                for class_old_join_row in class_old_join_rows:
                    try:
                        old_class_row = Classes.objects.get(
                            pk=class_old_join_row.class_id)
                        try:
                            venue = Venue.objects.get(
                                pk=old_class_row.venue_id)
                            try:
                                branch = Branch.objects.get(pk=venue.branch_id)
                                if branch.slug.lower() == branch_slug.lower():
                                    do_save = True
                            except Branch.DoesNotExist:
                                pass
                        except Venue.DoesNotExist:
                            pass
                    except Classes.DoesNotExist:
                        pass

        print "     saving: %s" % do_save

        if do_save is True:
            # auto generate slug field
            slug = slugify(Person, data['fullname'])

            # we're not copyig the id, to not clash with migrated teachers,
            # as both are represented with the Person model
            student = Person.objects.filter(email=data['email'].lower())
            if student.exists() is False:
                student = Person.objects.create_user(
                    fullname=data['fullname'],
                    email=data['email'].lower(),
                    phone=data['phone'],
                    slug=slug,
                    created=timezone.make_aware(data['timestamp'], timezone.utc),
                    updated=timezone.make_aware(data['timestamp'], timezone.utc)
                )
                student.save()
                print "     saved Person: [%s]" % student
            else:
                student = Person.objects.get(email=data['email'].lower())
                print "     found Person: [%s]" % student

            # create student registrations
            if StudentsXClasses.objects.filter(
                    student_id=int(data['id'])).exists():
                class_old_join_rows = StudentsXClasses.objects.filter(
                    student_id=int(data['id']))
                for class_old_join_row in class_old_join_rows:
                    class_old_row = Classes.objects.get(
                        pk=class_old_join_row.class_id)
                    try:
                        course = Course.objects.get(
                            title=class_old_row.title)
                        print "         found Course: [%s]" % course

                        registration = Registration.objects.filter(
                            course=course,
                            student=student
                        )
                        status = 'registered'
                        if class_old_join_row.status == 1:
                            status = 'unregistered'

                        if registration.exists() is False:
                            registration = Registration(
                                course=course,
                                student=student,
                                registration_status=status,
                                created=data['timestamp']
                            )
                            registration.save()
                            #print "         saved Registration: [%s]" % registration
                        else:
                            registration = Registration.objects.get(
                                course=course, student=student)
                            registration.registration_status = status
                            registration.save()
                            #print "         found Registration: [%s]" % registration

                        # save branch id in student Person object
                        branch = registration.course.branch
                        #print "             found Branch: [%s]" % branch

                        student.branches.add(branch)
                        student.save()


                        # create student items
                        if StudentsXItems.objects.filter(
                                student_id=int(data['id'])).exists():
                            item_old_join_rows = StudentsXItems.objects.filter(
                                student_id=int(data['id']))
                            #print "             found row: [%s]" % item_old_join_rows

                            for item_old_join_row in item_old_join_rows:

                                if ClassesXItems.objects.filter(
                                        class_id=course.pk, item_id=item_old_join_row.item_id).exists():
                                    
                                    class_old_join_rows = ClassesXItems.objects.filter(class_id=course.pk, item_id=item_old_join_row.item_id)
                                    for class_old_row in class_old_join_rows:

                                        print course.pk
                                        print class_old_row.class_id

                                        if course.pk == class_old_row.class_id:

                                            try:
                                                barter_item = BarterItem.objects.get(
                                                    pk=item_old_join_row.item_id)
                                                registration.items.add(barter_item)
                                                registration.save()
                                            except BarterItem.DoesNotExist:
                                                pass
                    except Course.DoesNotExist:
                        pass
Example #3
0
    def migrate(self, data, branch_slug=None):
        # find teacher object according to old join classes
        if TeachersXClasses.objects.filter(class_id=int(data['id'])).exists():
            old_join_row = TeachersXClasses.objects.get(
                class_id=int(data['id']))

            do_save = True
            if branch_slug is not None:
                do_save = False
                try:
                    old_class_row = Classes.objects.get(
                        pk=old_join_row.class_id)
                    try:
                        venue = Venue.objects.get(pk=old_class_row.venue_id)
                        try:
                            branch = Branch.objects.get(pk=venue.branch_id)
                            if branch.slug.lower() == branch_slug.lower():
                                do_save = True
                        except Branch.DoesNotExist:
                            pass
                    except Venue.DoesNotExist:
                        pass
                except Classes.DoesNotExist:
                    pass

            print "     saving: %s" % do_save

            if do_save is True:
                old_teacher_row = Teachers.objects.get(
                    pk=old_join_row.teacher_id)

                teacher = Person.objects.get(
                    email=old_teacher_row.email.lower())

                # use django slugify to generate a slug
                slug = slugify(Course, data['title'])

                # fake a default category value
                if data['category_id'] is None:
                    category = 0
                else:
                    category = data['category_id']

                color = '#cc3333'
                if category == 0:
                    color = '#cc3333'
                elif category == 1:
                    color = '#e26521'
                elif category == 2:
                    color = '#dda51e'
                elif category == 3:
                    color = '#74ac23'
                elif category == 4:
                    color = '#2da57c'
                elif category == 5:
                    color = '#2d9ac2'
                elif category == 6:
                    color = '#8a54bb'

                # create course
                course = Course.objects.filter(title=data['title'])

                if course.exists() is False:

                    # get venue object for the Course venue foreign key field
                    # and for the branch's timezone
                    if Venue.objects.filter(pk=data['venue_id']).exists():
                        venue = Venue.objects.get(pk=data['venue_id'])
                        print "         in venue: [%s]" % venue

                        branch = Branch.objects.get(pk=venue.branch.id)
                        print "         in branch: [%s]" % branch

                        # save branch to teacher
                        teacher.branches.add(branch)
                        teacher.save()

                        # convert the old unix time values (a bigint)
                        # to a timezone-aware datetime object
                        import datetime
                        import pytz

                        start_time_naive = datetime.datetime.fromtimestamp(
                            int(data['unix_start_time'])
                        )
                        end_time_naive = datetime.datetime.fromtimestamp(
                            int(data['unix_end_time'])
                        )

                        tz = pytz.timezone(branch.timezone)

                        aware_start_time = tz.normalize(
                            tz.localize(start_time_naive)).astimezone(pytz.utc)
                        aware_end_time = tz.normalize(
                            tz.localize(end_time_naive)).astimezone(pytz.utc)

                        print "     course start time: [%s]" % aware_start_time
                        print "     course end time: [%s]" % aware_end_time

                        if data['status'] == 0:
                            status = 'pending'
                        elif data['status'] == 1:
                            status = 'contacted'
                        elif data['status'] == 2:
                            status = 'updated'
                        elif data['status'] == 3:
                            status = 'approved'
                        elif data['status'] == 4:
                            status = 'rejected'
                        else:
                            status = 'pending'

                        print 'saving new course'
                        course = Course(
                            pk=int(data['id']),
                            title=data['title'],
                            branch=branch,
                            venue=venue,
                            teacher=teacher,
                            description=data['description'],
                            max_students=int(data['max_students']),
                            slug=slug,
                            color=color,
                            status=status,
                            start_time=aware_start_time,
                            end_time=aware_end_time,
                            created=timezone.make_aware(data['timestamp'], timezone.utc),
                            updated=timezone.make_aware(data['timestamp'], timezone.utc),                            
                        )
                        course.save()

                        print "     saved Course: [%s]" % course
                else:
                    course = Course.objects.get(title=data['title'])
                    print "     found Course: [%s]" % course

                # create the related barter items
                # and add them to the course item
                if Venue.objects.filter(pk=data['venue_id']).exists():

                    if ClassesXItems.objects.filter(class_id=int(data['id'])).exists():

                        item_old_join_rows = ClassesXItems.objects.filter(class_id=int(data['id']))

                        print "    items: %i" % item_old_join_rows.count()

                        for item_old_join_row in item_old_join_rows:

                            try:
                                item = Items.objects.get(pk=item_old_join_row.item_id)
                                barter_item = BarterItem.objects.filter(pk=item.id)

                                try:
                                    barter_item = BarterItem.objects.get(pk=item.id)
                                    barter_item.course = course
                                    barter_item.save()

                                except BarterItem.DoesNotExist:
                                    barter_item = BarterItem(
                                        pk=item.id,
                                        title=item.title,
                                        course=course,
                                        created=timezone.make_aware(data['timestamp'], timezone.utc),
                                    )
                                    barter_item.save()
                            except Items.DoesNotExist:
                                pass