def createNewTerm(self,cid): 
     club = None
     term = None
     latestTerm = None
     
     try:
         club = Club.objects.get(id__exact=cid)
     except:
         return (term,0)
         pass
     
     try:
         latestTerm = Term.objects.filter(club__id__exact=cid).latest('id')
     except:
         pass
     
     if latestTerm:
         try:
             term = Term()
             term.startdate = latestTerm.enddate + 1
             term.enddate = term.startdate + 16416000000 # half a year: 190*24*3600000
             term.club = club
             term.save()
             
             if club.curterm == 0: #if club current term is not set, set it anyway
                 club.curterm = term.id
                 club.save()
         except:
             return (term,0)
     else:
         try:
             term = Term()
             term.startdate = int(time.mktime(datetime.now().timetuple())*1000)
             term.enddate = term.startdate + 16416000000 # half a year: 190*24*3600000
             term.club = club
             term.save()
             
             if club.curterm == 0: #if club current term is not set, set it anyway
                 club.curterm = term.id
                 club.save()
         except:
             return (term,0)
     
     try: #copy the membership
         mss = Membership.objects.filter(club__id__exact=cid,term__id__exact=latestTerm.id,status__exact=True)
         for ms in mss:
             membership = Membership()
             membership.club=club
             membership.term=term
             membership.person=ms.person
             membership.type=ms.type
             membership.access=ms.access
             membership.save()
     except:
         pass
         
     return (term,club.curterm)
person.last_name = "napple"
person.username = "******"
person.displayname = "tiger gu"
person.email = "*****@*****.**"
person.set_password("d9b1d7db4cd6e70935368a1efb10e377")
person.note = "i am here"
person.phone = 13955544587
person.qq = "25364125"
person.weibo = "*****@*****.**"
person.location = "GZ"
person.is_staff = True
person.gender = 1

person.save()
membership = Membership(club=gnclub,term=gnclub_term, person=person,type=MemberType.ActiveMember,access=UserUtil.getExpectedAccess(MemberType.ActiveMember))
membership.save()
print(person.password)

person = Person()
person.tmid = 410000002
person.first_name = "peter"
person.last_name = "pan"
person.note = "i am here"
person.phone = 1395554887
person.username = "******"
person.displayname = "peter pan"
person.email = "*****@*****.**"
person.set_password("d9b1d7db4cd6e70935368a1efb10e377")
person.qq = "25364123235"
person.weibo = "*****@*****.**"
person.location = "SZ"
from gncom.models import Person,Club,Term,Membership
from datetime import datetime

f = open("membership.txt")
line = f.readline()
while line:
    tags = line.split("|")
    print(tags[1] + tags[2])
    club = Club.objects.get(tmid__exact=int(tags[1].lstrip().rstrip()))
    person = Person.objects.get(username=tags[2].lstrip().rstrip())
    ms = Membership()
    ms.club = club
    ms.term_id = club.curterm
    ms.person = person
    ms.type = int(tags[3].lstrip().rstrip())
    ms.access = int(tags[4].lstrip().rstrip())
    ms.status = 1
    ms.updtimestamp = datetime.strptime(tags[6].lstrip().rstrip(),'%Y-%m-%d %H:%M:%S')
    ms.save()
    
    line = f.readline()
f.close()
 def approve_ccr(ccrid):
     ccr = None
     exclub = None
     
     try:
         ccr = CreateClubRequest.objects.get(id__exact=ccrid)
     except:
         pass
     
     try:
         exclub = Club.objects.get(tmid__exact=ccr.tmid)
     except:
         pass
     
     if exclub is None and ccr:
         club = Club()
         club.tmid = ccr.tmid
         club.fullname = ccr.fullname
         club.briefname = ccr.briefname
         club.address = ccr.address
         club.addressd = ccr.addressd
         
         club.mday = ccr.mday
         club.mtime = ccr.mtime
         
         club.conperson = ccr.conperson
         club.conphone = ccr.conphone
         
         club.email = ccr.email
         club.website = ccr.website
         club.qq = ccr.qq
         club.weibo = ccr.weibo
         club.facebook = ccr.facebook
         
         club.cdate = ccr.cdate            
         club.advanced = ccr.advanced            
         club.cstatus = 0 # set as created
         
         club.desc = ccr.desc            
         #process the location/section: district/area/division  country/province              
         club.location = ccr.location
         club.section = ccr.section
         
         club.active = True
         
         club.save()
         print("club: {} {}".format(club.id, club.fullname))            
         
         term = Term()
         term.startdate = int(time.mktime(datetime.now().timetuple())*1000)
         term.enddate = term.startdate + 16416000000 # half a year: 190*24*3600000
         term.club = club
         term.save()
         club.curterm = term.id
         club.save()
         print("term: {} {}".format(term.id, term.startdate))
         
         membership = Membership(club=club,term=term, person=ccr.user,type=MemberType.President,access=UserUtil.getExpectedAccess(MemberType.President))
         membership.save()
         print("membership(Set requester as President)# club:{} term:{} person: {}".format(membership.club.id, membership.club.id,membership.person.username))
         
         #the first club
         if Membership.objects.filter(person=ccr.user).count()==1:
             ccr.user.defclub = club.id
             ccr.user.save()
         
         msgcontent = "Congratulation! Club {} has bee setup with {} as president.".format(club.fullname,membership.person.username)
         MessageUtil.sendClubNty(club.id,club.fullname, club.id, msgcontent)
         
         print(msgcontent)
         CreateClubRequest.objects.filter(id__exact=ccrid).delete()
     else:
         print("This club has been created or the create request is not existed!")
         return False