示例#1
0
def save_scanned(payload):
    EXPIRATION_OPTIONS = {
        '1m': 1,
        '3m': 3,
        '1y': 12
    }
    ONE_MONTH_IN_SECONDS = 60 * 60 * 24 * 30

    expiration = time.mktime(datetime.now().timetuple()) + (ONE_MONTH_IN_SECONDS * EXPIRATION_OPTIONS.get(payload.get('expiration', '1m')))

    scanned = Scanned(
        id=generate_id(payload['title'], payload['excerpt'], str(datetime.now())),
        title=payload['title'],
        excerpt=payload['excerpt'],
        result=ast.literal_eval(payload['result']),
        created=datetime.now(),
        expiration=datetime.fromtimestamp(expiration),
        verified=payload['verified']
    )

    saved = scanned.save()
    if not saved:
        raise Exception
    return {
        'id': scanned.id,
        'title': scanned.title,
        'excerpt': scanned.excerpt,
        'expiration': str(scanned.expiration)
    }
示例#2
0
def save_alert(payload):
    alert = Alert.objects(email=payload['email']).first()
    if not alert:
        alert = Alert(
                id=generate_id(payload['email']),
                email=payload['email']
                )
        _add_search_to_alert(payload['search'], alert)
    else:
        searches = [s.search for s in alert.searches]
        search_exists = False
        for search in searches:
            if payload['search'] == search:
                search_exists = True
                break
        if search_exists:
            return
        _add_search_to_alert(payload['search'], alert)

    result = alert.save()
    if not result:
        raise Exception

    '''
    Add init() before validate() to ensure it always use the same
    celery instance, despite flask multithrading
    '''
    tipi_tasks.init()
    tipi_tasks.validate.send_validation_emails.apply_async()
示例#3
0
 def __refresh_parliamentarygroups(self):
     groups = Deputy.objects.distinct('parliamentarygroup')
     groups.remove('')
     for group in groups:
         pg = ParliamentaryGroup(id=generate_id(group),
                                 name=group,
                                 shortname=group,
                                 active=True)
         pg.save()
示例#4
0
def _add_search_to_alert(search, alert):
    now = datetime.now()
    hash = generate_id(alert.email, str(search), str(now))
    alert.searches.append(Search(
        hash=hash,
        search=search,
        dbsearch=str(
            SearchInitiativeParser(
                json.loads(search)
                ).params
            ),
        created=now
        )
    )
示例#5
0
 def generate_id(self, initiative):
     return generate_id(initiative['reference'])
示例#6
0
 def generate_id(self, reference, text):
     return generate_id(reference, text)
 def generate_id(self):
     return generate_id(self.deputy['name'])
示例#8
0
 def generate_answer_id(self, initiative):
     return generate_id(initiative['reference'],
                        initiative['initiative_type_alt'])
示例#9
0
 def generate_id(self, link):
     return generate_id(link)