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)
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()