2 # Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
4 # This file is part of SiteCorePy.
6 # SiteCorePy is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by the
8 # Free Software Foundation, either version 2 of the License, or (at your
9 # option) any later version.
11 # SiteCorePy is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with SiteCorePy. If not, see <http://www.gnu.org/licenses/>.
19 """Move Professors from YAML -> Sitecore.
21 Professors will be created in:
22 Content -> Home -> physics -> contact -> facultyDirectory
28 from selenium.common.exceptions import NoSuchElementException
31 from .. import SiteCoreConnection
32 from . import Name, Graduation, Contact, Bio, Professor
35 class ProfessorAdder (object):
36 """See doc/faculty.txt for Drexel's field/format policy.
38 def __init__(self, sitecore_connection):
39 self.s = sitecore_connection
41 'Degrees':re.compile('Degrees:.*'),
42 'College':re.compile('College:.*'),
43 'Program':re.compile('Program:.*'),
44 'Office':re.compile('Office:.*'),
45 'Specialization':re.compile('.*Specialization:.*'),
46 'Research Interests':re.compile('.*Research Interests:.*'),
47 'Personal Site':re.compile('.*Personal Site:.*'),
48 'Title':re.compile('.*Title:.*'),
49 'First Name':re.compile('.*First Name:.*'),
50 'Last Name':re.compile('.*Last Name:.*'),
51 'Bio':re.compile('.*Bio:.*'),
52 'Headshot':re.compile('.*Headshot:.*'),
53 'Email':re.compile('.*Email:.*'),
54 'Phone':re.compile('.*Phone:.*'),
55 'Fax':re.compile('.*Fax:.*'),
56 'Page Title':re.compile('.*Page Title -.*'),
57 'Menu Title':re.compile('.*Menu Title -.*'),
58 'Breadcrumb Title':re.compile('.*Breadcrumb Title -.*'),
59 'See Also Title':re.compile('.*See Also title -.*'),
63 #self.s.expand_nav_section('sitecore')
64 #self.s.expand_nav_section('Content')
65 self.s.expand_nav_section('Home')
66 for i in range(5): # 'physics' takes a while to load
68 self.s.expand_nav_section('physics')
69 except NoSuchElementException, e:
70 time.sleep(self.s.wait_time)
71 self.s.expand_nav_section('contact')
72 self.s.expand_nav_section('facultyDirectory')
74 def __call__(self, prof):
75 name = '%s %s' % (prof.name.first_middle, prof.name.last)
76 self.create_prof_page(name)
77 self.lock_section(name)
79 ('Degrees', prof.degrees()),
80 ('College', prof.colleges()),
81 ('Program', prof.program()),
82 ('Office', prof.contact.office),
83 ('Specialization', prof.bio.specialization),
84 ('Research Interests', ''),
85 ('Personal Site', prof.sites()),
86 ('Title', prof.title),
87 ('First Name', prof.name.first_middle),
88 ('Last Name', prof.name.last),
89 ('Bio', prof.profile()),
90 ('Headshot', ('/Images/physics/headshots/%s'
91 % name.lower().replace('.','').replace(' ','_'))),
92 ('Email', prof.contact.email),
93 ('Phone', prof.contact.phone),
94 ('Fax', prof.lab_contact()),
97 ('Breadcrumb Title', name),
98 ('See Also title', name),
100 for field_name,value in settings:
103 granddad,field = self.s.find_field(self.regexps[field_name],
106 editor_link = granddad.find_element_by_link_text('Edit Html')
107 self.set_editor_field(editor_link, value)
108 except NoSuchElementException: # basic text field
110 field.send_keys(value)
111 #granddad,field = self.s.find_field('Headshot:')
112 #granddad.find_element_by_link_text('Properties')
113 # TODO, set width/height
114 self.s.publish_section('Added/updated Prof. %s' % prof.name.last)
116 def create_prof_page(self, name):
117 self.s.open_nav_section(name)
119 self.s.open_nav_section('Copy of Shyamalendu Bose')
120 old_windows = self.s.w.get_window_handles()
121 self.s.w.find_element_by_link_text('Copy To').click()
122 time.sleep(self.s.wait_time)
123 windows = self.s.w.get_window_handles()
124 current_window = self.s.w.get_current_window_handle()
125 popup = [w for w in windows if w not in old_windows][0]
126 if current_window != popup:
127 self.s.logger.info('handling copy popup %s (from %s, old %s)'
128 % (popup, windows, current_window))
129 self.s.w.switch_to_window(popup)
130 name = self.s.w.find_element_by_id('Filename')
133 '/sitecore/content/Home/physics/contact/facultyDirectory/%s'
135 self.s.w.find_element_by_id('OK').click()
136 time.sleep(self.s.wait_time)
137 current_window = self.s.w.get_current_window_handle()
138 self.s.logger.info('handled copy popup %s, back to %s'
139 % (popup, current_window))
140 self.s.open_nav_section(name)
142 def lock_section(self, name):
144 self.s.lock_section()
145 except NoSuchElementException, e:
146 self.s.logger.info("can't lock %s (already locked?), skipping" % name)
148 def set_editor_field(self, editor_link, value):
149 pass # TODO, save first.
154 usage = """%prog [options] PROF_FILE
156 Where PROF_FILE is a YAML file containing professor data.
158 Example setup before running:
159 Xvfb :99 > /dev/null 2>&1 &
161 java -jar selenium-server-1.0.2-SNAPSHOT-standalone.jar > /dev/null 2>&1 &
162 ./sc.py prof-import profs.yaml
164 p = optparse.OptionParser(usage)
165 p.add_option('-u', '--url', metavar='URL', dest='url',
166 help='set the website URL (%default)',
167 default='https://webedit.drexel.edu/sitecore/login')
168 p.add_option('-v', '--verbose', dest='verbose', action='count',
169 help='increment verbosity (%default)',
172 options,args = p.parse_args(argv[1:])
174 profs = yaml.load(open(prof_file, 'r'))
176 s = SiteCoreConnection(options.url, options.verbose)
180 add = ProfessorAdder(s)