Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. import django
  2. import os
  3. import sys
  4. import requests
  5. import json
  6.  
  7. def find_filename(file_name):
  8. return os.path.basename(file_name)
  9.  
  10. # Logging in ==============================================================
  11. session = requests.session()
  12. url = "http://localhost:8000/accounts/login/"
  13. print("\nPlease log in\n")
  14. username = input("Username: ")
  15. password = input("Password: ")
  16. session.get(url)
  17. token = session.cookies['csrftoken']
  18. payload = {'username': username, 'password': password, "csrfmiddlewaretoken": token}
  19. r = session.post(url, data=payload, headers=dict(Referer=url))
  20.  
  21. # obtaining a master list of reports to use
  22. master_report_list = []
  23. url = "http://localhost:8000/fetch_all/"
  24. session.get(url)
  25. token = session.cookies['csrftoken']
  26. payload = {"csrfmiddlewaretoken": token}
  27. rr = session.post(url, data=payload, headers=dict(Referer=url))
  28. data = json.loads(rr.text)
  29. if data == "No public reports":
  30. print("There seems to be no reports")
  31. else:
  32. master_report_list = data
  33.  
  34. master = True
  35. while master == True:
  36. decision = input("\n1: List all public reports"
  37. "\n2: List specific report""\n3: Terminate\nPlease choose number option: ")
  38.  
  39. # grabbing all public reports
  40. if decision == "1":
  41. print("\nHere are all public reports: ")
  42. for report in master_report_list:
  43. print(" ", report[0], "\n - ", report[1], "\n")
  44.  
  45. decision = "2"
  46.  
  47. # retrieving the specific report and giving options to download files
  48. if decision == "2":
  49. # grabbing the report =====================================================
  50. t = True
  51. while t:
  52. title = input("Which existing report? ")
  53. for each in master_report_list:
  54. if title != each[0]:
  55. continue
  56. else:
  57. t = False
  58. break
  59. url = "http://localhost:8000/fetch/"
  60. session.get(url)
  61. token = session.cookies['csrftoken']
  62. payload = {"title": title, "csrfmiddlewaretoken": token}
  63. rr = session.post(url, data=payload, headers=dict(Referer=url))
  64. data = json.loads(rr.text)
  65. if data == "nothing":
  66. print("There are no documents attached to this report")
  67. continue
  68. else:
  69. print("\nHere are the files in this report:\n ")
  70. dictionary = {}
  71. count = 1
  72. for document in data:
  73. print(" ", count, os.path.basename(document))
  74. dictionary[count] = document
  75. count += 1
  76.  
  77. # grabbing the file to download ===========================================
  78. go = True
  79. while go:
  80. key = input("\nDownload which file? (Choose number or no): ").lower()
  81. if key.isdigit():
  82. file = dictionary[int(key)]
  83. elif key == "no":
  84. print("\n Returning back to menu")
  85. break
  86. else:
  87. continue
  88. url = "http://localhost:8000/download/"
  89. session.get(url)
  90. token = session.cookies['csrftoken']
  91. payload = {"title": title, "name": file, "csrfmiddlewaretoken": token}
  92. rr = session.post(url, data=payload, headers=dict(Referer=url))
  93. if rr.text == "File not found":
  94. print("File not Found")
  95. else:
  96. with open(find_filename(file), 'wb') as reader:
  97. for chunk in rr:
  98. reader.write(chunk)
  99. print("\n Downloaded", os.path.basename(dictionary[int(key)]))
  100.  
  101. test = input("\nDownload another file? (Yes or No): ").lower()
  102. while test != "yes" or test != "no":
  103. if test == "yes":
  104. break
  105. elif test == "no":
  106. go = False
  107. break
  108. else:
  109. test = input(" Yes or No please: ")
  110. if decision == "3":
  111. break
  112. print("\nTerminated")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement