source: OpenWorkouts-current/ow/migrations/2.py @ 42baca4

current
Last change on this file since 42baca4 was 42baca4, checked in by Borja Lopez <borja@…>, 5 years ago

(#39) Do not allow duplicated workouts by default when uploading track files.
We still allow users to add duplicates if they want, by checking a checkbox
we show in the upload workout form when we find a possible duplicate.

  • Property mode set to 100644
File size: 1.3 KB
Line 
1
2
3def migrate(root):
4    """
5    Adds the "hashed" workout index to the main catalog, reindexing all
6    existing workouts.
7
8    >>> from decimal import Decimal
9    >>> from datetime import datetime, timezone, timedelta
10    >>> from ow.catalog import get_catalog
11    >>> from ow.models.root import OpenWorkouts
12    >>> from ow.models.user import User
13    >>> from ow.models.workout import Workout
14    >>> root = OpenWorkouts()
15    >>> indexes = get_catalog(root)
16    >>> 'hashed' in root.catalog
17    True
18    >>> del root.catalog['hashed']
19    >>> 'hashed' in root.catalog
20    False
21    >>> user = User(email='user@example.net')
22    >>> workout = Workout(start=datetime.now(timezone.utc))
23    >>> workout.duration = timedelta(seconds=3600)
24    >>> workout.distance = Decimal(30)
25    >>> workout.sport = 'cycling'
26    >>> workout.title = 'cycling in migrations'
27    >>> root.add_user(user)
28    >>> user.add_workout(workout)
29    >>> migrate(root)
30    >>> 'hashed' in root.catalog
31    True
32    >>> hashes = [h for h in root.catalog['hashed']._fwd_index]
33    >>> workout.hashed in hashes
34    True
35    >>> 'some-faked-non-existant-hash' in hashes
36    False
37    """
38    root._update_indexes()
39    for user in root.users:
40        for workout in user.workouts():
41            root.reindex(workout)
Note: See TracBrowser for help on using the repository browser.