Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Oops; misread the spec as tracking hits on individual shurls, not dom…

…ains. Time to rearchitect...
  • Loading branch information...
commit e4a623867744d5cd67f8c2037bbab67ab050c71d 1 parent 392c83f
Karen Rustad authored
70 shortcake/migrations/0005_auto__add_domain__add_field_shurl_domain__del_field_monthlog_shurl__ad.py
... ... @@ -0,0 +1,70 @@
  1 +# encoding: utf-8
  2 +import datetime
  3 +from south.db import db
  4 +from south.v2 import SchemaMigration
  5 +from django.db import models
  6 +
  7 +class Migration(SchemaMigration):
  8 +
  9 + def forwards(self, orm):
  10 +
  11 + # Adding model 'Domain'
  12 + db.create_table('shortcake_domain', (
  13 + ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)),
  14 + ('domain', self.gf('django.db.models.fields.URLField')(unique=True, max_length=200)),
  15 + ('access_count', self.gf('django.db.models.fields.IntegerField')(default=0)),
  16 + ))
  17 + db.send_create_signal('shortcake', ['Domain'])
  18 +
  19 + # Adding field 'Shurl.domain'
  20 + db.add_column('shortcake_shurl', 'domain', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['shortcake.Domain'], null=True, blank=True), keep_default=False)
  21 +
  22 + # Deleting field 'MonthLog.shurl'
  23 + db.delete_column('shortcake_monthlog', 'shurl_id')
  24 +
  25 + # Adding field 'MonthLog.domain'
  26 + db.add_column('shortcake_monthlog', 'domain', self.gf('django.db.models.fields.related.ForeignKey')(default=1, to=orm['shortcake.Domain']), keep_default=False)
  27 +
  28 +
  29 + def backwards(self, orm):
  30 +
  31 + # Deleting model 'Domain'
  32 + db.delete_table('shortcake_domain')
  33 +
  34 + # Deleting field 'Shurl.domain'
  35 + db.delete_column('shortcake_shurl', 'domain_id')
  36 +
  37 + # Adding field 'MonthLog.shurl'
  38 + db.add_column('shortcake_monthlog', 'shurl', self.gf('django.db.models.fields.related.ForeignKey')(default='http://google.com', to=orm['shortcake.Shurl']), keep_default=False)
  39 +
  40 + # Deleting field 'MonthLog.domain'
  41 + db.delete_column('shortcake_monthlog', 'domain_id')
  42 +
  43 +
  44 + models = {
  45 + 'shortcake.domain': {
  46 + 'Meta': {'object_name': 'Domain'},
  47 + 'access_count': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
  48 + 'domain': ('django.db.models.fields.URLField', [], {'unique': 'True', 'max_length': '200'}),
  49 + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'})
  50 + },
  51 + 'shortcake.monthlog': {
  52 + 'Meta': {'ordering': "['month']", 'object_name': 'MonthLog'},
  53 + 'access_count': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
  54 + 'creation_date': ('django.db.models.fields.DateField', [], {'auto_now_add': 'True', 'blank': 'True'}),
  55 + 'domain': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['shortcake.Domain']"}),
  56 + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
  57 + 'month': ('django.db.models.fields.DateField', [], {'default': 'datetime.date(2012, 4, 1)'})
  58 + },
  59 + 'shortcake.shurl': {
  60 + 'Meta': {'ordering': "['-creation_time']", 'object_name': 'Shurl'},
  61 + 'access_count': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
  62 + 'creation_time': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
  63 + 'domain': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['shortcake.Domain']", 'null': 'True', 'blank': 'True'}),
  64 + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
  65 + 'short_suffix': ('django.db.models.fields.CharField', [], {'max_length': '20', 'unique': 'True', 'null': 'True', 'blank': 'True'}),
  66 + 'url': ('django.db.models.fields.URLField', [], {'unique': 'True', 'max_length': '200'})
  67 + }
  68 + }
  69 +
  70 + complete_apps = ['shortcake']
