2ed57c0d9a0fa8a4e0aeaa66212c6a2ea6dd3e78
[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 -> Drexel -> ? -> physics -> ?
23 """
24
25 import time
26
27 from selenium.common.exceptions import NoSuchElementException
28 import yaml
29
30 from .. import SiteCoreConnection
31 from . import Name, Graduation, Contact, Bio, Professor
32
33
34 class ProfessorAdder (object):
35     """See doc/faculty.txt for Drexel's field/format policy.
36     """
37     def __init__(self, sitecore_connection):
38         self.s = sitecore_connection
39
40     def setup(self):
41         #self.s.expand_nav_section('sitecore')
42         #self.s.expand_nav_section('Content')
43         self.s.expand_nav_section('Home')
44         for i in range(5): # 'physics' takes a while to load
45             try:
46                 self.s.expand_nav_section('physics')
47             except NoSuchElementException, e:
48                 time.sleep(self.s.wait_time)
49         self.s.expand_nav_section('contact')
50         self.s.expand_nav_section('facultyDirectory')
51
52     def __call__(self, prof):
53         name = '%s %s' % (prof.name.first_middle, prof.name.last)
54         self.create_prof_page(name)
55         self.lock_section(name)
56         settings = [
57             ('Degrees:', prof.degrees()),
58             ('College:', prof.colleges()),
59             ('Program:', prof.program()),
60             ('Office:', prof.contact.office),
61             ('Specialization:', prof.bio.specialization),
62             ('Research Interests:', ''),
63             ('Personal Site:', prof.sites()),
64             ('Title:', prof.title),
65             ('First Name:', prof.name.first_middle),
66             ('Last Name:', prof.name.last),
67             ('Bio:', prof.profile()),
68             ('Headshot:', ('/Images/physics/headshots/%s'
69                            % name.lower().replace('.','').replace(' ','_'))),
70             ('Email:', prof.contact.email),
71             ('Phone:', prof.contact.phone),
72             ('Fax:', prof.lab_contact()),
73             ('Page Title - This shows in the tab and title bar of the brower -- Google rates it highly:', name),
74             ("Menu Title - This shows in the menus and navigation blocks -- it's usually a shorted version of the page title:", name),
75             ("Breadcrumb Title - This shows in the breadcrumb trail -- it's usually a very short version of the page title:", name),
76             ("See Also title - Other items that refer to this one will use this text:", name),
77             ]
78         for field_name,value in settings:
79             if value == None:
80                 value = ''
81             granddad,field = s.find_field(field_name)
82             field.clear()
83             field.send_keys(value)
84         #granddad,field = s.find_field('Headshot:')
85         #granddad.find_element_by_link_text('Properties')
86         # TODO, set width/height
87         s.publish_section('Added/updated Prof. %s' % prof.name.last)
88
89     def create_prof_page(self, name):
90         self.s.open_nav_section(name)
91         return
92         self.s.open_nav_section('Copy of Shyamalendu Bose')
93         old_windows = self.s.w.get_window_handles()
94         self.s.w.find_element_by_link_text('Copy To').click()
95         time.sleep(self.s.wait_time)
96         windows = self.s.w.get_window_handles()
97         current_window = self.s.w.get_current_window_handle()
98         popup = [w for w in windows if w not in old_windows][0]
99         if current_window != popup:
100             self.s.logger.info('handling copy popup %s (from %s, old %s)'
101                                % (popup, windows, current_window))
102             self.s.w.switch_to_window(popup)
103         name = self.s.w.find_element_by_id('Filename')
104         name.clear()
105         name.send_keys(
106             '/sitecore/content/Home/physics/contact/facultyDirectory/%s'
107             % name)
108         self.s.w.find_element_by_id('OK').click()
109         time.sleep(self.s.wait_time)
110         current_window = self.s.w.get_current_window_handle()
111         self.s.logger.info('handled copy popup %s, back to %s'
112                            % (popup, current_window))
113         self.s.open_nav_section(name)
114
115     def lock_section(self, name):
116         try:
117             self.s.lock_section()
118         except NoSuchElementException, e:
119             self.s.logger.info("can't lock %s (already locked?), skipping" % name)
120
121
122 def main(argv):
123     import optparse
124     usage = """%prog [options] PROF_FILE
125
126 Where PROF_FILE is a YAML file containing professor data.
127
128 Example setup before running:
129   Xvfb :99 > /dev/null 2>&1 &
130   export DISPLAY=:99
131   java -jar selenium-server-1.0.2-SNAPSHOT-standalone.jar > /dev/null 2>&1 &
132   ./sc.py prof-import profs.yaml
133 """
134     p = optparse.OptionParser(usage)
135     p.add_option('-u', '--url', metavar='URL', dest='url',
136                  help='set the website URL (%default)',
137                  default='https://webedit.drexel.edu/sitecore/login')
138     p.add_option('-v', '--verbose', dest='verbose', action='count',
139                  help='increment verbosity (%default)',
140                  default=0)
141
142     options,args = p.parse_args(argv[1:])
143     prof_file = args[0]
144     profs = yaml.load(open(prof_file, 'r'))
145
146     s = SiteCoreConnection(options.url, options.verbose)
147     s.start()
148     try:
149         s.login()
150         add = ProfessorAdder(s)
151         add.setup()
152         for prof in profs:
153             add(prof)
154     finally:
155         s.stop()