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:.*', re.DOTALL),
42 'College':re.compile('College:.*', re.DOTALL),
43 'Program':re.compile('Program:.*', re.DOTALL),
44 'Office':re.compile('Office:.*', re.DOTALL),
45 'Specialization':re.compile('.*Specialization:.*', re.DOTALL),
46 'Research Interests':re.compile('.*Research Interests:.*',
48 'Personal Site':re.compile('.*Personal Site:.*', re.DOTALL),
49 'Title':re.compile('.*Title:.*', re.DOTALL),
50 'First Name':re.compile('.*First Name:.*', re.DOTALL),
51 'Last Name':re.compile('.*Last Name:.*', re.DOTALL),
52 'Bio':re.compile('.*Bio:.*', re.DOTALL),
53 'Headshot':re.compile('.*Headshot:.*', re.DOTALL),
54 'Email':re.compile('.*Email:.*', re.DOTALL),
55 'Phone':re.compile('.*Phone:.*', re.DOTALL),
56 'Fax':re.compile('.*Fax:.*', re.DOTALL),
57 'Page Title':re.compile('.*Page Title -.*', re.DOTALL),
58 'Menu Title':re.compile('.*Menu Title -.*', re.DOTALL),
59 'Breadcrumb Title':re.compile('.*Breadcrumb Title -.*', re.DOTALL),
60 'See Also Title':re.compile('.*See Also title -.*', re.DOTALL),
64 #self.s.expand_nav_section('sitecore')
65 #self.s.expand_nav_section('Content')
66 self.s.expand_nav_section('Home')
67 for i in range(5): # 'physics' takes a while to load
69 self.s.expand_nav_section('physics')
70 except NoSuchElementException, e:
71 time.sleep(self.s.wait_time)
72 self.s.expand_nav_section('contact')
73 self.s.expand_nav_section('facultyDirectory')
75 def __call__(self, prof):
76 name = '%s %s' % (prof.name.first_middle, prof.name.last)
77 self.create_prof_page(name)
78 self.lock_section(name)
80 ('Degrees', prof.degrees()),
81 ('College', prof.colleges()),
82 ('Program', prof.program()),
83 ('Office', prof.contact.office),
84 ('Specialization', prof.bio.specialization),
85 ('Research Interests', ''),
86 ('Personal Site', prof.sites()),
87 ('Title', prof.title),
88 ('First Name', prof.name.first_middle),
89 ('Last Name', prof.name.last),
90 ('Bio', prof.profile()),
91 ('Headshot', ('/Images/physics/headshots/%s'
92 % name.lower().replace('.','').replace(' ','_'))),
93 ('Email', prof.contact.email),
94 ('Phone', prof.contact.phone),
95 ('Fax', prof.lab_contact()),
98 ('Breadcrumb Title', name),
99 ('See Also title', name),
101 for field_name,value in settings:
104 granddad,field = self.s.find_field(self.regexps[field_name],
107 editor_link = granddad.find_element_by_link_text('Edit Html')
108 self.set_editor_field(editor_link, value)
109 except NoSuchElementException: # basic text field
111 field.send_keys(value)
112 #granddad,field = self.s.find_field('Headshot:')
113 #granddad.find_element_by_link_text('Properties')
114 # TODO, set width/height
115 self.s.publish_section('Added/updated Prof. %s' % prof.name.last)
117 def create_prof_page(self, name):
118 self.s.open_nav_section(name)
120 self.s.open_nav_section('Copy of Shyamalendu Bose')
121 old_windows = self.s.w.get_window_handles()
122 self.s.w.find_element_by_link_text('Copy To').click()
123 time.sleep(self.s.wait_time)
124 windows = self.s.w.get_window_handles()
125 current_window = self.s.w.get_current_window_handle()
126 popup = [w for w in windows if w not in old_windows][0]
127 if current_window != popup:
128 self.s.logger.info('handling copy popup %s (from %s, old %s)'
129 % (popup, windows, current_window))
130 self.s.w.switch_to_window(popup)
131 name = self.s.w.find_element_by_id('Filename')
134 '/sitecore/content/Home/physics/contact/facultyDirectory/%s'
136 self.s.w.find_element_by_id('OK').click()
137 time.sleep(self.s.wait_time)
138 current_window = self.s.w.get_current_window_handle()
139 self.s.logger.info('handled copy popup %s, back to %s'
140 % (popup, current_window))
141 self.s.open_nav_section(name)
143 def lock_section(self, name):
145 self.s.lock_section()
146 except NoSuchElementException, e:
147 self.s.logger.info("can't lock %s (already locked?), skipping" % name)
149 def set_editor_field(self, editor_link, value):
150 pass # TODO, save first.
155 usage = """%prog [options] PROF_FILE
157 Where PROF_FILE is a YAML file containing professor data.
159 Example setup before running:
160 Xvfb :99 > /dev/null 2>&1 &
162 java -jar selenium-server-1.0.2-SNAPSHOT-standalone.jar > /dev/null 2>&1 &
163 ./sc.py prof-import profs.yaml
165 p = optparse.OptionParser(usage)
166 p.add_option('-u', '--url', metavar='URL', dest='url',
167 help='set the website URL (%default)',
168 default='https://webedit.drexel.edu/sitecore/login')
169 p.add_option('-v', '--verbose', dest='verbose', action='count',
170 help='increment verbosity (%default)',
173 options,args = p.parse_args(argv[1:])
175 profs = yaml.load(open(prof_file, 'r'))
177 s = SiteCoreConnection(options.url, options.verbose)
181 add = ProfessorAdder(s)