Exemplo n.º 1
0
    def get_pass(self):

        pass_template = PassTemplate.all().filter('short_code',self.pass_code).get()
        if not pass_template:
            # return 404
            raise ValueError('pass_template')
        self.context['linked_pass_template'] = pass_template.short_code
        self.context['linked_pass_entity'] = pass_template
Exemplo n.º 2
0
    def pass_file(self, url):
        if self.user:
            user_keyname = self.user.key().name()
        else:
            user_keyname = '_no_user'
        safe_url = url
        key_name = "%s_%s" % (user_keyname, safe_url)

        pass_template = PassTemplate.get_by_key_name(key_name)
        if pass_template:
            new = False
        else:
            new = True
            pass_template = PassTemplate(key_name=key_name, url=url)
            if self.user:
                pass_template.owner = self.user
            from utils import string as str_utils
            code = str_utils.genkey(length=4)
            pass_template.short_code = code
            pass_template.put()

        # TODO: download and parse file to update it
        # TODO: check last modified, only update if not modified in over x hours
        if new or pass_template.modified < (datetime.datetime.now() -
                                            datetime.timedelta(hours=1)):
            logging.info('deferring update from passfile')
            deferred.defer(update_from_passfile, key_name)
        else:
            logging.info('this pass was modified too recently')

        return pass_template
Exemplo n.º 3
0
    def get_pass(self):

        pass_template = PassTemplate.all().filter('short_code',
                                                  self.pass_code).get()
        if not pass_template:
            # return 404
            raise ValueError('pass_template')
        self.context['linked_pass_template'] = pass_template.short_code
        self.context['linked_pass_entity'] = pass_template
Exemplo n.º 4
0
    def increment(self):
        from model.passes import UserPass, PassTemplate
        pass_template = PassTemplate.get_by_key_name(self.get_argument('pass'))
        action = self.get_argument('action','download').lower()
        if action == 'offer':
            pass_template.shares += 1
        if action == 'request':
            pass_template.saves +=1

        pass_template.put()

        return
Exemplo n.º 5
0
    def increment(self):
        from model.passes import UserPass, PassTemplate
        pass_template = PassTemplate.get_by_key_name(self.get_argument('pass'))
        action = self.get_argument('action', 'download').lower()
        if action == 'offer':
            pass_template.shares += 1
        if action == 'request':
            pass_template.saves += 1

        pass_template.put()

        return
Exemplo n.º 6
0
	def pass_file(self, url):
		if self.user:
			user_keyname = self.user.key().name()
		else:
			user_keyname = '_no_user'
		safe_url = url
		key_name = "%s_%s" % (user_keyname, safe_url)
		
		pass_template = PassTemplate.get_by_key_name(key_name)
		if pass_template:
			new = False
		else:
			new = True
			pass_template = PassTemplate(key_name=key_name, url=url)
			if self.user:
				pass_template.owner = self.user
			from utils import string as str_utils
			code = str_utils.genkey(length=4)
			pass_template.short_code = code
			pass_template.put()			

		# TODO: download and parse file to update it
		# TODO: check last modified, only update if not modified in over x hours
		if new or pass_template.modified < (datetime.datetime.now() - datetime.timedelta(hours=1)):
			logging.info('deferring update from passfile')
			deferred.defer(update_from_passfile, key_name)
		else:
			logging.info('this pass was modified too recently')

		return pass_template	
Exemplo n.º 7
0
    def get(self, pass_code):

        from model.passes import PassTemplate, UserPass
        pass_template = PassTemplate.all().filter('short_code',pass_code).get()

        # if this is an external pass, redirect to it
        if pass_template.url and 'pkpass' in pass_template.url: 
            return self.redirect(pass_template.url)

        from backend.passes import passfile
        # TODO: in the future this should be cached in advance if possible 
        pass_creator = passfile.PassFile(pass_template=pass_template)
        pass_creator.create()
        pass_creator.send_info()
        return pass_creator.write(self)    
