update_proposals.rkt 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #lang racket/base
  2. ;; This program updates an entry in a proposals database.
  3. (require racket/cmdline
  4. racket/date
  5. db
  6. "config.rkt") ; load configuration file
  7. (define progname "update_proposals.rkt")
  8. ; give us the date in YYYY-MM-DD format
  9. (date-display-format 'iso-8601)
  10. ; set up command line arguments
  11. (define mode (command-line
  12. #:program "update_proposals"
  13. #:args ([updatetype "help"]) ; (add, update, list-open help)
  14. updatetype))
  15. ; print some help
  16. (define (printhelp)
  17. (displayln (string-append "Usage: "
  18. progname " MODE"))
  19. (newline)
  20. (displayln "Where MODE is one of:")
  21. (displayln " add\t\t - add new proposal to database.")
  22. (displayln " update\t\t - update a proposal with results.")
  23. (displayln " list-open\t - Show all submitted (but not resolved) proposals.")
  24. (displayln " help\t\t - Show this help message.")
  25. (newline)
  26. (displayln "Copyright 2019-2020 George C. Privon"))
  27. ; set up a condensed prompt for getting information
  28. (define (getinput prompt)
  29. (write-string prompt)
  30. (write-string ": ")
  31. (read-line))
  32. ; take an input result from the SQL search and write it out nicely
  33. (define (printentry entry)
  34. (displayln (string-append
  35. (number->string (vector-ref entry 0))
  36. ": "
  37. (vector-ref entry 1)
  38. "("
  39. (vector-ref entry 2)
  40. "; PI: "
  41. (vector-ref entry 4)
  42. ") \""
  43. (vector-ref entry 3)
  44. "\"")))
  45. ; add a new proposal to the database
  46. (define (addnew)
  47. (displayln "Adding new proposal to database.")
  48. ; user inputs proposal data
  49. (define proptype (getinput "Proposal type"))
  50. (define org (getinput "Submitting organization"))
  51. (define solic (getinput "Solitation/call"))
  52. (define tele (getinput "Telescope"))
  53. (define title (getinput "Proposal title"))
  54. (define pi (getinput "PI"))
  55. (define coi (getinput "CoIs"))
  56. ; assume all these proposals are submitted, don't ask the user
  57. (define status "submitted")
  58. (define submitdate (getinput "Submit date (YYYY-MM-DD)"))
  59. (define oID (getinput "Organization's proposal ID"))
  60. ; do the INSERT into the Sqlite database
  61. (query-exec conn "INSERT INTO proposals (type, organization, solicitation, telescope, PI, title, CoI, status, submitdate, orgpropID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
  62. proptype org solic tele pi title coi status submitdate oID))
  63. ; update an entry with new status (accepted, rejected, etc.)
  64. (define (update ID)
  65. (displayln (string-append "Updating entry " (number->string ID)))
  66. (define entry (query-maybe-row conn "SELECT * FROM proposals WHERE ID=?" ID))
  67. (cond
  68. [(eq? #f entry) (error "Invalid ID. Row not found")])
  69. (displayln (string-append "Current status is: "
  70. (vector-ref entry 9)
  71. " ("
  72. (vector-ref entry 10)
  73. ")"))
  74. (write-string "Please enter new status: ")
  75. (define newstatus (read-line))
  76. ;(write-string "Please enter date of updated status (leave blank to use current date): ")
  77. ;(define resdate (read-line))
  78. (define resdate (date->string (seconds->date (current-seconds))))
  79. ; now update that entry
  80. (query-exec conn "UPDATE proposals SET status=?, resultdate=? WHERE ID=?"
  81. newstatus
  82. resdate
  83. ID)
  84. (displayln "Entry updated."))
  85. ; retrieve and print the proposals whose status is still listed as "submitted"
  86. (define (printopen)
  87. ; retrieve all proposals wh
  88. (define unfinished (query-rows conn "SELECT ID,telescope,solicitation,title,PI FROM proposals WHERE status='submitted'"))
  89. (displayln (string-append (number->string (length unfinished)) " pending proposals found."))
  90. (newline)
  91. ; print all the unresolved proposals to the screen
  92. (map printentry unfinished))
  93. ; find proposals waiting for updates
  94. (define (findpending)
  95. (write-string "Updating proposals")
  96. (printopen)
  97. (write-string "Please enter a proposal number to edit (enter 0 or nothing to exit): ")
  98. (define upID (read-line))
  99. (cond
  100. [(eq? (string->number upID) 0) (exit)]
  101. [(string->number upID) (update (string->number upID))]
  102. [else (exit)]))
  103. ; make sure we can use the sqlite3 connection
  104. (cond (not (sqlite3-available?))
  105. (error "Sqlite3 library not available."))
  106. ; open the database file
  107. (define conn (sqlite3-connect #:database dbloc))
  108. ; determine which mode we're in
  109. (cond
  110. [(regexp-match "help" mode) (printhelp)]
  111. [(regexp-match "add" mode) (addnew)]
  112. [(regexp-match "update" mode) (findpending)]
  113. [(regexp-match "list-open" mode) (printopen)]
  114. [else (error (string-append "Unknown mode. Try " progname " help\n\n"))])
  115. ; close the databse
  116. (disconnect conn)