Skip to content

Commit

Permalink
Pagination support for recent scans
Browse files Browse the repository at this point in the history
  • Loading branch information
ajinabraham committed May 24, 2024
2 parents 8b97723 + 9417595 commit e357823
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 7 deletions.
2 changes: 1 addition & 1 deletion mobsf/MobSF/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
"""
# ===============================================
DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'
DEBUG = False
DEBUG = bool(os.getenv('MOBSF_DEBUG', '0') == '1')
DJANGO_LOG_LEVEL = DEBUG
TEMPLATE_DEBUG = DEBUG
ALLOWED_HOSTS = ['127.0.0.1', 'mobsf', '*']
Expand Down
5 changes: 5 additions & 0 deletions mobsf/MobSF/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@

bundle_id_regex = r'(?P<bundle_id>([a-zA-Z]{1}[\w.-]{1,255}))$'
checksum_regex = r'(?P<checksum>[0-9a-f]{32})'
paginate = r'(?P<page_size>[0-9]{1,10})/(?P<page_number>[0-9]{1,10})'

urlpatterns = [
re_path(r'^login/$',
authentication.login_view,
Expand Down Expand Up @@ -188,6 +190,9 @@
re_path(r'^donate$', home.donate, name='donate'),
re_path(r'^api_docs$', home.api_docs, name='api_docs'),
re_path(r'^recent_scans/$', home.recent_scans, name='recent'),
re_path(fr'^recent_scans/{paginate}/$',
home.recent_scans,
name='scans_paginated'),
re_path(r'^delete_scan/$', home.delete_scan, name='delete_scan'),
re_path(r'^search$', home.search),
re_path(r'^error/$', home.error, name='error'),
Expand Down
22 changes: 17 additions & 5 deletions mobsf/MobSF/views/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,21 @@ def dynamic_analysis(request):


@login_required
def recent_scans(request):
def recent_scans(request, page_size=10, page_number=1):
"""Show Recent Scans Route."""
entries = []
db_obj = RecentScansDB.objects.all().order_by('-TIMESTAMP').values()
android = StaticAnalyzerAndroid.objects.all()
ios = StaticAnalyzerIOS.objects.all()
paginator = Paginator(
RecentScansDB.objects.all().order_by('-TIMESTAMP').values(), page_size)
page_obj = paginator.get_page(page_number)
page_obj.page_size = page_size
md5_list = [i['MD5'] for i in page_obj]

android = StaticAnalyzerAndroid.objects.filter(
MD5__in=md5_list).only(
'PACKAGE_NAME', 'VERSION_NAME', 'FILE_NAME', 'MD5')
ios = StaticAnalyzerIOS.objects.filter(
MD5__in=md5_list).only('FILE_NAME', 'MD5')

updir = Path(settings.UPLD_DIR)
icon_mapping = {}
package_mapping = {}
Expand All @@ -268,12 +277,14 @@ def recent_scans(request):
icon_mapping[item.MD5] = item.ICON_PATH
for item in ios:
icon_mapping[item.MD5] = item.ICON_PATH
for entry in db_obj:

for entry in page_obj:
if entry['MD5'] in package_mapping.keys():
entry['PACKAGE'] = package_mapping[entry['MD5']]
else:
entry['PACKAGE'] = ''
entry['ICON_PATH'] = icon_mapping.get(entry['MD5'], '')

if entry['FILE_NAME'].endswith('.ipa'):
entry['BUNDLE_HASH'] = get_md5(
entry['PACKAGE_NAME'].encode('utf-8'))
Expand All @@ -286,6 +297,7 @@ def recent_scans(request):
'title': 'Recent Scans',
'entries': entries,
'version': settings.MOBSF_VER,
'page_obj': page_obj,
}
template = 'general/recent.html'
return render(request, template, context)
Expand Down
52 changes: 51 additions & 1 deletion mobsf/templates/general/recent.html
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,57 @@ <h3 class="box-title"><i class="fa fa-rocket"></i> Recent Scans</h3>
</tbody>
</table>
</div>

<!--pagination-->
<nav aria-label="Pagination">
<ul class="pagination justify-content-center">
{% if page_obj.has_previous %}
<li class="page-item">
<a class="page-link" href="{% url 'scans_paginated' page_size=page_obj.page_size page_number=page_obj.previous_page_number %}">Previous</a>
</li>
{% else %}
<li class="page-item">
<a class="page-link">Previous</a>
</li>
{% endif %}

{% for i in page_obj.paginator.page_range %}
{% if page_obj.number == i %}
<li class="page-item active">
<a class="page-link" href="#">{{ i }} </a>
</li>
{% else %}
<li class="page-item">
<a class="page-link" href="{% url 'scans_paginated' page_size=page_obj.page_size page_number=i %}">{{ i }}</a>
</li>
{% endif %}
{% endfor %}

{% if page_obj.has_next %}
<li class="page-item">
<a class="page-link" href="{% url 'scans_paginated' page_size=page_obj.page_size page_number=page_obj.next_page_number %}">Next</a>
</li>
{% else %}
<li class="page-item">
<a class="page-link">Next</a>
</li>
{% endif %}
<li class="page-item">
<div class="dropdown">
<button class="page-link dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Items per page
</button>
<div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
<a class="dropdown-item" href="{% url 'scans_paginated' page_size=5 page_number=page_obj.number %}">5</a>
<a class="dropdown-item" href="{% url 'scans_paginated' page_size=10 page_number=page_obj.number %}">10</a>
<a class="dropdown-item" href="{% url 'scans_paginated' page_size=20 page_number=page_obj.number %}">20</a>
<a class="dropdown-item" href="{% url 'scans_paginated' page_size=50 page_number=page_obj.number %}">50</a>
<a class="dropdown-item" href="{% url 'scans_paginated' page_size=100 page_number=page_obj.number %}">100</a>
<a class="dropdown-item" href="{% url 'scans_paginated' page_size=250 page_number=page_obj.number %}">250</a>
</div>
</div>
</ul>
</nav>
<!--end pagination-->
</div>
<!-- /.box-body -->
</div>
Expand Down

0 comments on commit e357823

Please sign in to comment.