Exemplo n.º 8
0
    def create(self):
        from model.passes import UserPass, PassTemplate
        
        self.pass_code = self.get_argument('pass_template')
        pass_template = PassTemplate.all().filter('short_code',self.pass_code).get()
        if not pass_template:
            raise ValueError('pass_template')
        self.pass_template = pass_template

        code = str_utils.genkey(length=5)
        self.user_pass = UserPass(key_name=code, code=code,owner=self.user, 
                template=self.pass_template, pass_name=self.pass_template.name, pass_code=self.pass_template.short_code, action=self.action.lower())
        if self.get_argument('owner_name',''):
            self.user_pass.owner_name = self.get_argument('owner_name')
        if self.get_argument('theme',''):
            self.user_pass.theme = self.get_argument('theme')
Exemplo n.º 9
0
    def get(self, pass_code):

        from model.passes import PassTemplate, UserPass
        pass_template = PassTemplate.all().filter('short_code',
                                                  pass_code).get()

        # if this is an external pass, redirect to it
        if pass_template.url and 'pkpass' in pass_template.url:
            return self.redirect(pass_template.url)

        from backend.passes import passfile
        # TODO: in the future this should be cached in advance if possible
        pass_creator = passfile.PassFile(pass_template=pass_template)
        pass_creator.create()
        pass_creator.send_info()
        return pass_creator.write(self)
Exemplo n.º 10
0
    def find(self,
             query=None,
             order='-modified',
             create='true',
             fetch=1000,
             passes=None,
             account=None,
             **kwargs):
        logging.info('finding passes')

        self.user = kwargs.get('user')

        self.query, self.order, self.create, self.fetch, self.passes, self.account = query, order, create, fetch, passes, account

        if self.create == 'true':
            self.create = True
        else:  # you must manually set create to something besides true
            self.create = False

        if self.passes:
            return self.find_for_list()

        if query:
            logging.info('finding pass for query %s' % query)
            return self.find_pass({'name': query})

        qry = PassTemplate.all()

        if account:
            # account should be key name
            from model.user import User
            self.user = User.get_by_key_name(account)

        if self.user and not self.user.is_admin():
            logging.info('filtering for ownership by %s' %
                         self.user.key().name())
            qry = qry.filter('owner', self.user)

        if order:
            qry = qry.order(order)
        pass_templates = qry.fetch(fetch)
        logging.info('found pass templates: %s' %
                     [pt.key().name() for pt in pass_templates])
        return pass_templates
Exemplo n.º 11
0
def get_passes():
    # TODO: this should be cached!
    from model.passes import PassTemplate, PassList
    pass_dict = {'listings': [], 'tickets': [], 'coupons': []}
    all_lists = PassList.all().fetch(1000)
    for pass_list in all_lists:
        logging.info('getting %s passes for list %s:' %
                     (len(pass_list.passes), pass_list.name))
        if pass_list.key().name() in pass_dict.keys():
            # this is only for curated chosen lists

            list_passes = [
                p for p in PassTemplate.get_by_key_name(pass_list.passes) if p
            ]
            pass_dict[pass_list.key().name()] = list_passes

            for p in pass_dict[pass_list.key().name()]:
                logging.info(p.name)

    return pass_dict
Exemplo n.º 12
0
    def download(self):
        # TODO: create new UserPass to record that this user has this pass? 

        to_email = self.get_argument('to_email','')
        to_phone = self.get_argument('to_phone','')
        theme = self.get_argument('theme','')
        
        current_user = self.get_current_user()
        
        # TODO: refactor this to backend API

        from model.passes import UserPass, PassTemplate
        self.pass_template = PassTemplate.all().filter('short_code',self.get_argument('pass_template')).get()
        self.context['pass_template'] = self.pass_template

        
        
        """
        # Deprecated for now 
        code = str_utils.genkey(length=5)
        self.user_pass = UserPass(key_name=code, code=code,owner=current_user, 
                template=self.pass_template, pass_name=self.pass_template.name, pass_code=self.pass_template.short_code, action='download')
        
        if to_email:
            self.user_pass.to_email = to_email
        if to_phone:
            self.user_pass.to_phone = self.sanitize_phone(to_phone)
        """


        from google.appengine.ext.deferred import deferred
        from model.passes import increment
        deferred.defer(increment, 'sends', self.pass_template.short_code)

        #db.put([self.user_pass, self.pass_template])
        

        if to_email:
            self.download_email(to_email)
        if to_phone:
            self.download_phone()