59 shortcake/models.py
@@ -7,6 +7,42 @@
7 7
8 8 def first_of_the_month(date=datetime.date.today()):
9 9 return datetime.date(date.year, date.month, 1)
  10 +
  11 +class Domain(models.Model):
  12 + domain = models.URLField(unique=True)
  13 + access_count = models.IntegerField(default=0)
  14 +
  15 + def get_or_create_log(self,month):
  16 + ''' returns a MonthLog object, either old or freshly created, for the given first-of-the-month date '''
  17 + try:
  18 + log = self.monthlog_set.get(month=month)
  19 + except:
  20 + log = MonthLog(domain=self,month=month)
  21 + log.save()
  22 + return log
  23 +
  24 + @classmethod
  25 + def extract_domain_from_url(url):
  26 + ''' Takes a url, returns the url's domain '''
  27 + # take apart the domain
  28 + domain_parts = url.split('.')
  29 + # get the tld out of the last period-separated chunk
  30 + tail_parts = domain_parts[-1].split('/',1)
  31 + tld = tail_parts[0]
  32 + domain_parts.append(tld)
  33 + # put the string back together
  34 + return string.join(domain_parts, '.')
  35 +
  36 + @classmethod
  37 + def get_or_create(url):
  38 + '''Returns a Domain object, either old or freshly created, based on a url'''
  39 + domain = Domain.extract_domain_from_url(url)
  40 + try:
  41 + d = Domain.objects.get(domain=domain)
  42 + except:
  43 + d = Domain(domain=domain)
  44 + d.save()
  45 + return d
10 46
11 47 class Shurl(models.Model):
12 48 '''Model for a shortened URL.
@@ -15,6 +51,9 @@ class Shurl(models.Model):
15 51 short_suffix = models.CharField(max_length=20,unique=True,**optional)
16 52 access_count = models.IntegerField(default=0)
17 53 creation_time = models.DateTimeField(auto_now_add=True)
  54 + # this really shouldn't be optional
  55 + # for now, it gets filled in for sure the first time get_url is called
  56 + domain = models.ForeignKey(Domain, **optional)
18 57
19 58 def __unicode__(self):
20 59 return self.url
@@ -56,11 +95,19 @@ def get_url(self):
56 95 You probably want to use this method instead of accessing the url directly!'''
57 96 self.access_count += 1
58 97 self.save()
59   - print "Incremented shurl access count"
60   - log = self.monthlog_set.get(month=first_of_the_month())
  98 +
  99 + try:
  100 + d = self.domain
  101 + except:
  102 + self.domain = Domain.get_or_create(self.url)
  103 + self.save()
  104 + d = self.domain
  105 + d.access_count += 1
  106 + d.save()
  107 +
  108 + log = d.get_or_create_log(first_of_the_month())
61 109 log.access_count += 1
62 110 log.save()
63   - print "Incremented log access count for month " + str(log.month)
64 111 return self.url
65 112
66 113 class ShurlForm(ModelForm):
@@ -69,14 +116,14 @@ class Meta:
69 116 fields = ('url',)
70 117
71 118 class MonthLog(models.Model):
72   - '''Model for a log of accesses for a given shortened url in a given month.'''
73   - shurl = models.ForeignKey(Shurl)
  119 + '''Model for a log of accesses for a given domain in a given month.'''
  120 + domain = models.ForeignKey(Domain)
74 121 month = models.DateField(default=first_of_the_month())
75 122 creation_date = models.DateField(auto_now_add=True)
76 123 access_count = models.IntegerField(default=0)
77 124
78 125 def __unicode__(self):
79   - return 'Log for ' + str(self.month) + ' of ' + self.shurl.short_suffix
  126 + return 'Log for ' + str(self.month) + ' of ' + self.domain
80 127
81 128 class Meta:
82 129 ordering = ['month']

0 comments on commit e4a6238

Please sign in to comment.
Something went wrong with that request. Please try again.