self.logger -> self.s.logger in prof/import.py
[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         self.create_prof_page(prof)
54         self.s.open_nav_section(raw_name)
55         TODO
56
57     def create_prof_page(self, prof):
58         self.s.open_nav_section('Copy of Shyamalendu Bose')
59         self.s.w.find_element_by_link_text('Copy To').click()
60         time.sleep(self.s.wait_time)
61         windows = self.s.w.get_window_handles()
62         current_window = self.s.w.get_current_window_handle()
63         self.s.logger.info('handling copy popup %s (from %s)'
64                          % (current_window, windows))
65         name = self.s.find_element_by_id('Filename')
66         name.clear()
67         name.send_keys(
68             '/sitecore/content/Home/physics/contact/facultyDirectory/%s %s'
69             % (prof.name.first, prof.name.last))
70         self.s.w.find_element_by_link_text('Copy').click()
71         time.sleep(self.s.wait_time)
72
73
74 def main(argv):
75     import optparse
76     usage = """%prog [options] PROF_FILE
77
78 Where PROF_FILE is a YAML file containing professor data.
79
80 Example setup before running:
81   Xvfb :99 > /dev/null 2>&1 &
82   export DISPLAY=:99
83   java -jar selenium-server-1.0.2-SNAPSHOT-standalone.jar > /dev/null 2>&1 &
84   ./sc.py prof-import profs.yaml
85 """
86     p = optparse.OptionParser(usage)
87     p.add_option('-u', '--url', metavar='URL', dest='url',
88                  help='set the website URL (%default)',
89                  default='https://webedit.drexel.edu/sitecore/login')
90     p.add_option('-v', '--verbose', dest='verbose', action='count',
91                  help='increment verbosity (%default)',
92                  default=0)
93
94     options,args = p.parse_args(argv[1:])
95     prof_file = args[0]
96     profs = yaml.load(prof_file)
97
98     s = SiteCoreConnection(options.url, options.verbose)
99     s.start()
100     try:
101         s.login()
102         add = ProfessorAdder(s)
103         add.setup()
104         for prof in profs:
105             add(prof)
106     finally:
107         s.stop()