def create_services(event_id, serial_no=''):
    """Creates services and associates them with `event_id`. Service names
    have an optional `serial_no` that can be used to make them unique.
    """
    test_micro = 'TestMicrolocation{}_{}'.format(serial_no, event_id)
    test_track = 'TestTrack{}_{}'.format(serial_no, event_id)
    test_session = 'TestSession{}_{}'.format(serial_no, event_id)
    test_speaker = 'TestSpeaker{}_{}'.format(serial_no, event_id)
    test_sponsor = 'TestSponsor{}_{}'.format(serial_no, event_id)
    test_sponsor_type = 'TestSponsorType{}_{}'.format(serial_no, event_id)
    test_session_type = 'TestSessionType{}_{}'.format(serial_no, event_id)
    test_social_link = 'TestSocialLink{}_{}'.format(serial_no, event_id)

    microlocation = Microlocation(name=test_micro, event_id=event_id)
    track = Track(
        name=test_track,
        description='descp',
        event_id=event_id,
        color='#caf034'
    )
    session_type = SessionType(
        name=test_session_type,
        length=30,
        event_id=event_id
    )
    session = Session(title=test_session,
                      long_abstract='descp',
                      start_time=datetime(2014, 8, 4, 12, 30, 45),
                      end_time=datetime(2014, 8, 4, 13, 00, 00),
                      event_id=event_id,
                      session_type=session_type)
    speaker = Speaker(name=test_speaker,
                      email='*****@*****.**',
                      organisation='org',
                      country='japan',
                      event_id=event_id)
    sponsor = Sponsor(name=test_sponsor, sponsor_type=test_sponsor_type,
                      event_id=event_id, level='level')
    social_link = SocialLink(
        name=test_social_link,
        link='http://fb.com',
        event_id=event_id
    )

    save_to_db(social_link, 'SocialLink saved')
    save_to_db(session_type, 'SessionType saved')
    save_to_db(microlocation, 'Microlocation saved')
    save_to_db(track, 'Track saved')
    save_to_db(session, 'Session saved')
    save_to_db(speaker, 'Speaker saved')
    save_to_db(sponsor, 'Sponsor saved')
def save_sponsors_from_json(json, event_id=None):
    event_id = event_id if event_id else json['event_id']
    event = DataGetter.get_event(event_id)
    sponsors_enabled = json['sponsors_enabled']

    if sponsors_enabled:
        ids = []
        for sponsor in json['sponsors']:
            if sponsor['id'] and represents_int(sponsor['id']):
                item = Sponsor.query.get(sponsor['id'])
            else:
                item = Sponsor(event_id=event_id)

            if sponsor['name'].strip() == '':
                continue

            item.name = sponsor['name']
            item.level = sponsor['level']
            item.sponsor_type = sponsor['type']
            item.url = sponsor['url']
            item.description = sponsor['description']

            save_to_db(item)

            if item.logo != sponsor['logo']:
                if sponsor['logo'] and sponsor['logo'] != '':
                    item.logo = save_event_image(
                        sponsor['logo'],
                        UPLOAD_PATHS['sponsors']['logo'].format(
                            event_id=int(event.id), id=int(item.id)))
                else:
                    item.logo = ''

            save_to_db(item)
            ids.append(item.id)

        if len(ids) > 0:
            Sponsor.query.filter(~Sponsor.id.in_(ids)).filter_by(
                event_id=event_id).delete(synchronize_session='fetch')
    else:
        Sponsor.query.filter_by(event_id=event_id).delete(
            synchronize_session='fetch')

    event.state = json['state'] if event.location_name.strip(
    ) != '' else 'Draft'
    save_to_db(event)
    return {'event_id': event.id}
Exemple #3
0
 def create(self, payload):
     response = ResponseBuilder()
     attachment = self.save_file(
         payload['attachment']
     ) if payload['attachment'] is not None else None
     sponsor = Sponsor()
     sponsor.name = payload['name']
     sponsor.phone = payload['phone']
     sponsor.email = payload['email']
     sponsor.note = payload['note']
     sponsor.stage = str(
         payload['stage']
     ) if payload['stage'] else '1'  # default to one as lead
     sponsor.type = str(
         payload['type']
     ) or '4' if sponsor.stage == '3' else None  # default to four if stage is official
     sponsor.attachment = attachment
     sponsor.url = payload['url']
     sponsor.callback_url = payload['callback_url']
     db.session.add(sponsor)
     try:
         db.session.commit()
         #add sponsor_template
         if payload['stage'] in [3, '3']:
             sponsor = db.session.query(Sponsor).filter_by(
                 stage=3).order_by(desc(Sponsor.id)).first()
             sponsor_template = SponsorTemplate()
             sponsor_template.sponsor_id = sponsor.id
             sponsor_template.attachment = sponsor.attachment
             db.session.add(sponsor_template)
             db.session.commit()
         return response.set_data(sponsor.as_dict()).set_message(
             'Data created succesfully').build()
     except SQLAlchemyError as e:
         data = e.orig.args
         return response.set_data(data).set_error(True).build()
Exemple #4
0
    def setUp(self):
        self.app = Setup.create_app()
        with app.test_request_context():
            event = Event(name=UNICODE_STRING,
                          start_time=datetime(2013, 8, 4, 12, 30, 45),
                          end_time=datetime(2016, 9, 4, 12, 30, 45))
            event.owner = 1

            microlocation = Microlocation(name=UNICODE_STRING)
            session = Session(title=UNICODE_STRING,
                              long_abstract='descp',
                              start_time=datetime(2014, 8, 4, 12, 30, 45),
                              end_time=datetime(2015, 9, 4, 12, 30, 45))
            speaker = Speaker(name=UNICODE_STRING,
                              email='*****@*****.**',
                              organisation='org',
                              country='japan')
            sponsor = Sponsor(name=UNICODE_STRING)

            save_to_db(event, "Event saved")
            save_to_db(microlocation, 'Microlocation saved')
            save_to_db(session, 'Session saved')
            save_to_db(speaker, 'Speaker saved')
            save_to_db(sponsor, 'Sponsor saved')
Exemple #5
0
    def update(self, id, payload):
        response = ResponseBuilder()
        sponsor = db.session.query(Sponsor).filter_by(id=id)
        data = sponsor.first().as_dict() if sponsor.first() else None
        if data['attachment'] is not None and payload['attachment']:
            Helper().silent_remove(current_app.config['STATIC_DEST'] +
                                   data['attachment'])
        if data is None:
            return response.set_error(True).set_message(
                'data not found').set_data(None).build()
        if data['stage'] != payload['stage']:
            log = SponsorInteractionLog()
            _from = SPONSOR_STAGES[data['stage']] if data['stage'] else None
            _to = SPONSOR_STAGES[str(
                payload['stage'])] if payload['stage'] else None
            log.description = 'Admin move stage from: ' + _from + ' to: ' + _to
            log.sponsor_id = id
            db.session.add(log)

        if (data['type'] != payload['type']):
            log = SponsorInteractionLog()
            if data['type'] not in ['null', None]:
                _from = SPONSOR_TYPES[data['type']]
            else:
                _from = 'None'
            if payload['type'] not in ['null', None]:
                _to = SPONSOR_TYPES[str(
                    payload['type'])] if payload['type'] != 'null' else None
            else:
                _to = 'None'

            log.description = 'Admin move stage from: ' + _from + ' to: ' + _to
            log.sponsor_id = id
            db.session.add(log)

        new_data = super().filter_update_payload(payload)

        if SPONSOR_STAGES[str(payload['stage'])] is not SPONSOR_STAGES['3']:
            new_data['type'] = None
        if payload['attachment']:
            attachment = self.save_file(
                payload['attachment']
            ) if payload['attachment'] is not None else None
            new_data['attachment'] = attachment
        new_data['updated_at'] = datetime.datetime.now()
        new_data['url'] = payload['url']
        new_data['callback_url'] = payload['callback_url']
        sponsor.update(new_data)
        try:
            db.session.commit()
            #when update sponsor to official, automatically create sponsor_template
            if new_data['stage'] in [3, '3']:
                check_sponsor_template = db.session.query(
                    SponsorTemplate).filter_by(sponsor_id=id).first()
                if check_sponsor_template is None:
                    sponsor_update = Sponsor()
                    sponsor_update = db.session.query(Sponsor).filter(
                        and_(Sponsor.stage == 3, Sponsor.id == id)).first()
                    sponsor_template = SponsorTemplate()
                    sponsor_template.sponsor_id = sponsor_update.id
                    sponsor_template.attachment = sponsor_update.attachment
                    db.session.add(sponsor_template)
                    db.session.commit()
            return response.set_data(sponsor.first().as_dict()).build()

        except SQLAlchemyError as e:
            data = e.orig.args
            return response.set_data(data).set_error(True).build()