Exemplo n.º 13
0
def get_passes():
    # TODO: this should be cached!
    from model.passes import PassTemplate, PassList
    pass_dict = {
        'listings': [],
        'tickets': [],
        'coupons': []
    }
    all_lists = PassList.all().fetch(1000)
    for pass_list in all_lists:
        logging.info('getting %s passes for list %s:' % (len(pass_list.passes), pass_list.name) )
        if pass_list.key().name() in pass_dict.keys():
            # this is only for curated chosen lists

            list_passes = [ p for p in PassTemplate.get_by_key_name(pass_list.passes) if p]
            pass_dict[pass_list.key().name()] = list_passes 

            for p in pass_dict[pass_list.key().name()]:
                logging.info(p.name)

    return pass_dict                              
Exemplo n.º 14
0
def update_from_passfile(key_name):
	pass_template = PassTemplate.get_by_key_name(key_name)
	remote_pass = RemotePass()
	logging.info('getting pass from URL')
	remote_pass.from_url(pass_template.url)

	logging.debug('remote pass info: %s' % remote_pass.pass_info)
	pass_template.pass_info = remote_pass.pass_info
	for f in ['background', 'strip', 'logo']:
		imgObj = getattr(remote_pass, f)
		if imgObj:
			pass_template.save_image(imgObj)
			break
	# get name from description? 

	# TODO: other fields - [x][headerFields][secondaryFields][auxiliaryFields] - key/value
	if 'description' in remote_pass.pass_info:
		pass_template.name = remote_pass.pass_info['description']
	if 'organizationName' in remote_pass.pass_info:
		pass_template.organizationName = remote_pass.pass_info['organizationName']

	pass_template.put()
Exemplo n.º 15
0
    def create(self):
        from model.passes import UserPass, PassTemplate

        self.pass_code = self.get_argument('pass_template')
        pass_template = PassTemplate.all().filter('short_code',
                                                  self.pass_code).get()
        if not pass_template:
            raise ValueError('pass_template')
        self.pass_template = pass_template

        code = str_utils.genkey(length=5)
        self.user_pass = UserPass(key_name=code,
                                  code=code,
                                  owner=self.user,
                                  template=self.pass_template,
                                  pass_name=self.pass_template.name,
                                  pass_code=self.pass_template.short_code,
                                  action=self.action.lower())
        if self.get_argument('owner_name', ''):
            self.user_pass.owner_name = self.get_argument('owner_name')
        if self.get_argument('theme', ''):
            self.user_pass.theme = self.get_argument('theme')
Exemplo n.º 16
0
def update_from_passfile(key_name):
    pass_template = PassTemplate.get_by_key_name(key_name)
    remote_pass = RemotePass()
    logging.info('getting pass from URL')
    remote_pass.from_url(pass_template.url)

    logging.debug('remote pass info: %s' % remote_pass.pass_info)
    pass_template.pass_info = remote_pass.pass_info
    for f in ['background', 'strip', 'logo']:
        imgObj = getattr(remote_pass, f)
        if imgObj:
            pass_template.save_image(imgObj)
            break
    # get name from description?

    # TODO: other fields - [x][headerFields][secondaryFields][auxiliaryFields] - key/value
    if 'description' in remote_pass.pass_info:
        pass_template.name = remote_pass.pass_info['description']
    if 'organizationName' in remote_pass.pass_info:
        pass_template.organizationName = remote_pass.pass_info[
            'organizationName']

    pass_template.put()
Exemplo n.º 17
0
    def download(self):
        # TODO: create new UserPass to record that this user has this pass?

        to_email = self.get_argument('to_email', '')
        to_phone = self.get_argument('to_phone', '')
        theme = self.get_argument('theme', '')

        current_user = self.get_current_user()

        # TODO: refactor this to backend API

        from model.passes import UserPass, PassTemplate
        self.pass_template = PassTemplate.all().filter(
            'short_code', self.get_argument('pass_template')).get()
        self.context['pass_template'] = self.pass_template
        """
        # Deprecated for now 
        code = str_utils.genkey(length=5)
        self.user_pass = UserPass(key_name=code, code=code,owner=current_user, 
                template=self.pass_template, pass_name=self.pass_template.name, pass_code=self.pass_template.short_code, action='download')
        
        if to_email:
            self.user_pass.to_email = to_email
        if to_phone:
            self.user_pass.to_phone = self.sanitize_phone(to_phone)
        """

        from google.appengine.ext.deferred import deferred
        from model.passes import increment
        deferred.defer(increment, 'sends', self.pass_template.short_code)

        #db.put([self.user_pass, self.pass_template])

        if to_email:
            self.download_email(to_email)
        if to_phone:
            self.download_phone()
