scripts/generate_tasks_from_csv.py
author Madhusudan.C.S <madhusudancs@gmail.com>
Thu, 20 Jan 2011 17:46:49 +0530
changeset 501 b84d6a1d4603
parent 490 5b15b4080c33
permissions -rwxr-xr-x
Change all the queries to Textbook model to Tasks. Since Textbook model is deprecated now, implemented the changes in the views to query respected queries and make corresponding changes in the views.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
484
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     1
#! /usr/bin/python
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     2
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     3
"""Module to fill database with the tasks supplied in CSV.
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     4
This module takes the directory containing the csv files as
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     5
argument and creates task for the data in each CSV file in
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     6
this directory.
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     7
"""
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     8
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
     9
__authors__ = [
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    10
    '"Madhusudan.C.S" <madhusudancs@gmail.com>'
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    11
  ]
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    12
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    13
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    14
import csv
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    15
import datetime
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    16
import os
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    17
import sys
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    18
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    19
from django.contrib.auth.models import User
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    20
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    21
from pytask.taskapp.models import Task
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    22
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    23
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    24
STATIC_DATA = {
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    25
  'created_by': User.objects.get(pk=1),
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    26
  'creation_datetime': datetime.datetime.now()
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    27
  }
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    28
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    29
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    30
def get_textbooks_from_csv(directory, file_name):
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    31
    """Return the list of the titles of tasks.
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    32
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    33
    Args:
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    34
        file: name of the CSV file from which tasks must be fetched.
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    35
    """
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    36
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    37
    file_absolute_name = os.path.join(directory, file_name)
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    38
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    39
    csv_obj = csv.reader(open(file_absolute_name))
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    40
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    41
    # Nifty trick to separate the file extension out and get the
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    42
    # remaining part of the filename to use this as the tag for
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    43
    # branches/departments
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    44
    branch_name = os.extsep.join(file_name.split(os.extsep)[:-1])
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    45
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    46
    textbooks = []
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    47
    for line in csv_obj:
490
5b15b4080c33 Approve the textbooks directly.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 484
diff changeset
    48
        if len(line) == 2 and line[0]:
484
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    49
            sep = ' by '
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    50
        else:
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    51
            sep = ''
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    52
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    53
        textbooks.append({
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    54
          'title': sep.join(line),
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    55
          'desc': '(To be filled in by the Coordinator or the T/A.)',
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    56
          'tags_field': ', '. join(['Textbook', branch_name, line[1]]),
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    57
          'pynts': 10,
490
5b15b4080c33 Approve the textbooks directly.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 484
diff changeset
    58
          'status': 'Open',
484
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    59
          })
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    60
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    61
    return textbooks
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    62
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    63
def seed_db(data):
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    64
    """Seeds the database when the data is passed as the argument
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    65
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    66
    Args:
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    67
        data: A dictionary containing the data to be seeded into the
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    68
              task model.
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    69
    """
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    70
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    71
    for task in data:
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    72
        task.update(STATIC_DATA)
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    73
        task_obj = Task(**task)
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    74
        task_obj.save()
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    75
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    76
def main():
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    77
    """Just a wrapper function to make call the functions that perform
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    78
    the action.
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    79
    """
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    80
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    81
    for dir in sys.argv[1:]:
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    82
        args = list(os.walk(dir))
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    83
        files = args[0][2]
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    84
        for file_name in files:
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    85
            tasks = get_textbooks_from_csv(args[0][0], file_name)
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    86
            seed_db(tasks)
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    87
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    88
if __name__ == '__main__':
d2a4fcbe2fae A python script to load the data of tasks from CSV to database.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff changeset
    89
    main()