Use regexps instead of string matching for fields
[sitecorepy.git] / sitecore / prof / import.py
1 #!/usr/bin/env python
2 # Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
3 #
4 # This file is part of SiteCorePy.
5 #
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.
10 #
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.
15 #
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/>.
18
19 """Move Professors from YAML -> Sitecore.
20
21 Professors will be created in:
22   Content -> Home -> physics -> contact -> facultyDirectory
23 """
24
25 import re
26 import time
27
28 from selenium.common.exceptions import NoSuchElementException
29 import yaml
30
31 from .. import SiteCoreConnection
32 from . import Name, Graduation, Contact, Bio, Professor
33
34
35 class ProfessorAdder (object):
36     """See doc/faculty.txt for Drexel's field/format policy.
37     """
38     def __init__(self, sitecore_connection):
39         self.s = sitecore_connection
40         self.regexps = {
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 -.*'),
60             }
61
62     def setup(self):
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
67             try:
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')
73
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)
78         settings = [
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()),
95             ('Page Title', name),
96             ('Menu Title', name),
97             ('Breadcrumb Title', name),
98             ('See Also title', name),
99             ]
100         for field_name,value in settings:
101             if value == None:
102                 value = ''
103             granddad,field = self.s.find_field(self.regexps[field_name],
104                                                field_name)
105             try:
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
109                 field.clear()
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)
115
116     def create_prof_page(self, name):
117         self.s.open_nav_section(name)
118         return
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')
131         name.clear()
132         name.send_keys(
133             '/sitecore/content/Home/physics/contact/facultyDirectory/%s'
134             % name)
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)
141
142     def lock_section(self, name):
143         try:
144             self.s.lock_section()
145         except NoSuchElementException, e:
146             self.s.logger.info("can't lock %s (already locked?), skipping" % name)
147
148     def set_editor_field(self, editor_link, value):
149         pass  # TODO, save first.
150
151
152 def main(argv):
153     import optparse
154     usage = """%prog [options] PROF_FILE
155
156 Where PROF_FILE is a YAML file containing professor data.
157
158 Example setup before running:
159   Xvfb :99 > /dev/null 2>&1 &
160   export DISPLAY=:99
161   java -jar selenium-server-1.0.2-SNAPSHOT-standalone.jar > /dev/null 2>&1 &
162   ./sc.py prof-import profs.yaml
163 """
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)',
170                  default=0)
171
172     options,args = p.parse_args(argv[1:])
173     prof_file = args[0]
174     profs = yaml.load(open(prof_file, 'r'))
175
176     s = SiteCoreConnection(options.url, options.verbose)
177     s.start()
178     try:
179         s.login()
180         add = ProfessorAdder(s)
181         add.setup()
182         for prof in profs:
183             add(prof)
184     finally:
185         s.stop()