Exemplo n.º 18
0
    def find_pass(self, p, save=True):
        self.save = save

        from backend.location import find as loc_find

        logging.info('getting pass: %s' % p)

        if p.get('id'):
            logging.info('getting pass with ID %s' % p['id'])
            pass_template = PassTemplate.all().filter('short_code',
                                                      p['id']).get()
            if pass_template:
                logging.info('found pass template')
                return pass_template
            else:
                raise ValueError(p['id'])

        if 'url' in p:
            # will search in update class
            return self.create_or_update(p)

        if 'loc' not in p:
            raise ValueError('loc')

        # first do search for pass name and location.
        # If no result, get location and create new pass
        pass_template = None
        pass_results = self.search(p)
        if pass_results:
            pass_template_doc = pass_results[0]
            logging.info(pass_template_doc)
            for f in pass_template_doc.fields:
                logging.info(f.name)
                logging.info(f.value)

            pass_template = PassTemplate.get_by_key_name(
                pass_template_doc.fields[2].value)
            if not pass_template:
                logging.error(
                    'DID NOT GET PASS TEMPLATE FROM SEARCH RESULTS KEYNAME')

        if pass_template:
            logging.info(
                'Found existing template for name %s and location %s' %
                (p.get('name'), p.get('location_code')))

            return pass_template

            # if description is included, should it update existing template?
        else:
            logging.info('no pass template. getting location')

            # first get location, then create new pass

            location_finder = loc_find.FindLocation()
            pass_loc = location_finder.find(query=p.get('loc'), create='true')

            pass_loc.put()  # TODO: optimize saves

            logging.info('got loc %s for pass %s' % (pass_loc.code, p))

            p['location_code'] = pass_loc.code
            p['location'] = pass_loc

            logging.info(
                'Creating New Template. No template found for name %s and location %s'
                % (p.get('name'), p.get('location_code')))

            return self.create_or_update(p)
Exemplo n.º 19
0
	def create_or_update(self, name=None, slug=None, description=None, price=None, schedule=None, 
			neighborhood_name=None, location=None, location_code=None, price_rating=None, api=False, 
			image_url=None, url=None, user=None, organizationName=None, organizationUrl=None, **kwargs):
		
		self.user = user

		if url:
			# this does not allow file-based passes to have updated listings on Passtiche
			return self.pass_file(url)
		if not slug:
		    slug = PassTemplate.get_slug(name) or 'event'
		keyname = slug
		if location_code:    
			keyname += "-%s" % location_code
		if self.user:
			keyname += "~%s" % self.user.short_code

		logging.info(kwargs)
		if kwargs.get('new'):
			pass_template = None
		else:
			logging.info('getting pass template %s' % keyname)
			pass_template = PassTemplate.get_by_key_name(keyname)
			if pass_template:
				logging.info('found pass template %s' % keyname)
		
		if not pass_template:
			logging.info('creating new pass template')
			pass_template = PassTemplate(key_name=keyname, name=name, slug=slug)
			from utils import string as str_utils
			code = str_utils.genkey(length=4)
			pass_template.short_code = code

			if self.user and not self.user.is_admin():
				pass_template.owner = self.user
			if not location and location_code:
				from model.activity import Location
				location = Location.get_by_key_name(location_code)
			if location:
				location_name = location.name
			else:
				location_name = ''

			searchFields = [search.TextField(name='name', value=name),
                search.TextField(name='code', value=code),
                search.TextField(name='keyname', value=keyname),
                search.TextField(name='loc', value=(location_code or '')),
                search.TextField(name='location_name', value=location_name),
                search.DateField(name='date', value=datetime.datetime.now().date())]

			if self.user:
				searchFields.append(search.TextField(name='owner', value=self.user.short_code))
			pass_doc = search.Document(fields=searchFields)
			logging.info('adding pass doc to index')
			search.Index(name=_INDEX_NAME).put(pass_doc)

		elif name:
				pass_template.name = name

		if description:
		    pass_template.description = description
		if price:
			pass_template.price = int(price)
		if price_rating is not None:
			pass_template.price_rating = price_rating

		if organizationName:
			pass_template.organizationName = organizationName

		if organizationUrl:
			if not organizationUrl.startswith('http://'):
				organizationUrl = 'http://%s' % organizationUrl
			pass_template.organizationUrl = organizationUrl

		if image_url:
			pass_template.image_url = image_url	

		if schedule:

			pass_template.schedule = schedule

			pass_template = set_time_format(pass_template)
			
			# TODO: starts time
		if neighborhood_name:
			pass_template.neighborhood_name = neighborhood_name


		if location_code:
			if location:
				loc = location
			else:
				from model.activity import Location
				loc = Location.get_by_key_name(location_code)
			pass_template.location_code = loc.code
			pass_template.location_name = loc.name



		pass_template.get_location(reset=True)
		return pass_template
