Example #1
0
    def start_activity(self,
                       user_id,
                       name,
                       update: Update,
                       context: CallbackContext,
                       penalty=0):
        active_activity = DB.get_active_activity(user_id)

        stopped_activity = None

        duration = 0

        if active_activity is not None:
            data_now = datetime.datetime.now()
            data_start = datetime.datetime.strptime(
                active_activity['start_time'], '%Y-%m-%d %H:%M:%S')
            duration = (data_now - data_start).seconds / 3600 - penalty / 60

            stopped_activity = active_activity
            stopped_activity['duration'] = duration

        DB.start_activity(user_id, name, duration)

        if stopped_activity is not None and stopped_activity[
                'activity_id'] != 0:
            update.message.reply_text(
                text="✅ Занятие завершено ({})\n\n⏱ Продолжительность: {}.".
                format(
                    stopped_activity['name'],
                    self.get_string_by_duration(stopped_activity['duration'])),
                parse_mode="Markdown")

        if name != "Ничего":
            update.message.reply_text(
                text="🧾 Ты начал занятие \"{}\".".format(name),
                reply_markup=self.get_user_keyboard(
                    update.message.from_user.id))
Example #2
0
    def start_activity(self,
                       user_id,
                       name,
                       update: Update,
                       penalty=0,
                       edit=True,
                       delay=0,
                       project=None):
        active_activity = DB.get_active_activity(user_id)

        stopped_activity = None

        duration = 0

        if active_activity is not None:
            data_now = datetime.datetime.now()
            data_start = datetime.datetime.strptime(
                active_activity['start_time'], '%Y-%m-%d %H:%M:%S')
            duration = (data_now - data_start).seconds / 3600 - penalty / 60

            stopped_activity = active_activity
            stopped_activity['duration'] = duration

        if project is None:
            project = DB.get_active_project(user_id, name)

        if project is not None:
            DB.start_activity(user_id, name, duration, project['id'],
                              update.effective_chat.id, delay)
        else:
            DB.start_activity(user_id, name, duration, None,
                              update.effective_chat.id, delay)

        if stopped_activity is not None and stopped_activity[
                'activity_id'] != 0:
            if edit and update:
                func = update.callback_query.message.reply_to_message.reply_text
            else:
                func = update.message.reply_text

            ac_name = stopped_activity['name'].replace("_", "\_")
            ac_name = ac_name.replace("(", "\(")
            ac_name = ac_name.replace(")", "\)")
            ac_name = ac_name.replace("-", "\-")
            ac_name = ac_name.replace(".", "\.")

            project_string = ""

            if stopped_activity['project_id'] is not None:
                stopped_project = DB.get_project_by_id(
                    stopped_activity['project_id'])

                pr_name = stopped_project['name'].replace("_", "\_")
                pr_name = pr_name.replace("(", "\(")
                pr_name = pr_name.replace(")", "\)")
                pr_name = pr_name.replace(".", "\.")
                pr_name = pr_name.replace("-", "\-")

                project_string = "📂 *Проект:* _%s_\n" % pr_name

            day_activities = DB.get_user_activities_by_day(user_id, 0)

            sum_duration = 0

            for activity in day_activities:
                if activity['activity_id'] == stopped_activity['activity_id']:
                    if any([
                            stopped_activity['project_id'] is None,
                            stopped_activity['project_id'] ==
                            activity['project_id']
                    ]):
                        sum_duration += activity['sum']

            func(
                text=
                "✅ Занятие завершено \({}\)\n{}\n⏱ Продолжительность: {}\.\n😎 Всего за сутки: {}\."
                .format(
                    ac_name,
                    project_string,
                    self.get_string_by_duration(stopped_activity['duration']),
                    self.get_string_by_duration(sum_duration),
                ),
                parse_mode="MarkdownV2")

        if name != "Ничего":
            string = ""

            if project is not None:
                pr_name = project['name'].replace("_", "\_")
                pr_name = pr_name.replace("(", "\(")
                pr_name = pr_name.replace(")", "\)")
                pr_name = pr_name.replace(".", "\.")
                pr_name = pr_name.replace("-", "\-")

                string = "\n📂 *Проект:* _%s_" % pr_name

            ac_name = name.replace("_", "\_")
            ac_name = ac_name.replace("(", "\(")
            ac_name = ac_name.replace(")", "\)")
            ac_name = ac_name.replace("-", "\-")
            ac_name = ac_name.replace(".", "\.")

            delay_str = ""

            if delay > 0:
                delay_str = "\n\n⏱ \+%s мин\." % delay

            text = "🧾 Ты начал занятие \"{}\"\.{}{}\n\n⏹ Остановить: /stop".format(
                ac_name, string, delay_str)

            if update.callback_query is not None:
                update.callback_query.message.edit_text(
                    text=text, parse_mode="MarkdownV2")
            else:
                update.effective_message.reply_text(text=text,
                                                    parse_mode="MarkdownV2")