{"action":"created","comment":{"url":"https://api.github.com/repos/edx/ecommerce-scripts/pulls/comments/33643638","id":33643638,"diff_hunk":"@@ -0,0 +1,166 @@\n+#! /usr/bin/env python\n+\n+\"\"\" Verify all completed orders have active enrollments. \"\"\"\n+from contextlib import closing\n+from datetime import timedelta, datetime\n+from itertools import groupby\n+import logging\n+import os\n+import sys\n+\n+import MySQLdb\n+from MySQLdb.cursors import DictCursor\n+\n+logger = logging.getLogger(__name__)\n+\n+\n+def setup_logging():\n+    \"\"\" Setup logging.\n+\n+    Add support for console logging.\n+    \"\"\"\n+    msg_format = '%(asctime)s - %(levelname)s - %(message)s'\n+    ch = logging.StreamHandler()\n+    ch.setFormatter(logging.Formatter(msg_format))\n+    ch.setLevel(logging.DEBUG)\n+    logger.addHandler(ch)\n+    logger.setLevel(logging.DEBUG)\n+\n+\n+ecommerce_db = MySQLdb.connect(host=os.environ['ECOMMERCE_DB_HOST'],\n+                               user=os.environ['ECOMMERCE_DB_USER'],\n+                               passwd=os.environ['ECOMMERCE_DB_PASSWORD'],\n+                               db=os.environ['ECOMMERCE_DB_NAME'])\n+\n+edxapp_db = MySQLdb.connect(host=os.environ['EDXAPP_DB_HOST'],\n+                            user=os.environ['EDXAPP_DB_USER'],\n+                            passwd=os.environ['EDXAPP_DB_PASSWORD'],\n+                            db=os.environ['EDXAPP_DB_NAME'])\n+\n+# This is the number of minutes to look back when retrieving orders (e.g. all orders in the last 15 minutes).\n+ORDER_WINDOW_START_TIME = int(os.environ.get('ORDER_WINDOW_START_TIME', 15))\n+\n+\n+def get_orders(date_placed):\n+    \"\"\" Retrieve all completed orders placed after the given date. \"\"\"\n+    sql = \"\"\"SELECT\n+    u.username\n+    , u.email\n+    , o.number\n+    , o.date_placed\n+    , o.total_excl_tax\n+    , modes.value_text AS 'mode'\n+    , pav.value_text AS 'course_id'\n+FROM\n+    order_order o\n+    JOIN ecommerce_user u ON (o.user_id = u.id)\n+    JOIN order_line ol ON (ol.order_id = o.id)\n+    JOIN catalogue_productattributevalue pav ON (pav.product_id = ol.product_id)\n+    JOIN catalogue_productattribute pa ON (pa.id = pav.attribute_id AND pa.code = 'course_key')\n+    JOIN ( SELECT pav2.product_id, pav2.value_text\n+    FROM catalogue_productattributevalue pav2\n+    JOIN catalogue_productattribute pa2 ON (pa2.id = pav2.attribute_id AND pa2.code = 'certificate_type')\n+    ) AS modes ON (modes.product_id = ol.product_id)\n+    LEFT JOIN refund_refundline rl ON (rl.order_line_id = ol.id)\n+WHERE\n+    o.status = 'Complete'\n+    AND o.date_placed > %s\n+    AND rl.id IS NULL\n+ORDER BY\n+    u.username ASC\n+    , pav.value_text ASC\n+    , o.date_placed ASC;\n+\"\"\"\n+    date_placed = date_placed.strftime('%Y-%m-%d %H:%M:%S')\n+    with closing(ecommerce_db.cursor(DictCursor)) as cursor:\n+        cursor.execute(sql, (date_placed,))\n+        orders = list(cursor.fetchall())\n+\n+    # Identify upgrades and remove the honor purchase.\n+    # TODO We will need to do the same for credit.\n+    for key, grouped_orders in groupby(orders, lambda o: (o['username'], o['course_id'])):\n+        grouped_orders = list(grouped_orders)\n+        if len(grouped_orders) > 1:\n+            modes = set([order['mode'] for order in grouped_orders])\n+            if modes == {'honor', 'verified'}:\n+                username, course_id = key\n+                # The honor order should come before the verified order.\n+                order = grouped_orders[0]\n+                logger.info('User [%s] recently upgraded for course [%s]. Honor order [%s] will be ignored.', username,\n+                            course_id, order['number'])\n+                orders.remove(order)\n+\n+    return orders\n+\n+\n+def get_enrollments(usernames, course_ids):\n+    \"\"\" Retrieve all enrollments for the given courses and users. \"\"\"\n+    sql = \"\"\"SELECT\n+    u.username\n+    , e.course_id\n+    , e.mode\n+    , e.is_active\n+FROM\n+    student_courseenrollment e\n+    JOIN auth_user u ON (u.id = e.user_id)\n+WHERE\n+    u.username IN ({usernames})\n+    AND e.course_id in ({course_ids});\n+\"\"\".format(usernames=','.join('%s' for __ in usernames), course_ids=','.join('%s' for __ in course_ids))","path":"auditing/order_fulfillment.py","position":109,"original_position":109,"commit_id":"3dda6ed2a104ed9b8d9ceeab4e4339aa1bb98072","original_commit_id":"3dda6ed2a104ed9b8d9ceeab4e4339aa1bb98072","user":{"login":"clintonb","id":910510,"avatar_url":"https://avatars.githubusercontent.com/u/910510?v=3","gravatar_id":"","url":"https://api.github.com/users/clintonb","html_url":"https://github.com/clintonb","followers_url":"https://api.github.com/users/clintonb/followers","following_url":"https://api.github.com/users/clintonb/following{/other_user}","gists_url":"https://api.github.com/users/clintonb/gists{/gist_id}","starred_url":"https://api.github.com/users/clintonb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/clintonb/subscriptions","organizations_url":"https://api.github.com/users/clintonb/orgs","repos_url":"https://api.github.com/users/clintonb/repos","events_url":"https://api.github.com/users/clintonb/events{/privacy}","received_events_url":"https://api.github.com/users/clintonb/received_events","type":"User","site_admin":false},"body":"Your sample code is invalid. A simple join won't work. These are strings that would need to be wrapped in quotes, and the quotes are the problem. `MySQLdb` raises an exception if you attempt to pass quotes.","created_at":"2015-07-01T02:24:44Z","updated_at":"2015-07-01T02:24:44Z","html_url":"https://github.com/edx/ecommerce-scripts/pull/1#discussion_r33643638","pull_request_url":"https://api.github.com/repos/edx/ecommerce-scripts/pulls/1","_links":{"self":{"href":"https://api.github.com/repos/edx/ecommerce-scripts/pulls/comments/33643638"},"html":{"href":"https://github.com/edx/ecommerce-scripts/pull/1#discussion_r33643638"},"pull_request":{"href":"https://api.github.com/repos/edx/ecommerce-scripts/pulls/1"}}},"pull_request":{"url":"https://api.github.com/repos/edx/ecommerce-scripts/pulls/1","id":38922465,"html_url":"https://github.com/edx/ecommerce-scripts/pull/1","diff_url":"https://github.com/edx/ecommerce-scripts/pull/1.diff","patch_url":"https://github.com/edx/ecommerce-scripts/pull/1.patch","issue_url":"https://api.github.com/repos/edx/ecommerce-scripts/issues/1","number":1,"state":"open","locked":false,"title":"Added order fulfillment auditing script","user":{"login":"clintonb","id":910510,"avatar_url":"https://avatars.githubusercontent.com/u/910510?v=3","gravatar_id":"","url":"https://api.github.com/users/clintonb","html_url":"https://github.com/clintonb","followers_url":"https://api.github.com/users/clintonb/followers","following_url":"https://api.github.com/users/clintonb/following{/other_user}","gists_url":"https://api.github.com/users/clintonb/gists{/gist_id}","starred_url":"https://api.github.com/users/clintonb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/clintonb/subscriptions","organizations_url":"https://api.github.com/users/clintonb/orgs","repos_url":"https://api.github.com/users/clintonb/repos","events_url":"https://api.github.com/users/clintonb/events{/privacy}","received_events_url":"https://api.github.com/users/clintonb/received_events","type":"User","site_admin":false},"body":"This script should be run periodically (every 15-30 minutes) to ensure that orders are being fulfilled.\r\n\r\nXCOM-423\r\n\r\n@rlucioni @Nickersoft @jimabramson @wedaly ","created_at":"2015-06-30T20:47:51Z","updated_at":"2015-07-01T02:24:44Z","closed_at":null,"merged_at":null,"merge_commit_sha":"c4c348a2445f73500519ca426e2df2e7921d177c","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/edx/ecommerce-scripts/pulls/1/commits","review_comments_url":"https://api.github.com/repos/edx/ecommerce-scripts/pulls/1/comments","review_comment_url":"https://api.github.com/repos/edx/ecommerce-scripts/pulls/comments{/number}","comments_url":"https://api.github.com/repos/edx/ecommerce-scripts/issues/1/comments","statuses_url":"https://api.github.com/repos/edx/ecommerce-scripts/statuses/3dda6ed2a104ed9b8d9ceeab4e4339aa1bb98072","head":{"label":"edx:audit-order-fulfillment","ref":"audit-order-fulfillment","sha":"3dda6ed2a104ed9b8d9ceeab4e4339aa1bb98072","user":{"login":"edx","id":3179841,"avatar_url":"https://avatars.githubusercontent.com/u/3179841?v=3","gravatar_id":"","url":"https://api.github.com/users/edx","html_url":"https://github.com/edx","followers_url":"https://api.github.com/users/edx/followers","following_url":"https://api.github.com/users/edx/following{/other_user}","gists_url":"https://api.github.com/users/edx/gists{/gist_id}","starred_url":"https://api.github.com/users/edx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edx/subscriptions","organizations_url":"https://api.github.com/users/edx/orgs","repos_url":"https://api.github.com/users/edx/repos","events_url":"https://api.github.com/users/edx/events{/privacy}","received_events_url":"https://api.github.com/users/edx/received_events","type":"Organization","site_admin":false},"repo":{"id":38325946,"name":"ecommerce-scripts","full_name":"edx/ecommerce-scripts","owner":{"login":"edx","id":3179841,"avatar_url":"https://avatars.githubusercontent.com/u/3179841?v=3","gravatar_id":"","url":"https://api.github.com/users/edx","html_url":"https://github.com/edx","followers_url":"https://api.github.com/users/edx/followers","following_url":"https://api.github.com/users/edx/following{/other_user}","gists_url":"https://api.github.com/users/edx/gists{/gist_id}","starred_url":"https://api.github.com/users/edx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edx/subscriptions","organizations_url":"https://api.github.com/users/edx/orgs","repos_url":"https://api.github.com/users/edx/repos","events_url":"https://api.github.com/users/edx/events{/privacy}","received_events_url":"https://api.github.com/users/edx/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/edx/ecommerce-scripts","description":"","fork":false,"url":"https://api.github.com/repos/edx/ecommerce-scripts","forks_url":"https://api.github.com/repos/edx/ecommerce-scripts/forks","keys_url":"https://api.github.com/repos/edx/ecommerce-scripts/keys{/key_id}","collaborators_url":"https://api.github.com/repos/edx/ecommerce-scripts/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/edx/ecommerce-scripts/teams","hooks_url":"https://api.github.com/repos/edx/ecommerce-scripts/hooks","issue_events_url":"https://api.github.com/repos/edx/ecommerce-scripts/issues/events{/number}","events_url":"https://api.github.com/repos/edx/ecommerce-scripts/events","assignees_url":"https://api.github.com/repos/edx/ecommerce-scripts/assignees{/user}","branches_url":"https://api.github.com/repos/edx/ecommerce-scripts/branches{/branch}","tags_url":"https://api.github.com/repos/edx/ecommerce-scripts/tags","blobs_url":"https://api.github.com/repos/edx/ecommerce-scripts/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/edx/ecommerce-scripts/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/edx/ecommerce-scripts/git/refs{/sha}","trees_url":"https://api.github.com/repos/edx/ecommerce-scripts/git/trees{/sha}","statuses_url":"https://api.github.com/repos/edx/ecommerce-scripts/statuses/{sha}","languages_url":"https://api.github.com/repos/edx/ecommerce-scripts/languages","stargazers_url":"https://api.github.com/repos/edx/ecommerce-scripts/stargazers","contributors_url":"https://api.github.com/repos/edx/ecommerce-scripts/contributors","subscribers_url":"https://api.github.com/repos/edx/ecommerce-scripts/subscribers","subscription_url":"https://api.github.com/repos/edx/ecommerce-scripts/subscription","commits_url":"https://api.github.com/repos/edx/ecommerce-scripts/commits{/sha}","git_commits_url":"https://api.github.com/repos/edx/ecommerce-scripts/git/commits{/sha}","comments_url":"https://api.github.com/repos/edx/ecommerce-scripts/comments{/number}","issue_comment_url":"https://api.github.com/repos/edx/ecommerce-scripts/issues/comments{/number}","contents_url":"https://api.github.com/repos/edx/ecommerce-scripts/contents/{+path}","compare_url":"https://api.github.com/repos/edx/ecommerce-scripts/compare/{base}...{head}","merges_url":"https://api.github.com/repos/edx/ecommerce-scripts/merges","archive_url":"https://api.github.com/repos/edx/ecommerce-scripts/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/edx/ecommerce-scripts/downloads","issues_url":"https://api.github.com/repos/edx/ecommerce-scripts/issues{/number}","pulls_url":"https://api.github.com/repos/edx/ecommerce-scripts/pulls{/number}","milestones_url":"https://api.github.com/repos/edx/ecommerce-scripts/milestones{/number}","notifications_url":"https://api.github.com/repos/edx/ecommerce-scripts/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/edx/ecommerce-scripts/labels{/name}","releases_url":"https://api.github.com/repos/edx/ecommerce-scripts/releases{/id}","created_at":"2015-06-30T18:27:03Z","updated_at":"2015-06-30T18:27:03Z","pushed_at":"2015-06-30T20:47:52Z","git_url":"git://github.com/edx/ecommerce-scripts.git","ssh_url":"git@github.com:edx/ecommerce-scripts.git","clone_url":"https://github.com/edx/ecommerce-scripts.git","svn_url":"https://github.com/edx/ecommerce-scripts","homepage":null,"size":0,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"base":{"label":"edx:master","ref":"master","sha":"0d155c481713772d6ab3cd6dabe7842ab9643fa3","user":{"login":"edx","id":3179841,"avatar_url":"https://avatars.githubusercontent.com/u/3179841?v=3","gravatar_id":"","url":"https://api.github.com/users/edx","html_url":"https://github.com/edx","followers_url":"https://api.github.com/users/edx/followers","following_url":"https://api.github.com/users/edx/following{/other_user}","gists_url":"https://api.github.com/users/edx/gists{/gist_id}","starred_url":"https://api.github.com/users/edx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edx/subscriptions","organizations_url":"https://api.github.com/users/edx/orgs","repos_url":"https://api.github.com/users/edx/repos","events_url":"https://api.github.com/users/edx/events{/privacy}","received_events_url":"https://api.github.com/users/edx/received_events","type":"Organization","site_admin":false},"repo":{"id":38325946,"name":"ecommerce-scripts","full_name":"edx/ecommerce-scripts","owner":{"login":"edx","id":3179841,"avatar_url":"https://avatars.githubusercontent.com/u/3179841?v=3","gravatar_id":"","url":"https://api.github.com/users/edx","html_url":"https://github.com/edx","followers_url":"https://api.github.com/users/edx/followers","following_url":"https://api.github.com/users/edx/following{/other_user}","gists_url":"https://api.github.com/users/edx/gists{/gist_id}","starred_url":"https://api.github.com/users/edx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/edx/subscriptions","organizations_url":"https://api.github.com/users/edx/orgs","repos_url":"https://api.github.com/users/edx/repos","events_url":"https://api.github.com/users/edx/events{/privacy}","received_events_url":"https://api.github.com/users/edx/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/edx/ecommerce-scripts","description":"","fork":false,"url":"https://api.github.com/repos/edx/ecommerce-scripts","forks_url":"https://api.github.com/repos/edx/ecommerce-scripts/forks","keys_url":"https://api.github.com/repos/edx/ecommerce-scripts/keys{/key_id}","collaborators_url":"https://api.github.com/repos/edx/ecommerce-scripts/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/edx/ecommerce-scripts/teams","hooks_url":"https://api.github.com/repos/edx/ecommerce-scripts/hooks","issue_events_url":"https://api.github.com/repos/edx/ecommerce-scripts/issues/events{/number}","events_url":"https://api.github.com/repos/edx/ecommerce-scripts/events","assignees_url":"https://api.github.com/repos/edx/ecommerce-scripts/assignees{/user}","branches_url":"https://api.github.com/repos/edx/ecommerce-scripts/branches{/branch}","tags_url":"https://api.github.com/repos/edx/ecommerce-scripts/tags","blobs_url":"https://api.github.com/repos/edx/ecommerce-scripts/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/edx/ecommerce-scripts/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/edx/ecommerce-scripts/git/refs{/sha}","trees_url":"https://api.github.com/repos/edx/ecommerce-scripts/git/trees{/sha}","statuses_url":"https://api.github.com/repos/edx/ecommerce-scripts/statuses/{sha}","languages_url":"https://api.github.com/repos/edx/ecommerce-scripts/languages","stargazers_url":"https://api.github.com/repos/edx/ecommerce-scripts/stargazers","contributors_url":"https://api.github.com/repos/edx/ecommerce-scripts/contributors","subscribers_url":"https://api.github.com/repos/edx/ecommerce-scripts/subscribers","subscription_url":"https://api.github.com/repos/edx/ecommerce-scripts/subscription","commits_url":"https://api.github.com/repos/edx/ecommerce-scripts/commits{/sha}","git_commits_url":"https://api.github.com/repos/edx/ecommerce-scripts/git/commits{/sha}","comments_url":"https://api.github.com/repos/edx/ecommerce-scripts/comments{/number}","issue_comment_url":"https://api.github.com/repos/edx/ecommerce-scripts/issues/comments{/number}","contents_url":"https://api.github.com/repos/edx/ecommerce-scripts/contents/{+path}","compare_url":"https://api.github.com/repos/edx/ecommerce-scripts/compare/{base}...{head}","merges_url":"https://api.github.com/repos/edx/ecommerce-scripts/merges","archive_url":"https://api.github.com/repos/edx/ecommerce-scripts/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/edx/ecommerce-scripts/downloads","issues_url":"https://api.github.com/repos/edx/ecommerce-scripts/issues{/number}","pulls_url":"https://api.github.com/repos/edx/ecommerce-scripts/pulls{/number}","milestones_url":"https://api.github.com/repos/edx/ecommerce-scripts/milestones{/number}","notifications_url":"https://api.github.com/repos/edx/ecommerce-scripts/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/edx/ecommerce-scripts/labels{/name}","releases_url":"https://api.github.com/repos/edx/ecommerce-scripts/releases{/id}","created_at":"2015-06-30T18:27:03Z","updated_at":"2015-06-30T18:27:03Z","pushed_at":"2015-06-30T20:47:52Z","git_url":"git://github.com/edx/ecommerce-scripts.git","ssh_url":"git@github.com:edx/ecommerce-scripts.git","clone_url":"https://github.com/edx/ecommerce-scripts.git","svn_url":"https://github.com/edx/ecommerce-scripts","homepage":null,"size":0,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":1,"forks":0,"open_issues":1,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/edx/ecommerce-scripts/pulls/1"},"html":{"href":"https://github.com/edx/ecommerce-scripts/pull/1"},"issue":{"href":"https://api.github.com/repos/edx/ecommerce-scripts/issues/1"},"comments":{"href":"https://api.github.com/repos/edx/ecommerce-scripts/issues/1/comments"},"review_comments":{"href":"https://api.github.com/repos/edx/ecommerce-scripts/pulls/1/comments"},"review_comment":{"href":"https://api.github.com/repos/edx/ecommerce-scripts/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/edx/ecommerce-scripts/pulls/1/commits"},"statuses":{"href":"https://api.github.com/repos/edx/ecommerce-scripts/statuses/3dda6ed2a104ed9b8d9ceeab4e4339aa1bb98072"}}}}