Exemplo n.º 1
0
def checkin(request):
    error_msg=''
    tagId = ''
    all_checkins = {}
    if request.is_ajax():
        update = []
        #return non-displayed checkins.
        try: 
            lastCall = request.session['last_update']
            
            all_checkins = Checkin.objects.all()
            all_users = User.objects.all()
            for checkin in all_checkins:
                checkin_timestamp = int(checkin.checkin_date.strftime('%s%f'))
                lastCall_timestamp = int(lastCall.strftime('%s%f'))
                if (checkin_timestamp-lastCall_timestamp>0):
#                    print str(checkin_timestamp-lastCall_timestamp)
#                    print 'check in SINCE last refresh -- UPDATE -- '
                    update.append(checkin)
                    try: 
                        u = User.objects.get(rfid = checkin.rfid)
                        update.append(u)
                    except Exception as e:
                        print e
                    #convert to json
            update = serializers.serialize("json", update)
        except Exception as e:
            print e
        request.session['last_update'] = datetime.now()
        return HttpResponse(update)
    else: 
        try:
            dic = request.POST
            try:
                tagId = str(dic['tagId'])
                readerId = str(dic['readerId'])
                name = str(dic['name'])
                credit = int(dic['credit'])
                reader_credit = int(dic['credit_total'])
                if str(dic['is_addition'])=='True':
                    is_addition = True
                else:
                    is_addition = False
                print is_addition
            except Exception as e:
                print e
            if tagId is not '':  
                user, user_credit, error, lostPoints = addOrSubtract(tagId, credit, is_addition)
                group_average, error_msg = getGroupAverage(user)
                if error is not '':
                    print 'Check in with unassigned tag -- have you loaded players for this game? ERROR: '+error
                else:
                    print 'User "'+ user.name +'" checked in, new credit: '+ str(user_credit)
                    
                #get current game id
                current_game = 'UNDEFINED'
                try: 
                    current_game = Extra.objects.get(name='CURRENT_GAME').uneditable_value
                except Exception as e:
                    'ERROR: Extra with name CURRENT_GAME cannot be found: '+str(e)
                               
                c = Checkin (location = readerId, rfid = tagId, name=name, reader_credit = reader_credit, user_credit = user_credit, group_average = group_average, game_name = current_game)
                c.save()   
                
                try:
                    reader = Location.objects.get(reader_id=readerId)
                    reader.credit = reader_credit
                    reader.save()
                except Exception as e:
                    error_msg += str(e)
                    print error_msg
                
                if reader is not None:
                    atRecommendedStation, goToAnyLocation = determineSwipeSideConditions(user, reader)
                    rules = selectAppropriateRules(user, credit, group_average, lostPoints, atRecommendedStation, goToAnyLocation)
                    print rules
                    processRules(rules, user, '')
                
                all_checkins = Checkin.objects.order_by('checkin_date')
                all_checkins.reverse()
                all_users = User.objects.all()    
                                    
                print("|-----------------------|")
                print("|----SWIPE processed----|")
                print("|-READER  -|-   TAGID  -|")
                print("|- %s -|-%s-|" % (readerId, tagId) )
                print("|-----------------------|")
                
            else:
                all_checkins = Checkin.objects.order_by('checkin_date')
                all_checkins = all_checkins.reverse()
                all_users = User.objects.all()
                try:
                    current_game = Extra.objects.get(name='CURRENT_GAME')
                except Exception as e:
                    print e
                    current_game = ''
        except Exception as e:                       
            error_msg += str(e)
            print error_msg
        return render_to_response('cargoapp/checkin.html', {'all_checkins': all_checkins, 'all_users': all_users, 'game':current_game},
                                   context_instance=RequestContext(request))
Exemplo n.º 2
0
def checkin(request):
    error_msg = ""
    values = {}
    tagId = ""
    all_checkins = {}
    if request.is_ajax():
        update = []
        # TODO: return non-displayed checkins.
        try:
            all_checkins = Checkin.objects.all()
            for checkin in all_checkins:
                if checkin.displayed == False:
                    update.append(checkin)
                    checkin.displayed = True
                    checkin.save()
                    # TODO: convert to json
                    update = serializers.serialize("json", update)
        except Exception as e:
            print e
        return HttpResponse(update)
    else:
        try:
            dic = request.POST
            try:
                tagId = str(dic["tagId"])
                readerId = str(dic["readerId"])
            except Exception as e:
                print e
            if tagId is not "":
                #                try:
                #                    r = RFID.objects.create(rfid = tagId)
                #                    r.save()
                #                except Exception as e:
                #                    print e
                #                    r = RFID.objects.get(rfid = tagId)

                c = Checkin(location=readerId, rfid=tagId)
                c.save()
                #                c.rfid.add(r)

                #                all_rfids = RFID.objects.all()
                #                print all_rfids
                #
                #                values['tagId'] = tagId
                #                values['readerId'] = readerId
                print ("|-----------------------|")
                print ("|-------NEW POST--------|")
                print ("|-READER  -|-   TAGID  -|")
                print ("|- %s -|-%s-|" % (readerId, tagId))
                print ("|-----------------------|")
                all_checkins = Checkin.objects.all()
            else:
                # this is a browser request, mark as displayed
                all_checkins = Checkin.objects.all()
                for checkin in all_checkins:
                    checkin.displayed = True
                    checkin.save()
        except Exception as e:
            error_msg = str(e)
            print error_msg
        return render_to_response(
            "cargoapp/checkin.html", {"all_checkins": all_checkins}, context_instance=RequestContext(request)
        )