def handle(self, *args, **options):
        """
        The actual logic of the command. Subclasses must implement this 
        method. It may return a Unicode string which will be printed to 
        stdout. More: https://docs.djangoproject.com/en/1.8/howto/custom
        -management-commands/#django.core.management.BaseCommand.handle
        """

        try:
            cnetid = options['cnetid']
        except:
            sys.exit(1)
    
        staff_index_path = StaffIndexPage.objects.first().path
        staff_index_depth = StaffIndexPage.objects.first().depth
        staff_index_url = StaffIndexPage.objects.first().url
        staff_index_content_type_pk = ContentType.objects.get(model='staffindexpage').pk
        staff_content_type_pk = ContentType.objects.get(model='staffpage').pk
  
        xml_string = get_xml_from_directory_api('https://directory.uchicago.edu/api/v2/individuals/' + cnetid + '.xml') 
        info = get_individual_info_from_directory(xml_string)
        next_available_path = get_available_path_under(staff_index_path)

        # Update a StaffPage
        # works with just 'title' set as a default. 
        # setting path destroyed the staff index!!!
        # 'path': next_available_path

        if StaffPage.objects.filter(cnetid=info['cnetid']):
            sp, created = StaffPage.objects.update_or_create(
                cnetid=info['cnetid'],
                defaults = {
                    'title': info['displayName'],
                    'display_name': info['displayName'],
                    'official_name': info['officialName'],
                    'slug': make_slug(info['displayName']),
                    'url_path': '/loop/staff/' + make_slug(info['displayName']) + '/',
                    'depth': staff_index_depth + 1,
                })
            StaffIndexPage.objects.first().fix_tree(destructive=False)
        else:
            StaffPage.objects.create(
            title=info['displayName'],
            slug=make_slug(info['displayName']),
            path=next_available_path,
            depth=len(next_available_path) // 4,
            numchild=0,
            url_path='/staff/' + make_slug(info['displayName']) + '/',
            cnetid=info['cnetid'],
            display_name=info['displayName'],
            official_name=info['officialName'],
            )

        # Add new VCards
        for vcard in info['title_department_subdepartments_dicts']:
            faculty_exchange = ''
            if hasattr(vcard, 'facultyexchange'):
                faculty_exchange = vcard['facultyexchange']

            email = ''
            if hasattr(vcard, 'email'):
                email = vcard['email']

            phone_label = ''
            phone_number = ''
            if hasattr(vcard, 'phone'):
                phone_label = 'work'
                phone_number = vcard['phone']

            v, created = StaffPagePageVCards.objects.get_or_create(
                title=vcard['title'], 
                unit=DirectoryUnit.objects.get(pk=vcard['department']), 
                faculty_exchange=faculty_exchange,
                email=email,
                phone_label=phone_label,
                phone_number=phone_number,
                page=StaffPage.objects.get(cnetid=cnetid))

        # Delete unnecesary VCards
        for vcard in StaffPage.objects.get(cnetid=info['cnetid']).vcards.all():
            d = {
                'title': vcard.title,
                'department': vcard.unit.id,
                'facultyexchange': vcard.faculty_exchange,
                'email': vcard.email,
                'phone': vcard.phone_number
            }
            if not d in info['title_department_subdepartments_dicts']:
                vcard.delete()
        
        staff_page = StaffPage.objects.get(cnetid=info['cnetid'])
        staff_page.page_maintainer = staff_page
        staff_page.save()
Exemplo n.º 2
0
import base64
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'library_website.settings'

group_meeting_minutes_content_type = apps.get_model(
    'contenttypes.ContentType').objects.get(model='groupmeetingminutespage',
                                            app_label='group')
group_reports_content_type = apps.get_model(
    'contenttypes.ContentType').objects.get(model='groupreportspage',
                                            app_label='group')
intranet_units_reports_content_type = apps.get_model(
    'contenttypes.ContentType').objects.get(model='intranetunitsreportspage',
                                            app_label='intranetunits')

