from django.core.management.base import BaseCommand, CommandError
from room_manage.models import NickHistory, NickSummary

class Command(BaseCommand):
    help = 'Builds the NickSummary table from NickHistory'

    def add_arguments(self, parser):
        parser.add_argument('subcommand', nargs='?')
        

    def handle(self, *args, **options):
        subcommand = options['subcommand']
        if subcommand:
            pass
            self.stdout.write('Nothing done')
        else:
            for rec in NickHistory.objects.all():
                network = rec.network
                nick = rec.nick
                host_mask = rec.host_mask
                time_seen = rec.time_seen
                try:
                    obj = NickSummary.objects.get(
                        network = network,
                        nick = nick,
                        host_mask = host_mask)
                    
                    if time_seen > obj.last_seen:
                        obj.last_seen = time_seen
                        obj.save()
                except NickSummary.DoesNotExist:
                    obj = NickSummary(network = network,
                            nick = nick,
                            host_mask = host_mask,
                            last_seen = time_seen)
                    obj.save()

            self.stdout.write('Successfully finished addung data to NickSummary table.')

@@ -114,16 +114,50 @@
 kSummary
+%0Afrom django.db import transaction
 %0A%0Aclass 
@@ -537,50 +537,555 @@
     
-for rec in NickHistory.objects.all():%0A    
+self.create_summary()%0A            self.stdout.write('Successfully finished adding data to NickSummary table.')%0A%0A    @transaction.atomic%0A    def create_summary(self):%0A        last_rec = NickSummary.objects.order_by('last_seen').last()%0A        if last_rec:%0A            last_rec_dt = last_rec.last_seen%0A            self.stdout.write(str(last_rec_dt))%0A            qs = NickHistory.objects.filter(time_seen__gte = last_rec_dt).order_by('time_seen')%0A        else:%0A            qs = NickHistory.objects.all().order_by('time_seen')%0A%0A        for rec in qs:%0A
     
@@ -1122,28 +1122,24 @@
             
-    
 nick = rec.n
@@ -1146,36 +1146,32 @@
 ick%0A            
- 
-   
 host_mask = rec.
@@ -1180,20 +1180,16 @@
 st_mask%0A
-    
         
@@ -1234,21 +1234,13 @@
     
-   
- 
 try:%0A
-    
     
@@ -1294,36 +1294,32 @@
                 
-    
 network = networ
@@ -1333,36 +1333,32 @@
                 
-  
-  
 nick = nick,%0A   
@@ -1346,36 +1346,32 @@
    nick = nick,%0A
-    
                 
@@ -1413,20 +1413,16 @@
         
-    
 %0A       
@@ -1430,20 +1430,16 @@
         
-    
 if time_
@@ -1476,28 +1476,24 @@
             
-    
 obj.last_see
@@ -1518,36 +1518,32 @@
                 
-   
- 
 obj.save()%0A     
@@ -1537,20 +1537,16 @@
 .save()%0A
-    
         
@@ -1590,36 +1590,32 @@
                 
-    
 obj = NickSummar
@@ -1651,36 +1651,32 @@
                 
- 
-   
 nick = nick,%0A   
@@ -1664,36 +1664,32 @@
    nick = nick,%0A
-    
                 
@@ -1719,20 +1719,16 @@
 t_mask,%0A
-    
         
@@ -1774,36 +1774,32 @@
                 
-    
 obj.save()%0A%0A    
@@ -1798,93 +1798,4 @@
 ()%0A%0A
-            self.stdout.write('Successfully finished addung data to NickSummary table.')%0A

Optimise nick summary table build