bgneal@412: """ bgneal@412: import_old_downloads.py - For importing downloads from SG101 1.0 as csv files. bgneal@412: """ bgneal@412: from __future__ import with_statement bgneal@412: import csv bgneal@412: import datetime bgneal@412: bgneal@412: from django.core.management.base import LabelCommand, CommandError bgneal@412: from django.contrib.auth.models import User bgneal@412: bgneal@412: from downloads.models import Download, Category bgneal@412: from legacy.html2md import MarkdownWriter bgneal@412: bgneal@412: bgneal@412: # downloads with these lid's will be excluded bgneal@412: EXCLUDE_SET = set([1, 2, 3, 4, 277]) bgneal@412: bgneal@412: # Mapping of old category IDs to new; None means we don't plan on importing bgneal@412: CAT_MAP = { bgneal@412: 4: None, # Misc bgneal@412: 3: None, # Music bgneal@412: 1: None, # Demos bgneal@412: 6: 2, # Gear Samples bgneal@412: 8: 4, # Ringtones bgneal@412: 9: 8, # Tablature bgneal@412: 10: 6, # Interviews bgneal@412: 11: None, # 2008 MP3 Comp bgneal@412: 12: 1, # Backing Tracks bgneal@412: 13: None, # 2009 MP3 Comp bgneal@412: } bgneal@412: bgneal@412: SG101_PREFIX = 'http://surfguitar101.com/' bgneal@412: bgneal@412: bgneal@412: class Command(LabelCommand): bgneal@412: args = '' bgneal@412: help = 'Imports downloads from the old database in CSV format' bgneal@412: md_writer = MarkdownWriter() bgneal@412: bgneal@412: def handle_label(self, filename, **options): bgneal@412: """ bgneal@412: Process each line in the CSV file given by filename by bgneal@412: creating a new object and saving it to the database. bgneal@412: bgneal@412: """ bgneal@412: self.cats = {} bgneal@412: try: bgneal@412: self.default_user = User.objects.get(pk=2) bgneal@412: except User.DoesNotExist: bgneal@412: raise CommandError("Need a default user with pk=2") bgneal@412: bgneal@412: try: bgneal@412: with open(filename, "rb") as f: bgneal@412: self.reader = csv.DictReader(f) bgneal@412: try: bgneal@412: for row in self.reader: bgneal@412: self.process_row(row) bgneal@412: except csv.Error, e: bgneal@412: raise CommandError("CSV error: %s %s %s" % ( bgneal@412: filename, self.reader.line_num, e)) bgneal@412: bgneal@412: except IOError: bgneal@412: raise CommandError("Could not open file: %s" % filename) bgneal@412: bgneal@412: def get_category(self, old_cat_id): bgneal@412: """ bgneal@412: Return the Category object for the row. bgneal@412: bgneal@412: """ bgneal@412: cat_id = CAT_MAP[old_cat_id] bgneal@412: if cat_id not in self.cats: bgneal@412: try: bgneal@412: cat = Category.objects.get(pk=cat_id) bgneal@412: except Category.DoesNotExist: bgneal@412: raise CommandError("Category does not exist: %s on line %s" % ( bgneal@412: cat_id, self.reader.line_num)) bgneal@412: else: bgneal@412: self.cats[cat_id] = cat bgneal@412: return self.cats[cat_id] bgneal@412: bgneal@412: def get_user(self, username): bgneal@412: """ bgneal@412: Return the user object for the given username. bgneal@412: If the user cannot be found, self.default_user is returned. bgneal@412: bgneal@412: """ bgneal@412: try: bgneal@412: return User.objects.get(username=username) bgneal@412: except User.DoesNotExist: bgneal@412: return self.default_user bgneal@412: bgneal@412: def process_row(self, row): bgneal@412: """ bgneal@412: Process one row from the CSV file: create an object for the row bgneal@412: and save it in the database. bgneal@412: bgneal@412: """ bgneal@412: lid = int(row['lid']) bgneal@412: if lid in EXCLUDE_SET: bgneal@412: return # skip bgneal@412: bgneal@412: cat = int(row['cid']) bgneal@412: if CAT_MAP.get(cat) is None: bgneal@412: return # skip this one; we aren't carrying these over bgneal@412: bgneal@412: dl_date = datetime.datetime.strptime(row['date'], "%Y-%m-%d %H:%M:%S") bgneal@412: old_url = row['url'].decode('latin-1') bgneal@412: if old_url.startswith(SG101_PREFIX): bgneal@412: old_url = old_url[len(SG101_PREFIX):] bgneal@412: if old_url.startswith('dls/'): bgneal@412: old_url = old_url[4:] bgneal@412: new_url = u'downloads/1.0/%s' % old_url bgneal@412: bgneal@412: dl = Download( bgneal@412: id=lid, bgneal@412: title=row['title'].decode('latin-1'), bgneal@412: category=self.get_category(cat), bgneal@412: description=self.to_markdown(row['description'].decode('latin-1')), bgneal@412: file=new_url, bgneal@412: user=self.get_user(row['submitter']), bgneal@412: date_added=dl_date, bgneal@412: ip_address='127.0.0.1', # not available bgneal@412: hits=int(row['hits']), bgneal@412: average_score=float(row['downloadratingsummary']) / 2.0, bgneal@412: total_votes=int(row['totalvotes']), bgneal@412: is_public=True) bgneal@412: dl.save() bgneal@412: #print "cp %s %s" % (old_url, '/home/var/django-sites/sg101/sg101-trunk/media/' + new_url) bgneal@412: bgneal@412: def to_markdown(self, s): bgneal@412: self.md_writer.reset() bgneal@412: self.md_writer.feed(s) bgneal@412: return self.md_writer.markdown()