for g in GroupPage.objects.all():
    next_available_path = get_available_path_under(g.path)
    title = "Meeting Minutes"
    GroupMeetingMinutesPage.objects.create(
        title=title,
        slug=make_slug(title),
        content_type=group_meeting_minutes_content_type,
        path=next_available_path,
        depth=g.depth + 1,
        numchild=0,
        url_path=g.url + make_slug(title) + '/')

    next_available_path = get_available_path_under(g.path)
    title = "Reports"
    GroupReportsPage.objects.create(title=title,
                                    slug=make_slug(title),
                                    content_type=group_reports_content_type,
    def handle(self, *args, **options):
        """
        The actual logic of the command. Subclasses must implement this 
        method. It may return a Unicode string which will be printed to 
        stdout. More: https://docs.djangoproject.com/en/1.8/howto/custom
        -management-commands/#django.core.management.BaseCommand.handle
        """

        # Convenience variables for required fields.
        # Save in a dict so we can pass an unspecified
        # numer or arguments to the get_or_create method.
        try:
            kwargs = {'path': options['path']}
        except:
            sys.exit(1)

        # Make sure the path begins and ends with a slash.
        if not kwargs['path'][0] == '/':
            kwargs['path'] = '/' + kwargs['path']

        if not kwargs['path'][-1] == '/':
            kwargs['path'] = kwargs['path'] + '/'

        # GROUP PAGES
        if '/groups/' in kwargs['path']:
            # MEETING MINUTES
            try:
                group_page = GroupPage.objects.get(url_path='/loop' +
                                                   kwargs['path'])
                group_meeting_minutes_page = GroupMeetingMinutesPage.objects.descendant_of(
                    group_page).get()
            except:
                return "The group page could not be found."

            if GroupMeetingMinutesIndexPage.objects.descendant_of(
                    group_page).count() == 0:
                group_meeting_minutes_page.title = '2016'
                group_meeting_minutes_page.slug = '2016'
                group_meeting_minutes_page.set_url_path(group_page)
                group_meeting_minutes_page.save()

                # Create a new group meeting minutes index page.
                group_meeting_minutes_index_page = GroupMeetingMinutesIndexPage.objects.create(
                    depth=group_page.depth + 1,
                    numchild=0,
                    path=get_available_path_under(group_page.path),
                    slug='meeting-minutes',
                    title='Meeting Minutes',
                    url_path='/loop' + kwargs['path'] + 'meeting-minutes/')

                group_meeting_minutes_page.move(
                    group_meeting_minutes_index_page, 'first-child')

            # GROUP REPORTS
            try:
                group_reports_page = GroupReportsPage.objects.descendant_of(
                    group_page).get()
            except:
                return "The group page could not be found."

            if GroupReportsIndexPage.objects.descendant_of(
                    group_page).count() == 0:
                group_reports_page.title = '2016'
                group_reports_page.slug = '2016'
                group_reports_page.set_url_path(group_page)
                group_reports_page.save()

                # create a group reports index page here- call it "Reports".
                group_reports_index_page = GroupReportsIndexPage.objects.create(
                    depth=group_page.depth + 1,
                    numchild=0,
                    path=get_available_path_under(group_page.path),
                    slug='reports',
                    title='Reports',
                    url_path='/loop' + kwargs['path'] + 'reports/')

                # move 2016 into "Reports".
                group_reports_page.move(group_reports_index_page,
                                        'first-child')

            # fix paths.
            group_page.fix_tree(destructive=False)

        # DEPARTMENT PAGES
        elif '/departments/' in kwargs['path']:
            try:
                department_page = IntranetUnitsPage.objects.get(
                    url_path='/loop' + kwargs['path'])
                department_reports_page = IntranetUnitsReportsPage.objects.child_of(
                    department_page).get()
            except:
                return "The department page could not be found."

            if IntranetUnitsReportsIndexPage.objects.child_of(
                    department_page).count() == 0:
                department_reports_page.title = '2016'
                department_reports_page.slug = '2016'
                department_reports_page.set_url_path(department_page)
                department_reports_page.save()

                # create a department reports index page here- call it "Reports".
                department_reports_index_page = IntranetUnitsReportsIndexPage.objects.create(
                    depth=department_page.depth + 1,
                    numchild=0,
                    path=get_available_path_under(department_page.path),
                    slug='reports',
                    title='Reports',
                    url_path='/loop' + kwargs['path'] + 'reports/')

                # move 2016 into "Reports".
                department_reports_page.move(department_reports_index_page,
                                             'first-child')

                # fix paths.
                department_page.fix_tree(destructive=False)

        return "SUCCESS: " + kwargs['path']
from base.models import get_available_path_under, make_slug
from django.apps import apps
from group.models import GroupPage, GroupMeetingMinutesPage, GroupReportsPage
from intranetunits.models import IntranetUnitsPage, IntranetUnitsReportsPage

import base64
import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'library_website.settings'

group_meeting_minutes_content_type = apps.get_model('contenttypes.ContentType').objects.get(model='groupmeetingminutespage', app_label='group')
group_reports_content_type = apps.get_model('contenttypes.ContentType').objects.get(model='groupreportspage', app_label='group')
intranet_units_reports_content_type = apps.get_model('contenttypes.ContentType').objects.get(model='intranetunitsreportspage', app_label='intranetunits')

for g in GroupPage.objects.all():
    next_available_path = get_available_path_under(g.path)
    title = "Meeting Minutes"
    GroupMeetingMinutesPage.objects.create(
        title=title,
        slug=make_slug(title),
        content_type=group_meeting_minutes_content_type,
        path=next_available_path,
        depth=g.depth + 1,
        numchild=0,
        url_path=g.url + make_slug(title) + '/'
    )

    next_available_path = get_available_path_under(g.path)
    title = "Reports"
    GroupReportsPage.objects.create(
        title=title,
Exemplo n.º 5
0
    def test_staff_listing_stream_fields(self):
        # get a few pages for the test.
        home_page = Site.objects.first().root_page
        staff_index_page = StaffIndexPage.objects.first()
        alien = StaffPage.objects.live().first()

        try:
            StaffPage.objects.get(cnetid='ignatius').delete()
        except StaffPage.DoesNotExist:
            pass

        # create a fictional StaffPage object for testing.
        staff_page = StaffPage.objects.create(cnetid='ignatius',
                                              depth=staff_index_page.depth + 1,
                                              path=get_available_path_under(
                                                  staff_index_page.path),
                                              slug='ignatius-reilly',
                                              title='Ignatius Reilly')

        # build a streamfield by hand for testing.
        body = json.dumps([{
            "type": "staff_listing",
            "value": {
                "staff_listing": [staff_page.id],
                "show_photos": False,
                "show_contact_info": False,
                "show_subject_specialties": False
            }
        }])

        try:
            StandardPage.objects.get(slug='a-standard-page').delete()
        except StandardPage.DoesNotExist:
            pass

        # create a StandardPage under the homepage for testing.
        standard_page = StandardPage.objects.create(
            body=body,
            content_specialist=staff_page,
            depth=home_page.depth + 1,
            editor=alien,
            page_maintainer=staff_page,
            path=get_available_path_under(home_page.path),
            slug="a-standard-page",
            title="A Standard Page",
            unit=UnitPage.objects.get(title="Library"))

        # retrieve the StandardPage and make sure the status code is 200.
        request = HttpRequest()
        add_generic_request_meta_fields(request)
        response = standard_page.serve(request)
        self.assertEqual(response.status_code, 200)

        # delete the StaffPage object. Test again, the status code should still be
        # 200.
        try:
            staff_page.delete()
        except StaffPage.DoesNotExist:
            pass

        response = standard_page.serve(request)
        self.assertEqual(response.status_code, 200)

        # delete the StandardPage.
        try:
            standard_page.delete()
        except StandardPage.DoesNotExist:
            pass
Exemplo n.º 6
0
    def test_staff_listing_stream_fields(self):
        # get a few pages for the test. 
        home_page = Site.objects.first().root_page
        staff_index_page = StaffIndexPage.objects.first()
        dbietila = StaffPage.objects.get(cnetid='dbietila')
    
        try:
            StaffPage.objects.get(cnetid='ignatius').delete()
        except StaffPage.DoesNotExist:
            pass
    
        # create a fictional StaffPage object for testing. 
        staff_page = StaffPage.objects.create(
            cnetid='ignatius',
            depth=staff_index_page.depth+1,
            path=get_available_path_under(staff_index_page.path),
            slug='ignatius-reilly',
            title='Ignatius Reilly'
        )
    
        # build a streamfield by hand for testing. 
        body = json.dumps([
            {
                "type": "staff_listing",
                "value": {
                    "staff_listing": [staff_page.id],
                    "show_photos": False,
                    "show_contact_info": False,
                    "show_subject_specialties": False
                }
            }
        ])

        try:
            StandardPage.objects.get(slug='a-standard-page').delete()
        except StandardPage.DoesNotExist:
            pass
    
        # create a StandardPage under the homepage for testing. 
        standard_page = StandardPage.objects.create(
            body=body,
            content_specialist=staff_page,
            depth=home_page.depth+1,
            editor=dbietila,
            page_maintainer=staff_page,
            path=get_available_path_under(home_page.path),
            slug="a-standard-page",
            title="A Standard Page",
            unit=UnitPage.objects.get(title="Library")
        )
    
        # retrieve the StandardPage and make sure the status code is 200. 
        request = HttpRequest()
        response = standard_page.serve(request)
        self.assertEqual(response.status_code, 200)
    
        # delete the StaffPage object. Test again, the status code should still be
        # 200.
        try:
            staff_page.delete()
        except StaffPage.DoesNotExist:
            pass

        response = standard_page.serve(request)
        self.assertEqual(response.status_code, 200)
    
        # delete the StandardPage. 
        try:
            standard_page.delete()
        except StandardPage.DoesNotExist:
            pass
    def handle(self, *args, **options):
        """
        The actual logic of the command. Subclasses must implement this 
        method. It may return a Unicode string which will be printed to 
        stdout. More: https://docs.djangoproject.com/en/1.8/howto/custom
        -management-commands/#django.core.management.BaseCommand.handle
        """

        try:
            cnetid = options['cnetid']
        except:
            sys.exit(1)

        staff_index_path = StaffIndexPage.objects.first().path
        staff_index_depth = StaffIndexPage.objects.first().depth
        staff_index_url = StaffIndexPage.objects.first().url
        staff_index_content_type_pk = ContentType.objects.get(
            model='staffindexpage').pk
        staff_content_type_pk = ContentType.objects.get(model='staffpage').pk

        xml_string = get_xml_from_directory_api(
            'https://directory.uchicago.edu/api/v2/individuals/' + cnetid +
            '.xml')
        info = get_individual_info_from_directory(xml_string)
        next_available_path = get_available_path_under(staff_index_path)

        # Update a StaffPage
        # works with just 'title' set as a default.
        # setting path destroyed the staff index!!!
        # 'path': next_available_path

        if StaffPage.objects.filter(cnetid=info['cnetid']):
            sp, created = StaffPage.objects.update_or_create(
                cnetid=info['cnetid'],
                defaults={
                    'title': info['displayName'],
                    'display_name': info['displayName'],
                    'official_name': info['officialName'],
                    'slug': make_slug(info['displayName']),
                    'url_path':
                    '/loop/staff/' + make_slug(info['displayName']) + '/',
                    'depth': staff_index_depth + 1,
                })
            StaffIndexPage.objects.first().fix_tree(destructive=False)
        else:
            StaffPage.objects.create(
                title=info['displayName'],
                slug=make_slug(info['displayName']),
                path=next_available_path,
                depth=len(next_available_path) // 4,
                numchild=0,
                url_path='/staff/' + make_slug(info['displayName']) + '/',
                cnetid=info['cnetid'],
                display_name=info['displayName'],
                official_name=info['officialName'],
            )

        # Add new contact information.
        # for contact_info in info['title_department_subdepartments_dicts']:
        # add new email addresses, phone faculty exchange pairs,
        # telephone numbers.

        # Delete unnecesary contact information.

        staff_page = StaffPage.objects.get(cnetid=info['cnetid'])
        staff_page.page_maintainer = staff_page
        staff_page.save()
    def handle(self, *args, **options):
        """
        The actual logic of the command. Subclasses must implement this 
        method. It may return a Unicode string which will be printed to 
        stdout. More: https://docs.djangoproject.com/en/1.8/howto/custom
        -management-commands/#django.core.management.BaseCommand.handle
        """

        # Convenience variables for required fields.
        # Save in a dict so we can pass an unspecified
        # numer or arguments to the get_or_create method.
        try:
            kwargs = {
                'path': options['path']
            }
        except:
            sys.exit(1)

        # Make sure the path begins and ends with a slash.
        if not kwargs['path'][0] == '/':
            kwargs['path'] = '/' + kwargs['path']

        if not kwargs['path'][-1] == '/':
            kwargs['path'] = kwargs['path'] + '/'

        # GROUP PAGES
        if '/groups/' in kwargs['path']:
            # MEETING MINUTES
            try:
                group_page = GroupPage.objects.get(url_path='/loop' + kwargs['path'])
                group_meeting_minutes_page = GroupMeetingMinutesPage.objects.descendant_of(group_page).get()
            except:
                return "The group page could not be found."
    
            if GroupMeetingMinutesIndexPage.objects.descendant_of(group_page).count() == 0:
                group_meeting_minutes_page.title = '2016'
                group_meeting_minutes_page.slug = '2016'
                group_meeting_minutes_page.set_url_path(group_page)
                group_meeting_minutes_page.save()
        
                # Create a new group meeting minutes index page. 
                group_meeting_minutes_index_page = GroupMeetingMinutesIndexPage.objects.create(
                    depth=group_page.depth + 1,
                    numchild=0,
                    path=get_available_path_under(group_page.path),
                    slug='meeting-minutes',
                    title='Meeting Minutes',
                    url_path='/loop' + kwargs['path'] + 'meeting-minutes/'
                )
           
                group_meeting_minutes_page.move(group_meeting_minutes_index_page, 'first-child')
    
            # GROUP REPORTS
            try:
                group_reports_page = GroupReportsPage.objects.descendant_of(group_page).get()
            except:
                return "The group page could not be found."
    
            if GroupReportsIndexPage.objects.descendant_of(group_page).count() == 0:
                group_reports_page.title = '2016'
                group_reports_page.slug = '2016'
                group_reports_page.set_url_path(group_page)
                group_reports_page.save()
        
                # create a group reports index page here- call it "Reports".
                group_reports_index_page = GroupReportsIndexPage.objects.create(
                    depth=group_page.depth + 1,
                    numchild=0,
                    path=get_available_path_under(group_page.path),
                    slug='reports',
                    title='Reports',
                    url_path='/loop' + kwargs['path'] + 'reports/'
                )
        
                # move 2016 into "Reports".
                group_reports_page.move(group_reports_index_page, 'first-child')
    
            # fix paths.
            group_page.fix_tree(destructive=False)

        # DEPARTMENT PAGES
        elif '/departments/' in kwargs['path']:
            try:
                department_page = IntranetUnitsPage.objects.get(url_path='/loop' + kwargs['path'])
                department_reports_page = IntranetUnitsReportsPage.objects.child_of(department_page).get()
            except:
                return "The department page could not be found."

            if IntranetUnitsReportsIndexPage.objects.child_of(department_page).count() == 0:
                department_reports_page.title = '2016'
                department_reports_page.slug = '2016'
                department_reports_page.set_url_path(department_page)
                department_reports_page.save()

                # create a department reports index page here- call it "Reports".
                department_reports_index_page = IntranetUnitsReportsIndexPage.objects.create(
                    depth=department_page.depth + 1,
                    numchild=0,
                    path=get_available_path_under(department_page.path),
                    slug='reports',
                    title='Reports',
                    url_path='/loop' + kwargs['path'] + 'reports/'
                )

                # move 2016 into "Reports".
                department_reports_page.move(department_reports_index_page, 'first-child')
    
                # fix paths.
                department_page.fix_tree(destructive=False)

        return "SUCCESS: " + kwargs['path']