840a248ffb9b8cd9c3ff929f186c30b65685ba94
[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:', '<p>\n%s\n%</p>'+prof.bio.profile.replace(
68                     '\n', '\n</p>\n<p>\n')),
69             ('Headshot:', ('/Images/physics/headshots/%s'
70                            % name.lower().replace('.','').replace(' ','_'))),
71             ('Email:', prof.contact.email),
72             ('Phone:', prof.contact.phone),
73             ('Fax:', prof.lab_contact()),
74             ('Page Title - This shows in the tab and title bar of the brower -- Google rates it highly:', name),
75             ("Menu Title - This shows in the menus and navigation blocks -- it's usually a shorted version of the page title:", name),
76             ("Breadcrumb Title - This shows in the breadcrumb trail -- it's usually a very short version of the page title:", name),
77             ("See Also title - Other items that refer to this one will use this text:", name),
78             ]
79         for field_name,value in settings:
80             if value == None:
81                 value = ''
82             granddad,field = s.find_field(field_name)
83             field.clear()
84             field.send_keys(value)
85         #granddad,field = s.find_field('Headshot:')
86         #granddad.find_element_by_link_text('Properties')
87         # TODO, set width/height
88         s.publish_section('Added/updated Prof. %s' % prof.name.last)
89
90     def create_prof_page(self, name):
91         self.s.open_nav_section(name)
92         return
93         self.s.open_nav_section('Copy of Shyamalendu Bose')
94         old_windows = self.s.w.get_window_handles()
95         self.s.w.find_element_by_link_text('Copy To').click()
96         time.sleep(self.s.wait_time)
97         windows = self.s.w.get_window_handles()
98         current_window = self.s.w.get_current_window_handle()
99         popup = [w for w in windows if w not in old_windows][0]
100         if current_window != popup:
101             self.s.logger.info('handling copy popup %s (from %s, old %s)'
102                                % (popup, windows, current_window))
103             self.s.w.switch_to_window(popup)
104         name = self.s.w.find_element_by_id('Filename')
105         name.clear()
106         name.send_keys(
107             '/sitecore/content/Home/physics/contact/facultyDirectory/%s'
108             % name)
109         self.s.w.find_element_by_id('OK').click()
110         time.sleep(self.s.wait_time)
111         current_window = self.s.w.get_current_window_handle()
112         self.s.logger.info('handled copy popup %s, back to %s'
113                            % (popup, current_window))
114         self.s.open_nav_section(name)
115
116     def lock_section(self, name):
117         try:
118             self.s.lock_section()
119         except NoSuchElementException, e:
120             self.s.logger.info("can't lock %s (already locked?), skipping" % name)
121
122
123 def main(argv):
124     import optparse
125     usage = """%prog [options] PROF_FILE
126
127 Where PROF_FILE is a YAML file containing professor data.
128
129 Example setup before running:
130   Xvfb :99 > /dev/null 2>&1 &
131   export DISPLAY=:99
132   java -jar selenium-server-1.0.2-SNAPSHOT-standalone.jar > /dev/null 2>&1 &
133   ./sc.py prof-import profs.yaml
134 """
135     p = optparse.OptionParser(usage)
136     p.add_option('-u', '--url', metavar='URL', dest='url',
137                  help='set the website URL (%default)',
138                  default='https://webedit.drexel.edu/sitecore/login')
139     p.add_option('-v', '--verbose', dest='verbose', action='count',
140                  help='increment verbosity (%default)',
141                  default=0)
142
143     options,args = p.parse_args(argv[1:])
144     prof_file = args[0]
145     profs = yaml.load(open(prof_file, 'r'))
146
147     s = SiteCoreConnection(options.url, options.verbose)
148     s.start()
149     try:
150         s.login()
151         add = ProfessorAdder(s)
152         add.setup()
153         for prof in profs:
154             add(prof)
155     finally:
156         s.stop()