Skip to content

Instantly share code, notes, and snippets.

@danignibus
Last active August 29, 2015 14:19
Show Gist options
  • Save danignibus/2d57237bbe2966f17193 to your computer and use it in GitHub Desktop.
Save danignibus/2d57237bbe2966f17193 to your computer and use it in GitHub Desktop.
A snippet of my Backend Django work for NeedOne, a mobile Android and iOS application which alerts network when a particular user needs participants for an activity (ie a sports game, workout, meetup, meal).
#From event_api.py:
def create_event(author, place, scope, event_type, start_time, how_many):
newPlace = Place.objects.get(name=place)
event, created = Event.objects.get_or_create(author=author, place=newPlace, scope=scope, event_type=event_type, start_time=start_time, how_many = how_many, defaults={'status':0})
return event
def record_invites(event, invitees):
new_invitees = json.loads(invitees)
for invitee in new_invitees:
user = Profile.objects.get(user__username=invitee)
invitee_count = Profile.objects.filter(user=user)
if invitee_count > 0:
new_invite, created = Invite.objects.get_or_create(event=event, invitee=user)
else:
raise ValidationError("Invitee " + str(invitee) + " does not exist")
def record_rsvp(event, user_profile):
user_profile_id = user_profile.id
profile_object_user = Profile.objects.get(id=user_profile_id)
event_id = Event.objects.get(id=event)
added_RSVP, created = RSVP.objects.get_or_create(event = event_id, user_profile = profile_object_user)
return added_RSVP
#from event_views.py
class CreateEvent(View):
def post(self, request):
try:
place = request.POST.get('place')
scope = models.PUBLIC
event_type = request.POST.get('event_type')
start_time = request.POST.get('start_time')
author = request.user_profile
start_time = datetime.datetime.fromtimestamp(int(float(start_time)))
how_many = request.POST.get('how_many')
invitees = request.POST.get('invitees')
validate_input(author, scope, event_type, start_time)
new_event = create_event(author, place, scope, event_type, start_time, how_many)
return JsonResponse({'success': True, 'message': "Event created"})
except ValidationError as e:
return JsonResponse({'success': False, 'message': str(e)})
class SubmitRSVP(View):
def post(self, request):
try:
user = request.user_profile
event = request.POST.get('event')
rsvp = record_rsvp(event, user)
return JsonResponse({'success': True, 'message': "RSVP submitted"})
except ValidationError as f:
return JsonResponse({'success': False, 'message': str(f)})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment