Пример #1
0
    def trans():
        if new_event.id:
            event = get_event_by_id(service_user, sln_settings.solution,
                                    new_event.id)
            if new_event.new_picture:
                event.picture_version += 1
        else:
            event = Event(
                parent=parent_key(service_user, sln_settings.solution))
            event.picture_version = 0

        event.title = new_event.title
        event.place = new_event.place
        event.organizer = new_event.organizer
        event.description = new_event.description
        start_dates = []
        for start_date in new_event.start_dates:
            start_dates.append(start_date.toEpoch())

        start_dates_sorted, end_dates_sorted = zip(
            *sorted(zip(start_dates, new_event.end_dates)))
        startDates = list(start_dates_sorted)
        new_event.end_dates = list(end_dates_sorted)

        event.last_start_date = max(startDates)
        event.start_dates = start_dates
        event.end_dates = new_event.end_dates
        event.first_start_date = event.get_first_event_date()
        event.url = new_event.external_link
        event.picture = picture
        event.calendar_id = new_event.calendar_id
        sln_settings.updates_pending = True
        put_and_invalidate_cache(event, sln_settings)
        return sln_settings
Пример #2
0
def new_event_received(service_user, message_flow_run_id, member, steps,
                       end_id, end_message_flow_id, parent_message_key, tag,
                       result_key, flush_id, flush_message_flow_id,
                       service_identity, user_details):
    from solutions.common.bizz.messaging import _get_step_with_id

    logging.info("_flow_member_result_new_event: \n %s" % steps)

    calendar = _get_step_with_id(steps, 'message_calendar')

    title = _get_step_with_id(steps, 'message_title')
    if not title:
        logging.error(
            "Did not find step with id 'title'. Can not process message_flow_member_result with tag %s"
            % tag)
        return None

    date_ = _get_step_with_id(steps, 'message_date')
    if not date_:
        logging.error(
            "Did not find step with id 'date'. Can not process message_flow_member_result with tag %s"
            % tag)
        return None

    start_time = _get_step_with_id(steps, 'message_start_time')
    if not start_time:
        logging.error(
            "Did not find step with id 'start_time'. Can not process message_flow_member_result with tag %s"
            % tag)
        return None

    end_time = _get_step_with_id(steps, 'message_end_time')
    if not end_time:
        logging.error(
            "Did not find step with id 'end_time'. Can not process message_flow_member_result with tag %s"
            % tag)
        return None

    description = _get_step_with_id(steps, 'message_description')
    if not description:
        logging.error(
            "Did not find step with id 'description'. Can not process message_flow_member_result with tag %s"
            % tag)
        return None

    place = _get_step_with_id(steps, 'message_place')
    if not place:
        logging.error(
            "Did not find step with id 'place'. Can not process message_flow_member_result with tag %s"
            % tag)
        return None

    photo = _get_step_with_id(steps, 'message_photo')
    if not photo:
        logging.error(
            "Did not find step with id 'photo'. Can not process message_flow_member_result with tag %s"
            % tag)
        return None

    sln_settings = get_solution_settings(service_user)
    app_user = user_details[0].toAppUser()
    calendars_ids_admin = get_solution_calendar_ids_for_user(
        service_user, sln_settings.solution, app_user)

    if len(calendars_ids_admin) == 0:
        logging.warn("User %s isn't a calendar admin anymore" %
                     app_user.email())
        return None

    calendar_id = None
    if calendar and calendar.form_result.result:
        calendar_id = long(calendar.form_result.result.value)
    else:
        if sln_settings.default_calendar in calendars_ids_admin:
            calendar_id = sln_settings.default_calendar
        else:
            calendar_id = calendars_ids_admin[0]

    sc = SolutionCalendar.get_by_id(
        calendar_id, parent_key(service_user, sln_settings.solution))
    if not sc:
        logging.warn("Calendar %s does not exists anymore" % calendar_id)
        return None

    event = Event(parent=parent_key(service_user, sln_settings.solution))
    event.picture_version = 0
    event.title = title.form_result.result.value
    event.place = place.form_result.result.value
    event.description = description.form_result.result.value

    start_date = get_epoch_from_datetime(
        datetime.fromtimestamp(date_.form_result.result.value).date()
    ) + start_time.form_result.result.value
    event.last_start_date = start_date
    event.start_dates = [start_date]
    event.end_dates = [end_time.form_result.result.value]
    event.first_start_date = event.get_first_event_date()
    event.calendar_id = calendar_id
    if photo.form_result:
        result = urlfetch.fetch(photo.form_result.result.value, {},
                                "GET", {},
                                False,
                                False,
                                deadline=10 * 60)
        if result.status_code != 200:
            logging.error(
                "Failed to download photo upload for new event whith link: %s"
                % photo.form_result.result)
            picture = None
        else:
            img_str = result.content
            img_b64 = base64.b64encode(img_str)

            previous_len_img = len(img_b64)
            while len(img_b64) > 512 * 1024:
                img = images.Image(img_str)
                img.im_feeling_lucky()
                img_str = img.execute_transforms(
                    output_encoding=images.JPEG)  # Reduces quality to 85%
                img_b64 = base64.b64encode(img_str)

                if previous_len_img <= len(img_b64):
                    break
                previous_len_img = len(img_b64)

            picture = "data:image/jpg;base64,%s" % img_b64
    else:
        picture = None
    event.picture = picture
    event.put()

    send_message(service_user, u"solutions.common.calendar.update")
    deferred.defer(common_provision, service_user)
    return None