Snippets

ASD Technologies add_starter_kit

Updated by Aleksandr Petrov

File add_starter_kit.py Modified

  • Ignore whitespace
  • Hide word diff
-# version 2020.10.28.0117
+# version 2020.10.28.1539
 
+from novakid.backend.db.utils import exists
 from novakid.backend.utils.other import log2console
-from novakid.backend.student_game_profile.models import GameProfileModel
+from novakid.backend.student_game_profile.models import GameProfileModel, ItemTransactionLogModel
 from novakid.backend.student_game_profile.models_processes import create_items_transactions_by_starter_rules
 
 
-def add_starter_kit(request, dry_run=True):
+def add_starter_kit(request, dry_run=True, logprint=False):
 
     log2console(f'add_starter_kit: dry_run={dry_run}')
     profiles_all = 0
     query = db_session.query(
         GameProfileModel
     )
+    query_log = db_session.query(ItemTransactionLogModel)
     for profile_id in profiles_ids:
         profiles_all += 1
         if profiles_all % 1000 == 0:
         with request.tm:
             profile = query.get(profile_id)
             if profile:
-                if profile.profile_items:
+                profile_id = profile.student_id
+                query_log = db_session.query(
+                    ItemTransactionLogModel
+                ).filter(
+                    ItemTransactionLogModel.profile_id == profile_id,
+                    ItemTransactionLogModel.evidence.like('starter_rule:%')
+                )
+                if exists(query_log):
                     continue
                 profiles_found += 1
                 if dry_run is False:
                     create_items_transactions_by_starter_rules(request, profile_id=profile.student_id)
                     db_session.refresh(profile)
-                    profiles_started += 1
-                log2console(f'{[(profile.student_id, x.item.label, x.item_id, x.balance) for x in profile.profile_items]}')
+                    if exists(query_log):
+                        profiles_started += 1
+                if logprint:
+                    pres = [(profile.student_id, x.item.label, x.item_id, x.balance) for x in profile.profile_items]
+                    if pres:
+                        log2console(f'{pres}')
     log2console(f'profiles_all: {profiles_all}')
     log2console(f'profiles_found: {profiles_found}')
     log2console(f'profiles_started: {profiles_started}')
Created by Aleksandr Petrov

File add_starter_kit.py Added

  • Ignore whitespace
  • Hide word diff
+# version 2020.10.28.0117
+
+from novakid.backend.utils.other import log2console
+from novakid.backend.student_game_profile.models import GameProfileModel
+from novakid.backend.student_game_profile.models_processes import create_items_transactions_by_starter_rules
+
+
+def add_starter_kit(request, dry_run=True):
+
+    log2console(f'add_starter_kit: dry_run={dry_run}')
+    profiles_all = 0
+    profiles_found = 0
+    profiles_started = 0
+
+    db_session = request.db_session
+    profiles_ids = []
+    with request.tm:
+        q = db_session.query(
+            GameProfileModel.student_id
+        )
+        for x in q:
+            profiles_ids.append(x[0])
+    log2console(f'all profiles: {len(profiles_ids)}')
+
+    query = db_session.query(
+        GameProfileModel
+    )
+    for profile_id in profiles_ids:
+        profiles_all += 1
+        if profiles_all % 1000 == 0:
+            log2console(f'profiles: {profiles_all}:{profiles_started}')
+        with request.tm:
+            profile = query.get(profile_id)
+            if profile:
+                if profile.profile_items:
+                    continue
+                profiles_found += 1
+                if dry_run is False:
+                    create_items_transactions_by_starter_rules(request, profile_id=profile.student_id)
+                    db_session.refresh(profile)
+                    profiles_started += 1
+                log2console(f'{[(profile.student_id, x.item.label, x.item_id, x.balance) for x in profile.profile_items]}')
+    log2console(f'profiles_all: {profiles_all}')
+    log2console(f'profiles_found: {profiles_found}')
+    log2console(f'profiles_started: {profiles_started}')
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.