def createZip(album): songs = Song.objects.filter(album_id=album._id) set_user_for_sharding(songs, album.musician_id) album_id = album._id musician_id = album.musician_id musician_dir = os.path.join(settings.ZIP_ROOT, musician_id) if not os.path.isdir(musician_dir): # Create direcotry os.mkdir(musician_dir) pass album_dir = os.path.join(musician_dir, album_id) if not os.path.isdir(album_dir): # Create direcotry os.mkdir(album_dir) pass version_hash = hashlib.sha256() for song in songs: version_hash.update(song._id) pass filename = version_hash.hexdigest() + '.zip' full_filename = os.path.join(album_dir, filename) # If zip file already exists exit if (os.path.isfile(full_filename)): return version_hash.hexdigest() # else create zip else: zip_file = ZipFile(full_filename, "w") for song in songs: song_loc = os.path.join(settings.MEDIA_ROOT, str(song.media)) zip_file.write(song_loc, song_loc.split('/')[-1]) pass zip_file.close() return version_hash.hexdigest()
def item(request, user_id, item_id): '''List of recent posts by people I follow''' user = get_user_from_cache(user_id) if user is None: return flashHomeMessage(request, 'Sorry, we could\'t find a user by that specification') # query items item = get_item_from_cache(item_id, user_id) # pass in the user_id as a backup to get the item from the db if it does not exist if item is None: # this means the cache doesn't have it and (if we passed in the user_id) the db doesn't have it return flashHomeMessage(request, 'Sorry, we could\'t find an item by that specification') if item.is_public == False: if request.user.is_authenticated(): set_user_for_sharding(user_query, user_id) logged_in_user = user_query.get(user_id=request.user.id) if item.owner != logged_in_user: return flashHomeMessage(request, 'This is not your item so you unfortunately can\'t view it') else: return flashHomeMessage(request, 'Please log in to view this item') context = { 'user': user, 'item': item, } return render(request, 'my_qrcode/item.html', context)
def item(request, user_id, item_id): '''List of recent posts by people I follow''' user = get_user_from_cache(user_id) if user is None: return flashHomeMessage( request, 'Sorry, we could\'t find a user by that specification') # query items item = get_item_from_cache( item_id, user_id ) # pass in the user_id as a backup to get the item from the db if it does not exist if item is None: # this means the cache doesn't have it and (if we passed in the user_id) the db doesn't have it return flashHomeMessage( request, 'Sorry, we could\'t find an item by that specification') if item.is_public == False: if request.user.is_authenticated(): set_user_for_sharding(user_query, user_id) logged_in_user = user_query.get(user_id=request.user.id) if item.owner != logged_in_user: return flashHomeMessage( request, 'This is not your item so you unfortunately can\'t view it' ) else: return flashHomeMessage(request, 'Please log in to view this item') context = { 'user': user, 'item': item, } return render(request, 'my_qrcode/item.html', context)
def clean_name(self): name = self.cleaned_data['name'] device = Device.objects.filter(user_id=self.user_id, name=name) set_user_for_sharding(device, self.user_id) if device.exists(): raise forms.ValidationError( "You already have a device with the same name!") return name
def get_object(self): device_id = self.kwargs['pk'] devices = Device.objects.filter(user_id=self.request.user.id, id=device_id) set_user_for_sharding(devices, self.request.user.id) if len(devices) == 1: return devices[0] else: raise Http404
def analytics(request): all_fulfilled_purchases = [] for shard in range(0, NUM_PHYSICAL_SHARDS): purchases = Purchase.objects.filter(fulfilled=True) set_user_for_sharding(purchases, str(shard)) all_fulfilled_purchases = all_fulfilled_purchases + [ p for p in purchases ] return render(request, 'web/analytics.html', {'purchases': all_fulfilled_purchases})
def findId(Model, length, user_id, is_purchase_id): _id = '' if is_purchase_id: _id = uuid.uuid4().hex[0:length] + user_id else: _id = uuid.uuid4().hex[0:length] model_query = Model.objects set_user_for_sharding(model_query, user_id) while (model_query.filter(_id=_id).exists()): _id = uuid.uuid4().hex[0:length] pass return _id
def home(request): user = request.user first_name = user.first_name devices = Device.objects.filter(user_id=user.id) set_user_for_sharding(devices, user.id) num_devices = len(devices) context = { 'name': first_name, 'devices': devices, 'num_devices': num_devices, 'page_title': 'Home', } return render(request, 'micro/home.html', context)
def generate_item_id(user_id): # Check all items in all shards or keep last item id # so that we can increment it, unless that doesn't matter shards_to_query = get_all_shards() all_items = [] for shard in shards_to_query: item_list = Item.objects.all().order_by('-pk') set_user_for_sharding(item_list, int(shard)) # The list comprehension actually invokes the db query in the QuerySet. if len(item_list) > 0: all_items = all_items + [item_list[0]] # get the item with the highest pk # we just got the highest item pk from each shard last_item = sorted(all_items, key=lambda x: x.pk, reverse=True) # sort the items if len(last_item) > 0: return last_item[0].item_id + 1 return 1
def get_item_from_cache(item_id, user_id = None): cache = caches['items'] single_item_cache_key = 'qr_item_' + str(item_id) # have single item cache as well so that item = cache.get(single_item_cache_key) if item is None and user_id is not None: try: item_query = Item.objects set_user_for_sharding(item_query, user_id) item = item_query.get(item_id=item_id) cache.set(single_item_cache_key, item) except Item.DoesNotExist: #return flashHomeMessage(request, 'Sorry, we could\'t find an item by that specification') return None # both the cache and the db were tried else: print 'Item is in cache' return item
def get_item_from_cache(item_id, user_id=None): cache = caches['items'] single_item_cache_key = 'qr_item_' + str( item_id) # have single item cache as well so that item = cache.get(single_item_cache_key) if item is None and user_id is not None: try: item_query = Item.objects set_user_for_sharding(item_query, user_id) item = item_query.get(item_id=item_id) cache.set(single_item_cache_key, item) except Item.DoesNotExist: #return flashHomeMessage(request, 'Sorry, we could\'t find an item by that specification') return None # both the cache and the db were tried else: print 'Item is in cache' return item
def generate_item_id(user_id): # Check all items in all shards or keep last item id # so that we can increment it, unless that doesn't matter shards_to_query = get_all_shards() all_items = [] for shard in shards_to_query: item_list = Item.objects.all().order_by('-pk') set_user_for_sharding(item_list, int(shard)) # The list comprehension actually invokes the db query in the QuerySet. if len(item_list) > 0: all_items = all_items + [item_list[0] ] # get the item with the highest pk # we just got the highest item pk from each shard last_item = sorted(all_items, key=lambda x: x.pk, reverse=True) # sort the items if len(last_item) > 0: return last_item[0].item_id + 1 return 1
def get_user_from_cache(user_id): cache = caches['users'] user_cach_key = 'qr_user_' + str(user_id) user = cache.get(user_cach_key) if user is None: # get user from shard try: # query user and get him from the shard user_query = FinderUser.objects set_user_for_sharding(user_query, user_id) user = user_query.get(user_id=user_id) cache.set(user_cach_key, user) except FinderUser.DoesNotExist: #return flashHomeMessage(request, 'Sorry, we could\'t find a user by that specification') return None # user was not in cach or in the db else: print 'User is in the cache' return user
def genqr(request): if request.method == 'POST': musician_id = request.user.profile.musician_id purchase_id = findId(Purchase, 16, musician_id, True) album_query = Album.objects set_user_for_sharding(album_query, musician_id) album = album_query.get(_id=request.POST['album_id']) longitude = request.POST['longitude'] latitude = request.POST['latitude'] version_hash = createZip(album) p = Purchase(musician_id=musician_id, _id=purchase_id, album_id=album, longitude=longitude, latitude=latitude, version_hash=version_hash) p.save() return redirect('/streeTunes/qr/{pid}'.format(pid=purchase_id)) else: return HttpResponseNotFound()
def stream(request, user_id): # See if to present a 'follow' button form = None if request.user.is_authenticated() and request.user.id != int(user_id): try: following_query = Following.objects set_user_for_sharding(following_query, request.user.id) f = following_query.get(user_id=request.user.id, followee_id=user_id) except Following.DoesNotExist: form = FollowingForm user_query = Profile.objects set_user_for_sharding(user_query, user_id) user = user_query.get(pk=user_id) post_list = Post.objects.filter(user_id=user_id).order_by('-pub_date') set_user_for_sharding(post_list, user_id) paginator = Paginator(post_list, 10) page = request.GET.get('page') try: posts = paginator.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. posts = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. posts = paginator.page(paginator.num_pages) context = { 'posts' : posts, 'stream_user' : user, 'form' : form, } return render(request, 'micro/stream.html', context)
def home(request): '''List of recent posts by people I follow''' my_posts = Post.objects.filter(user_id=request.user.id).order_by('-pub_date') set_user_for_sharding(my_posts, request.user.id) if my_posts: my_post = my_posts[0] else: my_post = None following_list = Following.objects.filter(user_id=request.user.id) set_user_for_sharding(following_list, request.user.id) follows = [o.followee_id for o in following_list] # We need to query every shard where there are users who this user follows # We break the users into shards, then for each shard we annotate it with # a hint for that shard, issue a query and at the end join all results. # Note that this is not very efficient if users are spread accorss many # shards. This is where Fan-out could help improve performance. shards_to_query = bucket_users_into_shards(follows) all_posts = [] for shard, user_ids in shards_to_query.iteritems(): post_list = Post.objects.filter( user_id__in=user_ids).order_by('-pub_date')[0:10] set_user_for_sharding(post_list, shard) # The list comprehension actually invokes the db query in the QuerySet. all_posts = all_posts + [p for p in post_list] # TODO: Note that all_posts still needs to be sorted by time again, because # it is only ordered within each shard. context = { 'post_list': all_posts, 'my_post' : my_post, 'post_form' : PostForm } return render(request, 'micro/home.html', context)
def upload(request): if request.method == "GET": form = UploadFileForm() return render(request, 'web/upload', {'form': form}) else: musician_id = request.user.profile.musician_id song_id = findId(Song, 32, musician_id, False) album_query = Album.objects set_user_for_sharding(album_query, musician_id) album = album_query.get(_id=request.POST['album_id']) new_song = Song(musician_id=musician_id, album_id=album, _id=song_id, title=request.POST['title'], media=request.FILES['media']) # set_user_for_sharding(new_song, musician_id) new_song.save() return redirect('/streeTunes/dashboard/') pass pass
def dashboard(request): # Check if this is a first login (ie. just after signup) if request.session.has_key('first_login'): request.session.pop('first_login') return redirect('/streeTunes/profile/') musician_id = request.user.profile.musician_id data = [] albums = Album.objects.filter(musician_id=musician_id) set_user_for_sharding(albums, musician_id) for album in albums: songs = Song.objects.filter(musician_id=musician_id, album_id=album._id) set_user_for_sharding(songs, musician_id) data.append({ "album_id": album._id, "title": album.title, "songs": songs }) return render(request, 'web/dashboard.html', { "data": data, "username": request.user.username })
def emailView(request, user_id, device_id): devices = Device.objects.filter(user_id=user_id, id=device_id) set_user_for_sharding(devices, user_id) device = devices[0] if request.method == 'GET': form = ContactForm() else: form = ContactForm(request.POST) if form.is_valid(): owners = Profile.objects.filter(user_id=device.user_id) set_user_for_sharding(owners, device.user_id) owner = owners[0] owner_email = owner.email owner_name = owner.first_name device_name = device.name subject = 'Found Device: %s' % device_name from_email = settings.EMAIL_HOST_USER finder_name = form.cleaned_data['first_name'] body = email_body(device_name, finder_name, owner_name, form.cleaned_data['email']) try: send_mail(subject, body, from_email, [owner_email], fail_silently=False) except BadHeaderError: return HttpResponse('Invalid header found.') return redirect('success') context = { 'form': form, 'device': device.name, } return render(request, "micro/email.html", context)
def public_profile(request, parameter_user_id): try: request_user = request.user if request_user == None: # Requesting user is not logged in, will always return public profile views # query user shards user = get_user_from_cache(parameter_user_id) # query items shards item_query = Item.objects set_user_for_sharding(item_query, parameter_user_id) items = item_query.filter(owner=user) context = { 'user': user, 'items': items, } return render(request, 'my_qrcode/public_profile.html', context) else: # Requesting user is checking their own profile, allowed to edit if request_user.id == parameter_user_id: # User was matched, shown admin-rights profile page # query user shards user = get_user_from_cache(parameter_user_id) # query items shards item_query = Item.objects set_user_for_sharding(item_query, parameter_user_id) items = item_query.filter(owner=user) context = { 'user': user, 'items': items, } return render(request, 'my_qrcode/profile.html', context) else: # query user shards user = get_user_from_cache(parameter_user_id) # query items shards item_query = Item.objects set_user_for_sharding(item_query, parameter_user_id) items = item_query.filter(owner=user) context = { 'user': user, 'items': items, } return render(request, 'my_qrcode/public_profile.html', context) except FinderUser.DoesNotExist or UnboundLocalError: return flashHomeMessage( request, 'Sorry, we could\'t find a user by that specification')
def public_profile(request, parameter_user_id): try: request_user = request.user if request_user == None: # Requesting user is not logged in, will always return public profile views # query user shards user = get_user_from_cache(parameter_user_id) # query items shards item_query = Item.objects set_user_for_sharding(item_query, parameter_user_id) items = item_query.filter(owner=user) context = { 'user': user, 'items': items, } return render(request, 'my_qrcode/public_profile.html', context) else: # Requesting user is checking their own profile, allowed to edit if request_user.id == parameter_user_id: # User was matched, shown admin-rights profile page # query user shards user = get_user_from_cache(parameter_user_id) # query items shards item_query = Item.objects set_user_for_sharding(item_query, parameter_user_id) items = item_query.filter(owner=user) context = { 'user': user, 'items': items, } return render(request, 'my_qrcode/profile.html', context) else: # query user shards user = get_user_from_cache(parameter_user_id) # query items shards item_query = Item.objects set_user_for_sharding(item_query, parameter_user_id) items = item_query.filter(owner=user) context = { 'user': user, 'items': items, } return render(request, 'my_qrcode/public_profile.html', context) except FinderUser.DoesNotExist or UnboundLocalError: return flashHomeMessage(request, 'Sorry, we could\'t find a user by that specification')
def profile(request): user = request.user # TODO: a FinderUser is not created when a super user is created, # so we always make one for the user here cache = caches['users'] user_cach_key = 'qr_user_' + str(user.id) finderUser = cache.get(user_cach_key) if finderUser is None: try: # get user from shard user_query = FinderUser.objects set_user_for_sharding(user_query, user.id) finderUser = user_query.get(user_id=user.id) except FinderUser.DoesNotExist: createFinderUser(user) # get user from shard user_query = FinderUser.objects set_user_for_sharding(user_query, user.id) finderUser = user_query.get(user_id=user.id) cache.set(user_cach_key, finderUser) else: print 'User is in the cache' # get item from shard # item_cache_key = 'qr_items_' + str(user.id) # items = cache.get(item_cache_key) # if items is None: # item_query = Item.objects # set_user_for_sharding(item_query, user.id) # items = item_query.filter(owner=finderUser) # cache.set(item_cache_key, [item.item_id for item in items]) # store the item_id's so that we can get each from the cache # else: # item_vals = list() # still_need = list() # print 'Items are in cache' # cache = get_cache('items') # for item_id in items: # single_item_cache_key = 'qr_item_' + str(item_id) # have single item cache as well so that # cached_item = cache.get(single_item_cache_key) # if cached_item: # item_vals.append(cached_item) # else: # still_need.append(long(item_id)) # if len(still_need) > 0: # item_query = Item.objects # set_user_for_sharding(item_query, user.id) # items = item_query.filter(owner=finderUser, item_id__in=still_need) # for item in items: # item_vals.append(item) item_query = Item.objects set_user_for_sharding(item_query, user.id) items = item_query.filter(owner=finderUser) context = { 'user': finderUser, 'items': items, } return render(request, 'my_qrcode/profile.html', context)