def get_location_from_base_url(): try: return Location.select(Location.q.url==cherrypy.request.base)[0] except IndexError: try: # try w/o www. Ref: website #89 url_wo_www = cherrypy.request.base.replace("://www.", "") return Location.select(Location.q.url==url_wo_www)[0] except IndexError: pass return Location.get(1)
def get_location_from_base_url(): try: req_url = cherrypy.request.base.lower() if "https:" in req_url: # website#252 req_url = req_url.replace("https:", "http:") return Location.select(Location.q.url == req_url)[0] except IndexError: try: # try w/o www. Ref: website #89 url_wo_www = cherrypy.request.base.replace("://www.", "") return Location.select(Location.q.url == url_wo_www)[0] except IndexError: pass return Location.get(1)
def add_cities(): """Add cities to Locations """ from hubspace.model import Location for loc in Location.select(): if loc.name not in ['Southbank', 'Kings Cross', 'Islington']: loc.city = loc.name else: loc.city = "London"
def filter_members(location, text_filter, type, active_only, start, end, override_user=None): if type == "member_search": text_filter = "%" + " ".join(text_filter.split()).replace("'","\\'") + "%" if override_user: user_locs = Location.select() else: user_locs = user_locations(identity.current.user, ['member', 'host']) if user_locs: if location: relevant_groups = location.groups relevant_user_ids = tuple((ug.userID for ug in UserGroup.select(IN(UserGroup.q.group, tuple(relevant_groups))))) display_name_clause = iLIKE(User.q.display_name, text_filter) user_id_clause = IN(User.q.id, relevant_user_ids) # so that we select all members even those with homehub as this hub if active_only: user_active_clause = (User.q.active == 1) users = User.select(AND(display_name_clause, user_id_clause, user_active_clause)) else: users = User.select(AND(display_name_clause, user_id_clause)) else: display_name_clause = iLIKE(User.q.display_name, text_filter) if active_only: user_active_clause = (User.q.active == 1) users = User.select(AND(display_name_clause, user_active_clause)) else: users = User.select(display_name_clause) users = users.orderBy('display_name') else: users = [] elif type == 'rfid_member_search': users = User.select(AND(User.q.rfid == text_filter)) elif type == 'fulltext_member_search': users = hubspace.search.do_search(text_filter) if location: user_ids = tuple(user.id for user in users) if not user_ids: users = [] else: users = User.select(AND(IN(User.q.id, user_ids), User.q.homeplaceID==location.id)) if start != None and end != None: users = users[start:end] try: webapi = User.selectBy(first_name="web", last_name="api")[0] if webapi in users and not identity.has_permission("superuser"): users.remove(webapi) except: pass return users
def setup_microsite_spaces(): """Create the microsites spaces object """ from hubspace.model import ObjectReference, Page, ListItem, PublicSpace, Location, MetaData, MicroSiteSpace, PublicPlace, User from hubspace.microSite import microsite_pages, microsite_page_types, microsite_left_page_list, microsite_right_page_list, append_existing_item Page.createTable(ifNotExists=True) ObjectReference.createTable(ifNotExists=True) ListItem.createTable(ifNotExists=True) PublicSpace.createTable(ifNotExists=True) PublicPlace.createTable(ifNotExists=True) MetaData.createTable(ifNotExists=True) for user in User.select(): ObjectReference(**{'object': (user.__class__.__name__, user.id)}) for loc in Location.select(): if loc.id == 16 or not loc.url: continue try: old_space = MicroSiteSpace.select(AND(MicroSiteSpace.q.locationID==loc.id, MicroSiteSpace.q.nextID==None))[0] except IndexError: print `loc.name` + " doesn't have any spaces to migrate" old_space = None next_item = None while old_space: new_space = PublicSpace(name=old_space.name, description=old_space.description, image=old_space.image) object_ref = ObjectReference.selectBy(object_type='PublicSpace', object_id=new_space.id)[0] new_list_item = ListItem(next=next_item, location=loc, active=old_space.active, list_name="spaces_list", object_ref=object_ref) next_item = new_list_item old_space = old_space.previous ObjectReference(**{'object': (loc.__class__.__name__, loc.id)}) for page, type in microsite_pages.items(): microsite_page_types[type].create_page(page, loc, {}) kwargs = {'location':loc, 'object_type': Page, 'active': 1} for page in microsite_left_page_list: kwargs.update({'name':page}) try: page = Page.select(AND(Page.q.location == loc, IN(Page.q.path_name, [page, page + '.html'])))[0] append_existing_item('left_tabs', page, **kwargs) except IndexError: pass for page in microsite_right_page_list: kwargs.update({'name':page}) try: page = Page.select(AND(Page.q.location == loc, IN(Page.q.path_name, [page, page + '.html'])))[0] append_existing_item('right_tabs', page, **kwargs) except IndexError: pass
def update_locations(): from hubspace.utilities.object import create_object from hubspace.openTimes import create_default_open_times, add_accessPolicy2Proxy from hubspace.model import Location, Group for location in Location.select(): cal = create_object('Resource', type='calendar', time_based=1, active=0, place=location.id, name='calendar', description='calendar') location.calendar = cal.id for level in ['member', 'host', 'director']: group = Group.select(AND(Group.q.level == level, Group.q.placeID == location.id))[0] access_policy = add_accessPolicy2Proxy(cal, group.id, 'Group', 5, None, None) create_default_open_times(access_policy)
def all_hosts(): """all hosts in the system """ users = [] for location in Location.select(): groups = Group.select(AND(Group.q.placeID==location.id, Group.q.level=="host")) for group in groups: for user in group.users: if user not in users: users.append(user) users.sort(display_name) return users
def location_links(): locations = Location.select(AND(Location.q.in_region == None), orderBy='name') loc_tuples = [] for location in locations: link = write_link_tuple(location) if link: loc_tuples.append(link) if location.is_region: hubs = location.has_hubs hubs.sort(name_sort) for hub in hubs: link = write_link_tuple(hub, sub=True) if link: loc_tuples.append(link) return loc_tuples
def now(location=1): if not isinstance(location, Location): try: location = Location.get(location) except: return datetime.now() zone_name = location.timezone if not zone_name: zone_name = 'UTC' time_zone = timezone(zone_name) local_now = datetime.now(tz=time_zone) #convert back to a naive datetime object so that its comparable with those stored on rusages etc #ideally everything would be dealing with localised datetime objects but this would take significant work on the existing data and scripts. naive_now = datetime(local_now.year, local_now.month, local_now.day, local_now.hour, local_now.minute, local_now.second) return naive_now
def get_tariff(loc, userid, usage_start_time, default=True): result = Resource.select(AND(RUsage.q.resourceID == Resource.q.id, Resource.q.type=='tariff', RUsage.q.cancelled==0, RUsage.q.userID==userid, Resource.q.placeID==loc, RUsage.q.start <= usage_start_time, RUsage.q.end_time >= usage_start_time)) try: return result[0] except: if default: return Location.get(loc).defaulttariff return None
def vat_switch(): """add total taxes, resource_tax_dict, vat_included """ from hubspace.model import Invoice, Location from hubspace.invoice import calculate_tax_and_amount #recalculate all the old invoice costs and amounts, as used to happen everytime we did looked at the invoice! london = Location.get(1) bristol = Location.get(2) kx = Location.get(11) switch_time = datetime(2008, 12, 1, 0, 0) special = Invoice.select(AND(IN(Invoice.q.locationID, [1, 2, 11]), Invoice.q.created < switch_time)) not_special = Invoice.select(OR(NOT(IN(Invoice.q.locationID, [1, 2, 11])), Invoice.q.created >= switch_time)) for inv in not_special: tmp = inv.sent inv.sent = None calculate_tax_and_amount(inv) inv.sent = tmp london.vat_default = 17.5 bristol.vat_default = 17.5 kx.vat_default = 17.5 #might need to re-patch this bit later for bristol as I think they sent out some invoices at 17.5% after the 1st December for inv in special: tmp = inv.sent inv.sent = None calculate_tax_and_amount(inv) inv.sent = tmp london.vat_default = 15 bristol.vat_default = 15 kx.vat_default = 15
def location_links(): locations = Location.select(AND(Location.q.in_region == None, Location.q.hidden == False), orderBy='name') loc_tuples = [] for location in locations: if location.id in (2, 18, 32, 7, 90): continue # Website 226, 263, 271 link = write_link_tuple(location) if link: loc_tuples.append(link) if location.is_region: hubs = location.has_hubs hubs.sort(name_sort) for hub in hubs: link = write_link_tuple(hub, sub=True) if link: loc_tuples.append(link) return loc_tuples
def __init__(self, loc_id, period=None, start=None, end=None): self.location = Location.get(loc_id) if start and end: self.start, self.end = start, end elif period: if period == 'thismonth': self.start, self.end = get_this_months_limits() elif period == 'thisandlastmonths': self.start, self.end = get_this_and_last_months_limits() elif period == 'lastmonth': self.start, self.end = get_last_months_limits() elif period == 'last12months': self.start, self.end = get_last_12months_limits() else: self.start, self.end = get_this_months_limits() self.results = dict()
def fix_users_without_object_references(): from hubspace.model import User, ObjectReference, create_object_reference, Location for user in User.select(): try: object_ref = ObjectReference.select( AND(ObjectReference.q.object_type == user.__class__.__name__, ObjectReference.q.object_id == user.id) )[0] except IndexError: print "giving " + user.username + "an object reference" create_object_reference({"class": User, "id": user.id}, None) for loc in Location.select(): try: object_ref = ObjectReference.select( AND(ObjectReference.q.object_type == loc.__class__.__name__, ObjectReference.q.object_id == loc.id) )[0] except IndexError: create_object_reference({"class": Location, "id": loc.id}, None) print "giving " + loc.name + "an object reference"
def migrate_lists(): from hubspace.model import Page, List, ListItem, Location from hubspace.microSite import list_types for location in Location.select(): results = Page.selectBy(location=location, path_name='index.html') if results.count() == 0: continue; else: indexpage = results[0] for list_name, data in list_types.items(): object_types = ','.join(data['object_types']) mode = data['mode'] thelist = List(list_name=list_name, object_types=object_types, mode=mode, page=indexpage, location=location,) for listitem in ListItem.selectBy(location=location,list_name=list_name): listitem.list = thelist
def bristol_vat_switch(): """get the bristol invoices that were sent at 15% before mid day on the 2nd of december and set them to 17.5% """ from hubspace.model import Invoice, Location from hubspace.invoice import calculate_tax_and_amount #recalculate all the old invoice costs and amounts, as used to happen everytime we did looked at the invoice! bristol = Location.get(2) start_switch_time = datetime(2008, 12, 1, 0, 0) end_switch_time = datetime(2008, 12, 2, 12, 0) special = Invoice.select(AND(Invoice.q.locationID == 2, Invoice.q.created < end_switch_time, Invoice.q.created > start_switch_time)) bristol.vat_default = 17.5 for inv in special: tmp = inv.sent inv.sent = None calculate_tax_and_amount(inv) inv.sent = tmp bristol.vat_default = 15.0