2 __Description__: A django application for sharing and uploading class notes.
4 __Copyright__: FinalsClub, a 501c3 non-profit organization
6 __License__: GPLv3 except where otherwise noted
8 __Contact__: info@karmanotes.org
10 v3.0 of the karmanotes.org website from the FinalsClub Foundation
14 KarmaWorld is an online database of college lecture notes. KarmaWorld
15 empowers college students to participate in the free exchange of knowledge.
19 The repository and the project are called KarmaWorld. One implementation
20 of KarmaWorld, which is run by FinalsClub Foundation, is called
21 [KarmaNotes](https://www.karmanotes.org/).
27 Before doing anything, you'll need the code. Grab it from github.
29 Clone the project from the central repo using your github account:
31 git clone git@github.com:FinalsClub/karmaworld.git
33 If you aren't using a system setup for github, then grab the project with
36 git clone https://github.com/FinalsClub/karmaworld.git
38 Generally speaking, this will create a subdirectory called `karmaworld` under
39 the directory where the `git` command was run. This git repository directory
40 will be referred to herein as `{project_root}`.
42 There might be some confusion as the git repository's directory will likely be
43 called `karmaworld` (this is `{project_root}`), but there is also a `karmaworld`
44 directory underneath that (`{project_root}/karmaworld`) alongside files like
45 `fabfile.py` (`{project_root}/fabfile.py`) and `README.md`
46 (`{project_root}/README.md`).
48 ## External Software Dependencies
52 KarmaWorld uses [pdf2htmlEX](https://github.com/coolwanglu/pdf2htmlEX) as
53 a dependency. pdf2htmlEX is used to convert uploaded PDF notes into HTML.
55 An [outdated version of pdf2htmlEX](https://github.com/FinalsClub/pdf2htmlEX)
56 is available which includes the
57 [patch](https://github.com/FinalsClub/pdf2htmlEX/commit/3c19f6abd8d59d1b58cf254b7160b332b3f5b517)
58 required for pdf2htmlEX to correctly work with KarmaWorld.
60 Newer versions can be used by applying the patch by hand. It's a fairly
61 simple two-line modification that can be done after installing
66 If you wish to host your system publicly, you'll almost certainly want
67 an SSL certificate signed by a proper authority.
69 You may need to set the `SSL_REDIRECT` environment variable to `true` to
70 make KarmaWorld redirect insecure connections to secure ones.
72 Follow [Heroku's SSL setup](https://devcenter.heroku.com/articles/ssl-endpoint)
73 to get SSL running on your server with Heroku.
75 ## External Service Dependencies
77 Notice: This software makes use of external third party services which require
78 accounts to access the service APIs. Without these third parties available,
79 this software may require considerable overhaul. These services have
80 API keys, credentials, and other information that you must provide to KarmaWorld
81 as environment variables. The best way to persist these environment variables is
82 by using a `.env` file. Copy `.env.example` to `.env` and populate the fields as required.
84 A number of services are required even if running the KarmaWorld web service
85 locally, some of the services are recommended, and some are completely optional
86 even if running the web service on Heroku.
88 Many of these services have free tiers and can be used without charge for
89 development testing purposes.
92 * Copy `.env.example` to `.env` and populate the environment variables there.
94 * [Google Drive](#google-drive)
95 * [Filepicker](#filepicker)
96 * [PostgreSQL](#postgresql)
97 * [Celery](#celery-queue)
98 * Optional but recommended
99 * [IndexDen](#indexden): enables searching through courses, notes, etc
100 * [Heroku](#heroku): the production environment used by karmanotes.org
101 * it might not be possible to run KarmaWorld on Heroku using a free
103 * [Amazon S3](#s3-for-static-files): for static file hosting
104 * Entirely optional (though used in the production environment)
105 * [Twitter](#twitter): share updates about new uploads
106 * [Amazon Mechanical Turk](#amazon-mechanical-turk): generate quizzes, flashcards, etc
107 * [Amazon CloudFront](#amazon-cloudfront-cdn)
108 * [Amazon S3](#s3-for-filepicker): store files uploaded to Filepicker
109 * Filepicker does not support S3 storage in its free tier
112 This project has chosen to use [Heroku](www.heroku.com) to host the Django and
113 celery software. While not a hard requirement, the more up-to-date parts of this
114 documentation will operate assuming Heroku is in use.
116 See README.heroku for more information.
118 #### pdf2htmlEX on Heroku
119 If using Heroku, the default
120 [KarmaNotes Heroku buildpack](https://github.com/FinalsClub/heroku-buildpack-karmanotes)
121 will [include](https://github.com/FinalsClub/heroku-buildpack-karmanotes/blob/master/bin/steps/pdf2htmlex)
122 the [required version of pdf2htmlEX](#pdf2htmlex).
125 Celery uses the Apache Message Queueing Protocol for passing messages to its workers.
127 For production, we recommend using Heroku's CloudAMQP add-on, getting your own CloudAMQP account, or
128 running a queueing system on your own. The `CLOUDAMQP_URL` environment variable must be set correctly
129 for KarmaWorld to be able to use Celery. The `CELERY_QUEUE_NAME` environment variable
130 must be set to the name of the queue you wish to use. Settings this to something unique
131 allows multiple instances of KarmaWorld (or some other software) to share the same queueing server.
133 For development on localhost, `RabbitMQ` is the default for `djcelery` and is well supported. Ensure
134 `RabbitMQ` is installed for local development.
138 PostgreSQL is not necessarily required; other RDBMS could probably be fit into
139 place. However, the code was largely written assuming PostgreSQL will be used.
140 Change to another system with the caveat that it might take some work.
142 There are many cloud providers which provide PostgreSQL databases. Heroku has
143 an add-on for providing a PostgreSQL database. Ensure something like this
144 is made available and installed to the app.
146 For local development, ensure a PostgreSQL is running on localhost or is
147 otherwise accessible.
150 The instructions for creating an [S3](http://aws.amazon.com/s3/) bucket may be
151 [found on Amazon.](http://docs.aws.amazon.com/AmazonS3/latest/gsg/CreatingABucket.html)
153 Two, separate buckets may be used in production: one for static file hosting
154 and one as a communication bus with Filepicker.
156 #### S3 for Filepicker
158 This software uses S3 to store files which are sent to or received
159 from Filepicker. Filepicker will need to know the S3 bucket name, access key,
162 Filepicker users can only make use of an S3 bucket with a paid account. For
163 development purposes, no Filepicker S3 bucket is needed. Skip all references to
164 the Filepicker S3 bucket in the development case.
166 The software will not need to know the S3 credentials for the Filepicker
167 bucket, because the software will upload files to the Filepicker S3 bucket
168 through Filepicker's API and it will link to or download files from the
169 Filepicker S3 bucket through Filepicker's URLs. This will be covered in the
170 [Filepicker section](#filepicker).
172 #### S3 for static files
174 This software uses S3 for hosting static files. The software will need to
175 update static files on the S3 bucket. As such, the software will need the
176 S3 bucket name, access key, and secret key via the environment variables. This
177 is described in subsections below.
179 To support static hosting, `DEFAULT_FILE_STORAGE` should be set to
180 `'storages.backends.s3boto.S3BotoStorage'`, unless there is a compelling reason
183 There are three ways to setup access to the S3 buckets depending upon speed
184 and security. The more secure, the slower it will be to setup.
186 #### insecure S3 access
187 For quick and dirty insecure S3 access, create a single group and a single user
188 with full access to all buckets. Full access to all buckets is insecure!
191 [Amazon IAM group](http://docs.aws.amazon.com/IAM/latest/UserGuide/Using_CreatingAndListingGroups.html)
192 with full access to the S3 bucket. Select the "Amazon S3 Full Accesss" Policy
196 [Amazon IAM user](http://docs.aws.amazon.com/IAM/latest/UserGuide/Using_SettingUpUser.html).
197 Copy the credentials into the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`
198 environment variables. Be sure to write down the access information, as it
199 will only be shown once.
201 #### secure S3 access
202 For secure S3 access, two users will be needed. One with access to the
203 Filepicker bucket and one with access to the static hosting bucket.
205 Note: this might need to be modified to prevent creation and deletion of
209 [Amazon IAM group](http://docs.aws.amazon.com/IAM/latest/UserGuide/Using_CreatingAndListingGroups.html)
210 with full access to the S3 bucket. The quick way is to select the
211 "Amazon S3 Full Accesss" Policy Template and replace `"Resource": "*"` with
212 `"Resource": "arn:aws:s3:::<static_bucket_name>"`.
215 [Amazon IAM user](http://docs.aws.amazon.com/IAM/latest/UserGuide/Using_SettingUpUser.html).
216 Copy the credentials into the `AWS_ACCESS_KEY_ID` and `AWS_SECRET_ACCESS_KEY`
217 environment variables. Be sure to write down the access information, as it
218 will only be shown once.
220 Ensure the created user is a member of the group with access to the S3
223 Repeat the process again, creating a group for the Filepicker bucket and
224 creating a user with access to that group. These credentials will be passed
227 #### somewhat secure S3 access
228 Create two groups as described in the `secure S3 access` section above.
230 Create a single user, save the credentials as described in the
231 `insecure S3 access` section above, and pass the credentials on to Filepicker.
233 Add the single user to both groups.
235 This is less secure because if your web server or Filepicker get compromised
236 (so there are two points for potential failure), the single compromised
237 user has full access to both buckets.
239 ### Amazon Cloudfront CDN
240 [Cloudfront CDN](http://aws.amazon.com/cloudfront/) assists static file hosting.
243 [Amazon's instructions](http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/GettingStarted.html)
244 to host static files out of the appropriate S3 bucket. Note that Django's static
245 file upload process has been modified to mark static files as publicly
248 In the settings for the Cloudfront Distribution, copy the "Domain Name" from
249 General settings and set `CLOUDFRONT_DOMAIN` to it. For example, `abcdefghij.cloudfront.net`.
251 ### Amazon Mechanical Turk
252 Mechanical turk is employed to generate human feedback from uploaded notes.
253 This service is helpful for generating flash cards and quizzes.
255 This service is optional and it might cause unexpected charges when
256 deployed. If the required environment variable is not found,
257 then no errors will occur and no mechanical turk tasks will be created, avoiding any unexpected
260 The `MTURK_HOST` environment variable is almost certainly
261 `"mechanicalturk.amazonaws.com"`.
263 The code will create and publish HITs on your behalf.
266 This software uses [Google Drive](https://developers.google.com/drive/) to
267 convert documents to and from various file formats.
269 A Google Drive service account with access to the Google Drive is required.
270 This may be done with a Google Apps account with administrative privileges, or ask
271 your business sysadmin.
273 Follow [Google's instructions](https://developers.google.com/drive/web/auth/web-server)
274 to create a Google Drive service account. If using Google Apps, it is worth
275 looking at [these instructions](https://developers.google.com/drive/delegation).
277 Convert the p12 file into a Base64 encoded string for the
278 `GOOGLE_SERVICE_KEY_BASE64` environment variable. There are many ways to do
279 this. If Python is available, the
280 [binascii library](https://docs.python.org/2/library/binascii.html#binascii.b2a_base64)
281 makes this very easy:
284 with open('file.p12', 'r') as f:
285 print binascii.b2a_base64(f.read)
287 Copy the contents of `client_secret_*.apps.googleusercontent.com.json` into the
288 `GOOGLE_CLIENT_SECRETS` environment variable.
291 This software uses [Filepicker.io](https://www.inkfilepicker.com/) for uploading
292 files. This requires an account with Filepicker.
294 Filepicker can use an additional third party file hosting site where it may
295 send uploaded files. This project, in production, uses Amazon S3 as the third
296 party. See the Amazon S3 section above for more information.
298 Create a new App with Web SDK and provide the Heroku App URL for the
299 Application's URL. You'll be given an API Key for the App. Paste this into the
300 `FILEPICKER_API_KEY` environment variable.
302 Find the 'App Security' button on the left hand side of the web site. Make sure
303 'Use Security' is enabled. Generate a new secret key. Paste this key into the
304 `FILEPICKER_SECRET` environment variable.
306 If you have an upgraded plan, you can configure Filepicker to have access to
307 your Filepicker S3 bucket. Click 'Amazon S3' on the left hand side menu and
308 supply the credentials for the user with access to the Filepicker S3 bucket.
311 KarmaWorld uses IndexDen to create a searchable index of all the notes in the
312 system. Create an free IndexDen account at
313 [their homepage](http://indexden.com/). You will be given a private URL that
314 accesses your IndexDen account. This URL is visible on your dashboard (you
315 might need to scroll down).
317 Set the `INDEXDEN_PRIVATE_URL` environment variable to your private URL.
319 Set the `INDEXDEN_INDEX` environment variable to the name of the index you want
320 to use for KarmaWorld. The index will be created automatically when KarmaNotes
321 is run if it doesn't already exist. It may be created through the GUI if
326 Twitter is used to post updates about new courses. Access to the Twitter API
327 will be required for this task.
329 If this Twitter feature is desired, the consumer key and secret as well as the
330 access token key and secret are needed by the software.
332 If the required environment variables are not found, then no errors will occur
333 and no tweets will be posted.
336 [create a new Twitter application](https://dev.twitter.com/apps/new).
337 Use your Heroku App URL for the website field. Leave the Callback field blank.
339 Make sure this application has read/write access. Generate an access token. Go
340 to your OAuth settings, and grab the "Consumer key", "Consumer secret",
341 "Access token", and "Access token secret". Paste these, respectively, into the
342 environment variables `TWITTER_CONSUMER_KEY`, `TWITTER_CONSUMER_SECRET`,
343 `TWITTER_ACCESS_TOKEN_KEY`, `TWITTER_ACCESS_TOKEN_SECRET`.
347 ## Configuring foreman
349 KarmaNotes runs on Heroku as a webapp and thus makes use of a Procfie. While
350 not strictly necessary, KarmaWorld can use the same basic Procfile which is
351 convenient and consistent.
353 To use the Procfile locally, we recommend using `foreman`. To install `foreman`
354 and other Heroku tools, install the
355 [Heroku toolbelt](https://toolbelt.heroku.com/).
357 Ensure environment variables are available to `foreman` by copying
358 `.env.example` to `.env` and update those variables as appropriate for your
363 This project uses [pdf2htmlEX](https://github.com/coolwanglu/pdf2htmlEX) as
364 a dependency. pdf2htmlEX is used to convert uploaded PDF notes into HTML. It
365 needs to be installed on the same system that KarmaWorld is running on.
367 ### using their source
369 See their instructions at
370 [https://github.com/coolwanglu/pdf2htmlEX/wiki/Building](https://github.com/coolwanglu/pdf2htmlEX/wiki/Building).
372 Make sure to [patch](https://github.com/FinalsClub/pdf2htmlEX/commit/3c19f6abd8d59d1b58cf254b7160b332b3f5b517)
373 the source code to expose two variables.
377 You can use FinalsClub's [outdated version of pdf2htmlEX](https://github.com/FinalsClub/pdf2htmlEX).
378 See their installation instructions above, but don't worry about patching.
382 You can use [their upstream PPA](https://launchpad.net/~coolwanglu/+archive/ubuntu/pdf2htmlex).
384 apt-add-repository ppa:coolwanglu/pdf2htmlex
386 apt-get install pdf2htmlex
388 Then patch the javascript on your system by running this code in the shell.
390 cat >> `dpkg -L pdf2htmlex | grep pdf2htmlEX.js` <<PDF2HTMLEXHACK
391 Viewer.prototype['rescale'] = Viewer.prototype.rescale;
392 Viewer.prototype['scroll_to'] = Viewer.prototype.scroll_to;
398 1. `source venv/bin/activate`
399 1. `pip install -r requirements.txt`
400 * on Debian systems, some packages are required for pip to succeed:
401 * `apt-get install python-dev libpython-dev python-psycopg2 libmemcached-dev libffi-dev libssl-dev postgresql-server-dev-X.Y`
402 1. `pip install -r requirements-dev.txt`
406 Make sure [External Service Dependencies](#external_service_dependencies) are
407 satisfied. This includes running a local database and RabbitMQ instance as
410 1. configure `.env` as per [instructions](#external_service_dependencies)
411 1. `foreman run python manage.py syncdb --migrate --noinput`
412 1. `foreman run python manage.py createsuperuser`
413 1. `foreman run python manage.py fetch_usde_csv ./schools.csv`
414 1. `foreman run python manage.py import_usde_csv ./schools.csv`
415 1. `foreman run python manage.py sanitize_usde_schools`
417 * `fetch_usde_csv` downloads school records and stores them to `./schools.csv`. This file name
418 and location is arbitrary. As long as the same file is passed into `import_usde_csv` for
419 reading, everything should be fine.
421 * `fetching_usde_csv` requires `7zip` to be installed for processing compressed
422 archives. On Debian-based systems, this entails `apt-get install p7zip-full`
426 Make sure you are inside your virtual environment (`source venv/bin/activate`).
428 If the code has changed or this is the first run, make sure any modified static
429 files get compressed with `foreman run python manage.py compress`. Static files
430 then need to be uploaded correctly with `foreman run python manage.py
433 Run `foreman start`. `foreman` will load the `.env` file and manage running all
434 processes in a way that is similar to that of Heroku. This allows better
435 consistency with local, staging, and production deployments.
437 To run web-only, but no celery or beat, run `foreman start web` to specify
438 strictly the web worker.
440 Press ctrl-C to kill foreman. Foreman will run Django's runserver command.
441 If you wish to have more control over how this is done, you can do
442 `foreman run python manage.py runserver <options>`. For running any other
443 `manage.py` commands, you should also precede them with `foreman run` like just shown.
444 This simply ensures that the environment variables from `.env` are present.
448 KarmaNotes runs on Heroku as a webapp. This section addresses what was done
449 for KarmaNotes so that other implementations of KarmaWorld can be run on
452 Before anything else, download the [Heroku toolbelt](https://toolbelt.heroku.com/).
454 To run KarmaWorld on Heroku, do `heroku create` and `git push heroku master` as typical
455 for a Heroku application. Set your the variable `BUILDPACK_URL` to
456 `https://github.com/FinalsClub/heroku-buildpack-karmanotes` to use a buildpack
457 designed to support KarmaNotes.
459 You will need to import the US Department of Education's list of accredited schools.
460 1. Fetch USDE schools with
461 `heroku run python manage.py fetch_usde_csv ./schools.csv`
462 1. Upload the schools into the database with
463 `heroku run python /manage.py import_usde_csv ./schools.csv`
464 1. Clean up redundant information with
465 `heroku run python /manage.py sanitize_usde_schools`
468 # Django Database management
472 We have setup Django to use
473 [south](http://south.aeracode.org/wiki/QuickStartGuide) for migrations. When
474 changing models, it is important to run
475 `foreman run python manage.py schemamigration` which will create a migration
476 to reflect the model changes into the database. These changes can be pulled
477 into the database with `foreman run python manage.py migrate`.
479 Sometimes the database already has a migration performed on it, but that
480 information wasn't told to south. There are subtleties to the process which
481 require looking at the south docs. As a tip, start by looking at the `--fake`
484 # Assets from Third Parties
486 A number of assets have been added to the repository which come from external
487 sources. It would be difficult to keep a complete list in this README and keep
488 it up to date. Software which originally came from outside parties can
489 generally be found in `karmaworld/assets`.
491 Additionally, all third party Python projects (downloaded and installed with
492 pip) are listed in these files:
495 * `requirements-dev.txt`
499 * KarmaNotes.org is a project of the FinalsClub Foundation with generous funding from the William and Flora Hewlett Foundation
501 * Also thanks to [rdegges](https://github.com/rdegges/django-skel) for the django-skel template