Ejemplo n.º 1
0
    def handle(self, *args, **options):
        tags = {}
        if options['tagfile']:
            with open(options['tagfile']) as fp:
                for line in fp:
                    key, value = line.strip().split('=')
                    tags[key] = value

        user = User.objects.get(username=options['user'])
        pomodoros = Pomodoro.objects.filter(owner=user)
        if options['no_input'] is False:
            while len(pomodoros):
                self.stdout.write('Delete %d pomodoros for %s' % (len(pomodoros), user))
                if input('Confirm yes/no ').lower() == 'yes':
                    break
        pomodoros.delete()
        self.stdout.write('Importing Pomodoros')

        cursor = DatabaseWrapper(settings_dict={
            'NAME': options['inputfile'],
            'CONN_MAX_AGE': None,
            'OPTIONS': [],
            'AUTOCOMMIT': False,
        }).cursor()
        cursor.execute('SELECT cast(ZWHEN as integer), ZDURATIONMINUTES, ZNAME FROM ZPOMODOROS ')
        for zwhen, zminutes, zname in cursor.fetchall():
            seconds = zminutes * 60

            p = Pomodoro()
            p.title = zname
            p.owner = user
            #  p.start = datetime.datetime.fromtimestamp(zwhen + NSTIMEINTERVAL - seconds, pytz.utc)
            #  p.end = datetime.datetime.fromtimestamp(zwhen + NSTIMEINTERVAL, pytz.utc)
            p.created = datetime.datetime.fromtimestamp(zwhen + NSTIMEINTERVAL - seconds, pytz.utc)

            for word in p.title.split():
                if word.startswith('#'):
                    p.category = word.strip('#')
                    break

            if not p.category:
                for search, tag in tags.items():
                    if search in p.title:
                        p.category = tag
                        break

            p.duration = zminutes
            p.save()
            self.stdout.write('Added %s' % p)
Ejemplo n.º 2
0
    def start(self, request, pk):
        '''Quickstart a Pomodoro'''
        favorite = self.get_object()
        now = timezone.now().replace(microsecond=0)
        try:
            pomodoro = Pomodoro.objects\
                .filter(owner=request.user).latest('start')
        except Pomodoro.DoesNotExist:
            # Handle the case for a new user that does not have any
            # pomodoros at all
            pass
        else:
            if pomodoro.end > now:
                return JsonResponse({
                    'message': 'Cannot replace active pomodoro',
                    'data': PomodoroSerializer(pomodoro).data
                }, status=409)

        pomodoro = Pomodoro()
        pomodoro.title = favorite.title
        pomodoro.category = favorite.category
        pomodoro.owner = request.user
        pomodoro.start = now
        pomodoro.end = pomodoro.start + datetime.timedelta(minutes=favorite.duration)
        pomodoro.save()

        return JsonResponse(PomodoroSerializer(pomodoro).data, status=201)