Exemplo n.º 20
0
 def update_passes(self):
     from model.passes import PassTemplate
     PassTemplate.update_from_json()
     self.write("OK")
Exemplo n.º 21
0
 def update_passes(self):
     from model.passes import PassTemplate
     PassTemplate.update_from_json()
     self.write("OK")     
Exemplo n.º 22
0
    def create_or_update(self,
                         name=None,
                         slug=None,
                         description=None,
                         price=None,
                         schedule=None,
                         neighborhood_name=None,
                         location=None,
                         location_code=None,
                         price_rating=None,
                         api=False,
                         image_url=None,
                         url=None,
                         user=None,
                         organizationName=None,
                         organizationUrl=None,
                         **kwargs):

        self.user = user

        if url:
            # this does not allow file-based passes to have updated listings on Passtiche
            return self.pass_file(url)
        if not slug:
            slug = PassTemplate.get_slug(name) or 'event'
        keyname = slug
        if location_code:
            keyname += "-%s" % location_code
        if self.user:
            keyname += "~%s" % self.user.short_code

        logging.info(kwargs)
        if kwargs.get('new'):
            pass_template = None
        else:
            logging.info('getting pass template %s' % keyname)
            pass_template = PassTemplate.get_by_key_name(keyname)
            if pass_template:
                logging.info('found pass template %s' % keyname)

        if not pass_template:
            logging.info('creating new pass template')
            pass_template = PassTemplate(key_name=keyname,
                                         name=name,
                                         slug=slug)
            from utils import string as str_utils
            code = str_utils.genkey(length=4)
            pass_template.short_code = code

            if self.user and not self.user.is_admin():
                pass_template.owner = self.user
            if not location and location_code:
                from model.activity import Location
                location = Location.get_by_key_name(location_code)
            if location:
                location_name = location.name
            else:
                location_name = ''

            searchFields = [
                search.TextField(name='name', value=name),
                search.TextField(name='code', value=code),
                search.TextField(name='keyname', value=keyname),
                search.TextField(name='loc', value=(location_code or '')),
                search.TextField(name='location_name', value=location_name),
                search.DateField(name='date',
                                 value=datetime.datetime.now().date())
            ]

            if self.user:
                searchFields.append(
                    search.TextField(name='owner', value=self.user.short_code))
            pass_doc = search.Document(fields=searchFields)
            logging.info('adding pass doc to index')
            search.Index(name=_INDEX_NAME).put(pass_doc)

        elif name:
            pass_template.name = name

        if description:
            pass_template.description = description
        if price:
            pass_template.price = int(price)
        if price_rating is not None:
            pass_template.price_rating = price_rating

        if organizationName:
            pass_template.organizationName = organizationName

        if organizationUrl:
            if not organizationUrl.startswith('http://'):
                organizationUrl = 'http://%s' % organizationUrl
            pass_template.organizationUrl = organizationUrl

        if image_url:
            pass_template.image_url = image_url

        if schedule:

            pass_template.schedule = schedule

            pass_template = set_time_format(pass_template)

            # TODO: starts time
        if neighborhood_name:
            pass_template.neighborhood_name = neighborhood_name

        if location_code:
            if location:
                loc = location
            else:
                from model.activity import Location
                loc = Location.get_by_key_name(location_code)
            pass_template.location_code = loc.code
            pass_template.location_name = loc.name

        pass_template.get_location(reset=True)
        return pass_template