from optparse import make_option
from pprint import pprint

from django.core.management.base import NoArgsCommand
from django.template.defaultfilters import slugify
from django.conf import settings

from mzalendo.helpers import geocode
from mzalendo.core import models


class Command(NoArgsCommand):
    help = 'Deduplicate the position entries'

    fields_to_compare = ('person', 'organisation', 'place', 'title', 'subtitle', 'start_date', 'end_date' )

    def handle_noargs(self, **options):
        """Go through all the positions and report any that are duplicates"""


        # get all positions ordered such that duplicates are sequential
        position_qs = models.Position.objects.all().order_by( *self.fields_to_compare )
        
        previous_position = None
        
        for position in position_qs:
            # print "looking at %s" % position
            if previous_position:
                self.compare_positions( previous_position, position )

            previous_position = position
            
    def compare_positions(self, previous, current):
        """Compare two positions, deleting one if they are the same"""

        # check the fields - if any are different return
        for key in self.fields_to_compare:
            if getattr(previous, key) != getattr(current, key):
                return None
        
        print "## Potential Matches #################################"
        self.pprint_position( previous )
        self.pprint_position( current )
        
    def pprint_position(self, position):
        print "%s" % position
        print "  %s to %s" % ( position.start_date, position.end_date)

@@ -93,22 +93,20 @@
  import 
-NoArgs
+Base
 Command%0A
@@ -280,14 +280,12 @@
 and(
-NoArgs
+Base
 Comm
@@ -345,73 +345,351 @@
     
-fields_to_compare = ('person', 'organisation', 'place', 'title', 
+option_list = BaseCommand.option_list + (%0A        make_option(%0A            '--delete',%0A            action='store_true',%0A            dest='delete',%0A            default=False,%0A            help='Delete found duplicates'),%0A        )%0A%0A    fields_to_order   = ('person', 'organisation', 'place', 'title' )%0A    fields_to_compare = fields_to_order + (
 'sub
@@ -720,16 +720,24 @@
 nd_date'
+, 'note'
  )%0A%0A    
@@ -750,15 +750,8 @@
 ndle
-_noargs
 (sel
@@ -811,20 +811,20 @@
 s and re
-port
+move
  any tha
@@ -1004,16 +1004,39 @@
 ompare )
+.filter(person__id=189)
 %0A       
@@ -1053,25 +1053,16 @@
 previous
-_position
  = None%0A
@@ -1082,24 +1082,23 @@
     for 
-position
+current
  in posi
@@ -1106,16 +1106,42 @@
 ion_qs:%0A
+            %0A            %0A
         
@@ -1206,18 +1206,139 @@
 ious
-_position:
+ and self.are_positions_equal( previous, current ):%0A%0A                print %22###### Found Matches #################################%22
 %0A   
@@ -1355,23 +1355,22 @@
    self.
-compare
+pprint
 _positio
@@ -1370,17 +1370,16 @@
 position
-s
 ( previo
@@ -1384,31 +1384,59 @@
 ious
-_position, position
+ )%0A                self.pprint_position( current
  )%0A
-%0A
     
@@ -1447,36 +1447,142 @@
     
-previous_position = position
+    print%0A                %0A                if options%5B'delete'%5D:%0A                    previous.delete()%0A%0A            previous = current
 %0A   
@@ -1599,20 +1599,16 @@
     def 
-comp
 are_posi
@@ -1612,16 +1612,22 @@
 ositions
+_equal
 (self, p
@@ -1817,16 +1817,17 @@
 ompare:%0A
+%0A
         
@@ -1834,36 +1834,149 @@
     
-if getattr(previous, key) !=
+# get the values. Use repr so that future dates compare as equal%0A            p_val = repr( getattr(previous, key) )%0A            c_val = repr(
  get
@@ -1993,22 +1993,21 @@
 nt, key)
-:%0A    
+  )%0A%0A
         
@@ -2014,19 +2014,27 @@
     
-return None
+if  p_val != c_val:
 %0A   
@@ -2038,25 +2038,24 @@
         
-%0A
         
 print %22#
@@ -2050,184 +2050,134 @@
     
-print %22## Potential Matches #################################%22%0A        self.pprint_position( previous )%0A        self.pprint_position( current )%0A        %0A    def pprint_
+return False%0A%0A        return True%0A%0A        %0A    def pprint_position(self, position):%0A        print %22%25u: %25s%22       %25 ( 
 position
 (sel
@@ -2164,37 +2164,35 @@
     %25 ( position
-(self
+.id
 , position):%0A   
@@ -2177,34 +2177,34 @@
 ion.id, position
+ 
 )
-:
 %0A        print %22
@@ -2203,30 +2203,53 @@
  print %22
-%25s%22 %25 position
+    %25s%22       %25 ( position.subtitle )
 %0A       
@@ -2256,16 +2256,18 @@
  print %22
+  
   %25s to 

Fix issue with future != future